<?php

namespace App\Http\Controllers\Admin\Hr;

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Hr\Attendance\CheckInRequest;
use App\Http\Requests\Admin\Hr\Attendance\CheckOutRequest;
use App\Models\Attendance;
use App\Services\Hr\AttendanceService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class AttendanceController extends Controller
{
    use FlashesEntityMessage;

    public function __construct(
        private readonly AttendanceService $attendanceService,
    ) {}

    public function index(Request $request): Response
    {
        $start = $request->date('start') ?? now()->startOfMonth();
        $end = $request->date('end') ?? now()->endOfMonth();

        return Inertia::render('admin/hr/attendances/Index', $this->attendanceService->indexPageData(
            $request->user(),
            $start,
            $end,
        ));
    }

    public function checkIn(CheckInRequest $request): RedirectResponse
    {
        $this->attendanceService->checkIn($request->validated(), $request->user());

        $this->flashSuccess('Presensi masuk berhasil dicatat.');

        return redirect()->route('admin.hr.attendances.index');
    }

    public function checkOut(CheckOutRequest $request): RedirectResponse
    {
        $this->attendanceService->checkOut($request->validated(), $request->user());

        $this->flashSuccess('Presensi pulang berhasil dicatat.');

        return redirect()->route('admin.hr.attendances.index');
    }

    public function destroy(Attendance $attendance): RedirectResponse
    {
        $this->attendanceService->delete($attendance);

        $this->flashDeleted('Presensi');

        return redirect()->route('admin.hr.attendances.index');
    }
}
