<?php

namespace App\Modules\HR\EmployeeSelfService\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\HR\EmployeeSelfService\Requests\MeStoreLeaveRequestRequest;
use App\Modules\HR\EmployeeSelfService\Services\SelfEmployeeContextService;
use App\Modules\HR\Leave\DTOs\LeaveRequestSubmitData;
use App\Modules\HR\Leave\Models\LeaveRequest;
use App\Modules\HR\Leave\Repositories\LeaveRequestRepository;
use App\Modules\HR\Leave\Repositories\LeaveTypeRepository;
use App\Modules\HR\Leave\Services\LeaveCancellationService;
use App\Modules\HR\Leave\Services\LeaveRequestService;
use App\Modules\HR\Leave\Services\LeaveTypeBootstrapService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class MeLeaveController extends Controller
{
    use AuthorizesRequests;

    public function __construct(
        private readonly SelfEmployeeContextService $context,
        private readonly LeaveRequestRepository $requests,
        private readonly LeaveTypeRepository $types,
        private readonly LeaveTypeBootstrapService $typeBootstrap,
        private readonly LeaveRequestService $submit,
        private readonly LeaveCancellationService $cancellation,
    ) {}

    public function index(): View
    {
        $employee = $this->context->employee();
        $tenant = $this->tenant();
        $paginator = $this->requests->paginateForEmployee((int) $tenant->id, (int) $employee->id);

        return view('pages.me.leave.index', compact('paginator'));
    }

    public function create(): View
    {
        $this->authorize('create', LeaveRequest::class);
        $tenant = $this->tenant();
        $this->typeBootstrap->ensureDefaults($tenant);

        return view('pages.me.leave.create', [
            'types' => $this->types->activeForTenant((int) $tenant->id),
        ]);
    }

    public function store(MeStoreLeaveRequestRequest $request): RedirectResponse
    {
        $employee = $this->context->employee();
        $tenant = $this->tenant();
        $v = $request->validated();
        $this->submit->submit(
            $tenant,
            LeaveRequestSubmitData::fromValidated([
                'employee_id' => (int) $employee->id,
                'leave_type_id' => (int) $v['leave_type_id'],
                'start_date' => $v['start_date'],
                'end_date' => $v['end_date'],
                'reason' => $v['reason'] ?? null,
                'is_half_day' => (bool) ($v['is_half_day'] ?? false),
            ]),
            (int) $request->user()->id,
            $request->file('attachment'),
        );

        return redirect()->route('me.leaves.index')->with('status', __('leave.flash.submitted'));
    }

    public function cancel(int $leaveRequest): RedirectResponse
    {
        $tenant = $this->tenant();
        $row = $this->requests->findForTenant((int) $tenant->id, $leaveRequest);
        abort_if($row === null, 404);
        $this->authorize('cancel', $row);
        $this->cancellation->cancel($row, request()->user());

        return redirect()->route('me.leaves.index')->with('status', __('leave.flash.cancelled'));
    }

    private function tenant(): Tenant
    {
        $t = auth()->user()?->tenant;
        abort_if($t === null, 403);

        return $t;
    }
}
