<?php

namespace App\Modules\HR\Reports\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Admin\Tenancy\Services\TenantContext;
use App\Modules\HR\Reports\Repositories\WorkforceReportRepository;
use App\Modules\HR\Reports\Requests\HrReportRequest;
use App\Modules\HR\Reports\Services\HrReportService;
use App\Modules\HR\Reports\Support\HrReportKey;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class HrReportsPageController extends Controller
{
    public function __construct(
        private readonly HrReportService $reports,
        private readonly WorkforceReportRepository $workforce,
        private readonly TenantContext $tenantContext,
    ) {}

    public function index(): View
    {
        return view('pages.hr.reports.index', [
            'groups' => HrReportKey::grouped(),
        ]);
    }

    public function show(HrReportRequest $request, HrReportKey $report): View|RedirectResponse
    {
        if ($report === HrReportKey::ATTENDANCE_MONTHLY) {
            return redirect()->route('hr.attendance.monthly.index', $request->validated());
        }

        $tenantId = (int) ($this->tenantContext->tenantId() ?? $request->user()->tenant_id);

        return view('pages.hr.reports.show', [
            'report' => $report,
            'rows' => $this->reports->paginate($tenantId, $report, $request->validated()),
            'departments' => $this->workforce->departments($tenantId),
            'filters' => $request->validated(),
        ]);
    }
}
