<?php

namespace App\Controllers;

use App\Services\DashboardAdminService;
use App\Services\DashboardComunityService;
use App\Services\DashboardUgelService;
use App\Services\ProcessService;
use App\Services\UserService;

class DashboardController extends BaseController
{

    protected ProcessService $processService;

    public function __construct() {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }

        $this->processService = new ProcessService();
    }

    public function index()
    {
        $role = $_SESSION['role'];

        switch ($role) {
            case 'ADMIN':
                $this->indexAdmin();
                break;
            case 'UGEL':
                $this->indexUgel();
                break;
            case 'COMUNITY':
                $this->indexComunity();
                break;
            default:
                header("Location: " . BASE_HEADER);
                exit;
        }
    }

    public function indexAdmin()
    {
        $dashboardService = new DashboardAdminService();

        $period = $_GET['period'] ?? date('Y');
        $availablePeriods = $this->processService->getDistinctPeriods();
        $processSummary = $dashboardService->getAdminProcessSummaryByGeneralState($period);

        $cardCounts = [
            'Trámites totales' => array_sum($processSummary), 
            'En proceso'       => $processSummary['EN PROCESO'] ?? 0,
            'Observado'        => $processSummary['OBSERVADO'] ?? 0,
            'Resuelto'         => $processSummary['RESUELTO'] ?? 0,
        ];

        $processesByAreaAndState = $dashboardService->getProcessCountsByAreaAndGeneralState($period);
        $processesByPriority  = $dashboardService->getGlobalProcessStatesByPeriod($period);
        $urgentProcesses = $dashboardService->getUrgentProcesses($period);
        $processStatesByArea = $dashboardService->getCurrentProcessStatesByArea($period);


        $this->render('dashboard/dashboard-admin', [
            'availablePeriods' => $availablePeriods,
            'cardCounts' => $cardCounts,
            'processesByAreaAndState' => $processesByAreaAndState,
            'processesByPriority' => $processesByPriority,
            'urgentProcesses' => $urgentProcesses,
            'processStatesByArea' => $processStatesByArea,
            'currentPeriod' => $period
        ]);
    }

    public function indexUgel()
    {
        $user = UserService::getUser();
        $groupId = $user->group_id;
        $subgroupId = $user->subgroup_id;

        $dashboardService = new DashboardUgelService();

        $period = $_GET['period'] ?? date('Y');
        $availablePeriods = $this->processService->getDistinctPeriods();

        $processSummary = $dashboardService->getUgelProcessSummaryByGeneralState($period, $groupId, $subgroupId);
        $prioritySummary = $dashboardService->getUgelProcessSummaryByPriority($period, $groupId, $subgroupId);
        $urgentProcesses = $dashboardService->getUrgentProcessesByGroupOrSubgroup($period, $groupId, $subgroupId);
        $lastNotifications = $dashboardService->getLastGroupOrSubgroupNotifications($groupId, $subgroupId);
        $summaryByLocationState = $dashboardService->getUgelProcessSummaryByLocationState($period, $groupId, $subgroupId);
        $lastDerivedProcesses = $dashboardService->getLastDerivedProcesses($period, $groupId, $subgroupId);

        $cardCounts = [
            'Trámites totales' => array_sum($processSummary),
            'En proceso'       => $processSummary['EN PROCESO'] ?? 0,
            'Observado'        => $processSummary['OBSERVADO'] ?? 0,
            'Resuelto'         => $processSummary['RESUELTO'] ?? 0,
        ];


        $this->render('dashboard/dashboard-ugel', [
            'availablePeriods' => $availablePeriods,
            'currentPeriod' => $period,
            'cardCounts' => $cardCounts,
            'processSummary' => $processSummary,
            'prioritySummary' => $prioritySummary,
            'urgentProcesses' => $urgentProcesses,
            'lastNotifications' => $lastNotifications,
            'summaryByLocationState' => $summaryByLocationState,
            'lastDerivedProcesses' => $lastDerivedProcesses,
        ]);
    }

    public function indexComunity()
    {
        $user = UserService::getUser();
        $dashboardService = new DashboardComunityService();

        $responseSummary = $dashboardService->getResponsesSummaryByType($user->id);
        $processSummary = $dashboardService->getProcessSummaryByState($user->id);
        $lastProcesses = $dashboardService->getLastProcesses($user->id);
        $lastNotifications = $dashboardService->getLastNotifications($user->user_id);

        $cardCounts = [
            'Trámites totales' => array_sum($processSummary), 
            'En proceso'       => $processSummary['EN PROCESO'] ?? 0,
            'Observado'        => $processSummary['OBSERVADO'] ?? 0,
            'Resuelto'         => $processSummary['RESUELTO'] ?? 0,
        ];
        

        $this->render('dashboard/dashboard-comunity', [
            'cardCounts' => $cardCounts,
            'responseSummary' => $responseSummary,
            'processSummary' => $processSummary,
            'lastProcesses' => $lastProcesses,
            'lastNotifications' => $lastNotifications
        ]);
    }

}