<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\HourRegistration\Database\Seeders;

use Illuminate\Database\Seeder;
use Maestro\Core\Modules\HourRegistration\Models\HourInternalCategory;
use Maestro\Core\Modules\HourRegistration\Models\HourProject;
use Maestro\Core\Support\AuthUserResolver;

/**
 * Seeds default hour-registration reference data (extensible categories).
 */
class HourRegistrationSeeder extends Seeder
{
    public function run(): void
    {
        $definitions = [
            ['name' => 'Sick leave', 'slug' => 'sick', 'sort_order' => 10],
            ['name' => 'Vacation', 'slug' => 'vacation', 'sort_order' => 20],
            ['name' => 'Training', 'slug' => 'training', 'sort_order' => 30],
            ['name' => 'Administrative internal', 'slug' => 'admin-internal', 'sort_order' => 40],
        ];

        foreach ($definitions as $row) {
            HourInternalCategory::query()->updateOrCreate(
                ['slug' => $row['slug']],
                [
                    'name' => $row['name'],
                    'sort_order' => $row['sort_order'],
                    'is_active' => true,
                ],
            );
        }

        /** @phpstan-ignore-next-line */
        $leadId = AuthUserResolver::eloquentModelClass()::query()->orderBy('id')->value('id');
        if ($leadId !== null) {
            HourProject::query()->updateOrCreate(
                ['name' => 'Default hour project'],
                [
                    'customer_id' => null,
                    /** @phpstan-ignore-next-line */
                    'project_lead_id' => (int) $leadId,
                    'is_active' => true,
                ],
            );
        }
    }
}
