<?php

namespace App\Controllers;

use App\Core\Controller;
use App\Helpers\Auth;
use App\Helpers\SessionHelper;
use App\Models\AdvanceSalary;
use App\Models\Claim;
use App\Models\LeaveRequest;
use App\Models\StaffProfile;
use PDO;

class DashboardController extends Controller
{
    private PDO $pdo;  // Declare property manually

    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo; // Assign manually (PHP 7.4 style)
    }

    public function index(): void
    {
        Auth::requireRole(['staff', 'approver', 'admin']);

        $user = SessionHelper::user();
        $advModel = new AdvanceSalary($this->pdo);
        $leaveModel = new LeaveRequest($this->pdo);
        $claimModel = new Claim($this->pdo);

        $filters = [];
        if ($user['role'] === 'staff') {
            $filters['user_id'] = $user['id'];
        }

        // Base lists for table view
        $advance = $advModel->paginate($filters, 5, 0);
        $leaves = $leaveModel->filter($filters, 5, 0);
        $claims = $claimModel->paginate($filters, 5, 0);

        // Build summary analytics per role
        $stats = [];
        $leaveStats = null;

        if ($user['role'] === 'staff') {
            $profileModel = new StaffProfile($this->pdo);
            $leaveStats = $profileModel->getLeaveStats($user['id']);
            
            $stats = [
                [
                    'label' => 'Jumlah Cuti Dibenarkan',
                    'value' => $leaveStats['allowed'],
                    'subtitle' => 'Hari cuti yang dibenarkan',
                    'variant' => 'info',
                ],
                [
                    'label' => 'Jumlah Cuti Semasa',
                    'value' => $leaveStats['used'],
                    'subtitle' => 'Hari cuti yang telah digunakan',
                    'variant' => 'warning',
                ],
                [
                    'label' => 'Baki Cuti',
                    'value' => $leaveStats['remaining'],
                    'subtitle' => 'Hari cuti yang masih tinggal',
                    'variant' => 'success',
                ],
            ];
        } elseif ($user['role'] === 'approver') {
            $pendingFilter = ['status' => 'pending'];
            $stats = [
                [
                    'label' => 'Advance Pending',
                    'value' => $advModel->count($pendingFilter),
                    'subtitle' => 'Perlu semakan & kelulusan',
                    'variant' => 'warning',
                ],
                [
                    'label' => 'Cuti Pending',
                    'value' => $leaveModel->count($pendingFilter),
                    'subtitle' => 'Permohonan cuti menunggu keputusan',
                    'variant' => 'info',
                ],
                [
                    'label' => 'Claim Pending',
                    'value' => $claimModel->count($pendingFilter),
                    'subtitle' => 'Tuntutan kewangan menunggu keputusan',
                    'variant' => 'primary',
                ],
            ];
        } elseif ($user['role'] === 'admin') {
            $stats = [
                [
                    'label' => 'Total Advance',
                    'value' => $advModel->count([]),
                    'subtitle' => 'Semua permohonan advance',
                    'variant' => 'primary',
                ],
                [
                    'label' => 'Total Cuti',
                    'value' => $leaveModel->count([]),
                    'subtitle' => 'Semua permohonan cuti',
                    'variant' => 'success',
                ],
                [
                    'label' => 'Total Claim',
                    'value' => $claimModel->count([]),
                    'subtitle' => 'Semua tuntutan kewangan',
                    'variant' => 'secondary',
                ],
            ];
        }

        $this->view('dashboard/index', [
            'advance' => $advance,
            'leaves' => $leaves,
            'claims' => $claims,
            'stats' => $stats,
            'leaveStats' => $leaveStats,
        ]);
    }
}

