<?php

namespace App\Http\Controllers\Portal;

use App\Http\Controllers\Controller;
use App\Models\ChangeRequest;
use App\Models\Payslip;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class PortalController extends Controller
{
    /** Run statuses an employee is allowed to see (never DRAFT/PROCESSING/CALCULATED). */
    public const VISIBLE_STATUSES = ['APPROVED', 'PAID', 'CLOSED'];

    public function home(Request $request): Response
    {
        $employee = $request->user()->employee;

        $payslips = Payslip::where('employee_id', $employee->id)
            ->whereHas('run', fn ($q) => $q->whereIn('status', self::VISIBLE_STATUSES))
            ->with('run:id,period,status,pay_date')
            ->latest('id')
            ->get()
            ->map(fn (Payslip $p) => [
                'id' => $p->id,
                'period' => $p->run?->period,
                'pay_date' => $p->run?->pay_date?->toDateString(),
                'gross' => (float) $p->gross,
                'total_deductions' => (float) $p->total_deductions,
                'net_pay' => (float) $p->net_pay,
            ]);

        return Inertia::render('portal/home', [
            'employee' => [
                'full_name' => $employee->full_name,
                'employee_code' => $employee->employee_code,
                'salary_structure' => $employee->salary_structure,
                'grade_level' => $employee->grade_level,
                'salary_step' => $employee->salary_step,
                'department' => $employee->department?->name,
            ],
            'payslips' => $payslips,
            'pendingRequests' => ChangeRequest::where('employee_id', $employee->id)
                ->where('status', ChangeRequest::STATUS_PENDING)->count(),
        ]);
    }

    public function profile(Request $request): Response
    {
        $employee = $request->user()->employee->load(['department:id,name', 'bankAccount', 'pensionAccount']);

        return Inertia::render('portal/profile', [
            'employee' => [
                'full_name' => $employee->full_name,
                'employee_code' => $employee->employee_code,
                'file_number' => $employee->file_number,
                'department' => $employee->department?->name,
                'phone' => $employee->phone,
                'gender' => $employee->gender,
                'marital_status' => $employee->marital_status,
                'union_name' => $employee->union_name,
                'date_of_birth' => $employee->date_of_birth?->toDateString(),
                'date_of_join' => $employee->date_of_join?->toDateString(),
                'bank' => $employee->bankAccount ? [
                    'bank_name' => $employee->bankAccount->bank_name,
                    'branch_bank' => $employee->bankAccount->branch_bank,
                    'account_type' => $employee->bankAccount->account_type,
                    'account_number' => $employee->bankAccount->account_number,
                ] : null,
                'pension' => $employee->pensionAccount ? [
                    'pension_house' => $employee->pensionAccount->pension_house,
                    'pen_number' => $employee->pensionAccount->pen_number,
                ] : null,
            ],
        ]);
    }
}
