<?php

namespace App\Modules\HR\Attendance\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Attendance\Repositories\BiometricDeviceRepository;
use App\Modules\HR\Attendance\Requests\StoreBiometricDeviceRequest;
use App\Modules\HR\Attendance\Support\BiometricApiType;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class BiometricDevicesPageController extends Controller
{
    public function index(BiometricDeviceRepository $devices): View
    {
        $tenant = auth()->user()?->tenant;
        abort_if($tenant === null, 403);

        return view('pages.hr.attendance.devices', [
            'devices' => $devices->forTenant((int) $tenant->id),
            'apiTypeOptions' => [
                BiometricApiType::GENERIC => __('attendance.api_types.generic'),
                BiometricApiType::ZKTECO => __('attendance.api_types.zkteco'),
                BiometricApiType::SUPREMA => __('attendance.api_types.suprema'),
                BiometricApiType::REST_WEBHOOK => __('attendance.api_types.rest_webhook'),
            ],
        ]);
    }

    public function store(StoreBiometricDeviceRequest $request, BiometricDeviceRepository $devices): RedirectResponse
    {
        $tenant = auth()->user()?->tenant;
        abort_if($tenant === null, 403);
        $devices->create(array_merge($request->validated(), [
            'tenant_id' => $tenant->id,
            'is_active' => true,
        ]));

        return redirect()->route('hr.attendance.devices.index')->with('status', __('attendance.flash.device_created'));
    }
}
