<?php

namespace App\Modules\HR\Attendance\Jobs;

use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\Admin\Tenancy\Services\TenantContext;
use App\Modules\HR\Attendance\Services\AttendanceAbsenceGenerationService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GenerateAttendanceAbsencesJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public function __construct(
        public readonly int $tenantId,
        public readonly string $dateYmd,
    ) {}

    public function handle(
        TenantContext $tenantContext,
        AttendanceAbsenceGenerationService $absences,
    ): void {
        $tenantContext->runForTenant($this->tenantId, function () use ($absences): void {
            $tenant = Tenant::query()->find($this->tenantId);
            if ($tenant === null) {
                return;
            }
            $absences->generateForTenantDate($tenant, $this->dateYmd);
        });
    }
}
