<?php

namespace App\Modules\HR\EmployeeSelfService\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Payroll\Services\PayslipViewService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class MePayslipsPageController extends Controller
{
    public function __construct(
        private readonly PayslipViewService $payslips,
    ) {}

    public function index(Request $request): View
    {
        $user = $request->user();
        abort_if($user === null || $user->tenant_id === null || $user->employee_id === null, 403);
        $paginator = $this->payslips->listForEmployee((int) $user->tenant_id, (int) $user->employee_id);

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

    public function show(Request $request, int $snapshot): View
    {
        $user = $request->user();
        abort_if($user === null || $user->tenant_id === null || $user->employee_id === null, 403);
        $row = $this->payslips->buildEmployeePayslip((int) $user->tenant_id, (int) $user->employee_id, $snapshot);
        $breakdown = $this->payslips->breakdown($row);

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

