<?php

namespace App\Jobs;

use App\Models\AttendancePunch;
use App\Services\Biometric\PunchAttendanceApplicator;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessZktecoPunchJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries = 3;

    public function __construct(
        public int $punchId
    ) {}

    public function handle(PunchAttendanceApplicator $applicator): void
    {
        $punch = AttendancePunch::withoutGlobalScopes()->find($this->punchId);
        if (! $punch) {
            return;
        }

        if ($punch->status === AttendancePunch::STATUS_APPLIED) {
            return;
        }

        $applicator->apply($punch);
    }
}
