<?php

namespace App\Modules\HR\Overtime\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Overtime\DTOs\OvertimeRequestSubmitData;
use App\Modules\HR\Overtime\Models\OvertimeRequest;
use App\Modules\HR\Overtime\Repositories\OvertimeRequestRepository;
use App\Modules\HR\Overtime\Requests\StoreOvertimeRequestRequest;
use App\Modules\HR\Overtime\Services\OvertimeCreatePageService;
use App\Modules\HR\Overtime\Services\OvertimeRequestService;
use App\Modules\HR\Overtime\Services\OvertimeReviewService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class OvertimeRequestsPageController extends Controller
{
    public function __construct(
        private readonly OvertimeRequestRepository $requests,
        private readonly OvertimeRequestService $submit,
        private readonly OvertimeReviewService $review,
        private readonly OvertimeCreatePageService $createPage,
    ) {}

    public function index(): View
    {
        $this->authorize('viewAny', OvertimeRequest::class);

        return view('pages.hr.overtime.index', [
            'requests' => $this->requests->paginateForTenant((int) $this->tenant()->id),
        ]);
    }

    public function create(Request $request): View
    {
        $this->authorize('create', OvertimeRequest::class);
        $employeeId = $request->filled('employee_id') ? (int) $request->query('employee_id') : null;
        $date = $request->filled('attendance_date') ? (string) $request->query('attendance_date') : null;

        return view('pages.hr.overtime.create', $this->createPage->forHrCreate(
            $this->tenant(),
            $employeeId,
            $date,
        ));
    }

    public function store(StoreOvertimeRequestRequest $request): RedirectResponse
    {
        $this->submit->submit(
            $this->tenant(),
            OvertimeRequestSubmitData::fromValidated($request->validated()),
            (int) $request->user()->id,
        );

        return redirect()
            ->route('hr.overtime.index')
            ->with('status', __('overtime.flash.submitted'));
    }

    public function approve(int $overtimeRequest): RedirectResponse
    {
        $row = $this->findOrFail($overtimeRequest);
        $this->authorize('review', $row);
        $this->review->approve($row, request()->user());

        return redirect()
            ->route('hr.overtime.index')
            ->with('status', __('overtime.flash.approved'));
    }

    public function reject(int $overtimeRequest): RedirectResponse
    {
        $row = $this->findOrFail($overtimeRequest);
        $this->authorize('review', $row);
        $this->review->reject($row, request()->user());

        return redirect()
            ->route('hr.overtime.index')
            ->with('status', __('overtime.flash.rejected'));
    }

    public function cancel(int $overtimeRequest): RedirectResponse
    {
        $row = $this->findOrFail($overtimeRequest);
        $this->authorize('cancel', $row);
        $this->submit->cancel($row, (int) request()->user()->id);

        return redirect()
            ->route('hr.overtime.index')
            ->with('status', __('overtime.flash.cancelled'));
    }

    private function findOrFail(int $id): OvertimeRequest
    {
        $row = $this->requests->findForTenant((int) $this->tenant()->id, $id);
        abort_if($row === null, 404);

        return $row;
    }

    private function tenant(): \App\Modules\Admin\Tenancy\Models\Tenant
    {
        $t = auth()->user()?->tenant;
        abort_if($t === null, 403);

        return $t;
    }
}
