<?php

namespace App\Modules\Payroll\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\Payroll\Models\PayrollEmployeeSnapshot;
use App\Modules\Payroll\Services\PayslipViewService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\View\View;

class PayrollPayslipsPageController extends Controller
{
    use AuthorizesRequests;

    public function __construct(
        private readonly PayslipViewService $payslips,
    ) {}

    public function show(int $snapshot): View
    {
        $tenant = $this->tenant();
        $row = $this->payslips->buildAdminPayslip((int) $tenant->id, $snapshot);
        $this->authorize('view', $row);
        $breakdown = $this->payslips->breakdown($row);

        return view('pages.payroll.payslips.show', [
            'snapshot' => $row,
            'breakdown' => $breakdown,
        ]);
    }

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

        return $t;
    }
}

