<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreAttendanceDeviceRequest;
use App\Http\Requests\UpdateAttendanceDeviceRequest;
use App\Models\AttendanceDevice;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class AttendanceDeviceController extends Controller
{
    private const PER_PAGE_OPTIONS = [5, 10, 25, 50];

    public function index(Request $request): Response
    {
        $perPage = $request->integer('per_page', 10);
        if (! in_array($perPage, self::PER_PAGE_OPTIONS, true)) {
            $perPage = 10;
        }

        $search = $request->string('search')->trim()->toString();

        $devices = AttendanceDevice::query()
            ->withCount('enrollments', 'punches')
            ->when($search !== '', function ($query) use ($search): void {
                $query->where(function ($q) use ($search): void {
                    $q->where('name', 'like', '%'.$search.'%')
                        ->orWhere('serial_number', 'like', '%'.$search.'%')
                        ->orWhere('location', 'like', '%'.$search.'%');
                });
            })
            ->orderByDesc('id')
            ->paginate($perPage)
            ->withQueryString()
            ->through(fn (AttendanceDevice $device): array => [
                'id' => $device->id,
                'name' => $device->name,
                'serial_number' => $device->serial_number,
                'location' => $device->location,
                'is_active' => $device->is_active,
                'last_sync_at' => $device->last_sync_at?->toIso8601String(),
                'enrollments_count' => $device->enrollments_count,
                'punches_count' => $device->punches_count,
            ]);

        return Inertia::render('employees/attendance/devices/index', [
            'devices' => $devices,
            'filters' => [
                'search' => $search,
                'per_page' => $perPage,
            ],
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('employees/attendance/devices/create');
    }

    public function store(StoreAttendanceDeviceRequest $request): RedirectResponse
    {
        AttendanceDevice::query()->create($request->validated());

        return redirect()
            ->route('employees.attendance.devices.index')
            ->with('status', __('Attendance device registered.'));
    }

    public function edit(AttendanceDevice $attendanceDevice): Response
    {
        return Inertia::render('employees/attendance/devices/edit', [
            'device' => [
                'id' => $attendanceDevice->id,
                'name' => $attendanceDevice->name,
                'serial_number' => $attendanceDevice->serial_number,
                'location' => $attendanceDevice->location ?? '',
                'is_active' => $attendanceDevice->is_active,
            ],
        ]);
    }

    public function update(UpdateAttendanceDeviceRequest $request, AttendanceDevice $attendanceDevice): RedirectResponse
    {
        $attendanceDevice->fill($request->validated());
        $attendanceDevice->save();

        return redirect()
            ->route('employees.attendance.devices.index')
            ->with('status', __('Attendance device updated.'));
    }
}
