<?php

namespace Modules\HR\Repositories;

use Modules\Core\Enums\StatusEnum;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Models\BiometricDevice;

class BiometricDeviceRepository
{
    public function all(HrSettingFilter $filter, $columns = ['*'])
    {
        return BiometricDevice::query()
            ->filterStatus($filter->status)
            ->select($columns)
            ->search($filter->search)
            ->paginate($filter->perPage ?? 15);
    }

    public function find($id)
    {
        return BiometricDevice::findOrFail($id);
    }

    public function create(array $data)
    {
        return BiometricDevice::create([
            'branch_id' => $data['branch_id'] ?? null,
            'name' => $data['name'],
            'device_uid' => $data['device_uid'],
            'ip_address' => $data['ip_address'],
            'port' => $data['port'],
            'location' => $data['location'],
            'status' => $data['status'] ?? StatusEnum::ACTIVE,
        ]);
    }

    public function update(BiometricDevice $biometricDevice, array $data)
    {
        $biometricDevice->update([
            'branch_id' => $data['branch_id'] ?? null,
            'name' => $data['name'],
            'device_uid' => $data['device_uid'],
            'ip_address' => $data['ip_address'],
            'port' => $data['port'],
            'location' => $data['location'],
            'status' => $data['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $biometricDevice->fresh();
    }

    public function updateStatus(BiometricDevice $biometricDevice, array $status)
    {
        $biometricDevice->update([
            'status' => $status['status'] ?? StatusEnum::ACTIVE,
        ]);

        return $biometricDevice;
    }
}
