<?php

namespace App\Modules\HR\Attendance\Repositories;

use App\Modules\HR\Attendance\Models\BiometricLog;
use Illuminate\Support\Collection;

class BiometricLogRepository
{
    /**
     * @param  array<string, mixed>  $row
     */
    public function insertRaw(int $tenantId, ?int $deviceId, array $row): BiometricLog
    {
        return BiometricLog::query()->create([
            'tenant_id' => $tenantId,
            'employee_fingerprint_id' => $row['employee_fingerprint_id'],
            'device_id' => $deviceId,
            'punch_time' => $row['punch_time'],
            'raw_payload' => $row['raw_payload'] ?? null,
        ]);
    }

    /**
     * @return Collection<int, BiometricLog>
     */
    public function pendingForDevice(int $tenantId, int $deviceId, int $limit = 500): Collection
    {
        return BiometricLog::query()
            ->where('tenant_id', $tenantId)
            ->where('device_id', $deviceId)
            ->whereNull('processed_at')
            ->orderBy('punch_time')
            ->limit($limit)
            ->get();
    }

    public function markProcessed(BiometricLog $log): void
    {
        $log->forceFill(['processed_at' => now()])->save();
    }
}
