<?php

namespace App\Modules\HR\PermissionRequests\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Employees\Models\Employee;
use App\Modules\HR\PermissionRequests\DTOs\PermissionRequestSubmitData;
use App\Modules\HR\PermissionRequests\Models\PermissionRequest;
use App\Modules\HR\PermissionRequests\Repositories\PermissionRequestRepository;
use App\Modules\HR\PermissionRequests\Requests\StorePermissionRequestRequest;
use App\Modules\HR\PermissionRequests\Services\PermissionRequestReviewService;
use App\Modules\HR\PermissionRequests\Services\PermissionRequestService;
use App\Modules\HR\PermissionRequests\Support\PermissionRequestType;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class PermissionRequestsPageController extends Controller
{
    public function __construct(
        private readonly PermissionRequestRepository $requests,
        private readonly PermissionRequestService $submit,
        private readonly PermissionRequestReviewService $review,
    ) {}

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

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

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

        return view('pages.hr.permission-requests.create', [
            'employees' => Employee::query()->where('tenant_id', $tenant->id)->orderBy('full_name')->get(),
            'types' => $this->typeOptions(),
        ]);
    }

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

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

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

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

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

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

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

        return $row;
    }

    /**
     * @return array<string, string>
     */
    private function typeOptions(): array
    {
        $options = [];
        foreach (PermissionRequestType::all() as $type) {
            $options[$type] = __('permission_requests.types.'.$type);
        }

        return $options;
    }

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

        return $t;
    }
}
