<?php

namespace App\Modules\HR\Attendance\Repositories;

use App\Modules\HR\Attendance\Models\BiometricDevice;
use Illuminate\Database\Eloquent\Collection;

class BiometricDeviceRepository
{
    /**
     * @return Collection<int, BiometricDevice>
     */
    public function forTenant(int $tenantId): Collection
    {
        return BiometricDevice::query()
            ->where('tenant_id', $tenantId)
            ->orderBy('name')
            ->get();
    }

    public function findForTenant(int $tenantId, int $id): ?BiometricDevice
    {
        return BiometricDevice::query()
            ->where('tenant_id', $tenantId)
            ->whereKey($id)
            ->first();
    }

    public function findById(int $id): ?BiometricDevice
    {
        return BiometricDevice::query()->whereKey($id)->first();
    }

    /**
     * @param  array<string, mixed>  $data
     */
    public function create(array $data): BiometricDevice
    {
        return BiometricDevice::query()->create($data);
    }

    public function touchLastSync(BiometricDevice $device): void
    {
        $device->forceFill(['last_sync_at' => now()])->save();
    }
}
