<?php

namespace Database\Seeders;

use App\Models\Floor;
use App\Models\Unit;
use App\Models\UnitSubtype;
use App\Models\User;
use Illuminate\Database\Seeder;

class CatalogSeeder extends Seeder
{
    public function run(): void
    {
        $actor = User::query()->orderBy('id')->first();

        if ($actor === null) {
            return;
        }

        $catalog = [
            'Apartment' => ['Studio', '1 Bed', '2 Bed', '3 Bed'],
            'Shop' => ['Standard Shop', 'Corner Shop'],
        ];

        foreach ($catalog as $unitName => $subtypes) {
            $unit = Unit::query()->firstOrCreate(
                ['name' => $unitName],
                ['created_by' => $actor->id],
            );

            foreach ($subtypes as $subtypeName) {
                UnitSubtype::query()->firstOrCreate(
                    [
                        'unit_id' => $unit->id,
                        'name' => $subtypeName,
                    ],
                    ['created_by' => $actor->id],
                );
            }
        }

        foreach ([
            ['Ground', 0],
            ['1', 1],
            ['2', 2],
            ['3', 3],
            ['4', 4],
            ['5', 5],
            ['6', 6],
            ['7', 7],
            ['8', 8],
            ['9', 9],
            ['10', 10],
        ] as [$name, $sortOrder]) {
            Floor::query()->firstOrCreate(
                ['name' => $name],
                [
                    'sort_order' => $sortOrder,
                    'created_by' => $actor->id,
                ],
            );
        }
    }
}
