<?php

namespace App\Modules\HR\Leave\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Employees\Repositories\EmployeeRepository;
use App\Modules\HR\Leave\Models\LeaveRequest;
use App\Modules\HR\Leave\Requests\LeaveBalanceIndexRequest;
use App\Modules\HR\Leave\Services\LeaveBalancePageService;
use Illuminate\View\View;

class LeaveBalancesPageController extends Controller
{
    public function __construct(
        private readonly LeaveBalancePageService $page,
        private readonly EmployeeRepository $employees,
    ) {}

    public function index(LeaveBalanceIndexRequest $request): View
    {
        $this->authorize('viewAny', LeaveRequest::class);
        $tenant = auth()->user()?->tenant;
        abort_if($tenant === null, 403);
        $year = $request->year();
        $employeeId = $request->employeeId();

        return view('pages.hr.leave.balances.index', [
            'balances' => $this->page->paginate($tenant, $employeeId, $year),
            'employees' => $this->employees->listForSelect((int) $tenant->id),
            'year' => $year,
            'employeeId' => $employeeId,
        ]);
    }
}
