<?php

namespace App\Console\Commands;

use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\HR\Leave\Services\LeaveTypeBootstrapService;
use Illuminate\Console\Command;

class BootstrapLeaveTypesCommand extends Command
{
    protected $signature = 'hr:bootstrap-leave-types {--tenant=}';

    protected $description = 'Idempotently add missing default HR leave types';

    public function handle(LeaveTypeBootstrapService $service): int
    {
        $query = Tenant::query()->orderBy('id');
        if ($this->option('tenant')) {
            $query->whereKey((int) $this->option('tenant'));
        }
        $query->eachById(function (Tenant $tenant) use ($service): void {
            $service->ensureDefaults($tenant);
        });
        $this->info('Leave types synchronized.');

        return self::SUCCESS;
    }
}
