<?php

namespace Modules\Transport\Controllers;

use App\Controllers\BaseController;
use App\Models\TransportDriverModel;
use App\Models\TransportScheduleModel;
use App\Models\TransportVehicleModel;

class TransportDashboardController extends BaseController
{
    public function index()
    {
        $vehicleCounts  = (new TransportVehicleModel())->countByStatus();
        $driverCounts   = (new TransportDriverModel())->countByStatus();
        $scheduleCounts = (new TransportScheduleModel())->countByStatus();

        $activeVehicles = $vehicleCounts['active'] ?? 0;
        $activeDrivers  = $driverCounts['active'] ?? 0;
        $openTrips      = ($scheduleCounts['planned'] ?? 0)
            + ($scheduleCounts['scheduled'] ?? 0)
            + ($scheduleCounts['in_progress'] ?? 0);

        $upcoming = (new TransportScheduleModel())->getUpcoming(8);

        return view('Modules\Transport\Views\dashboard\index', array_merge($this->baseViewData(), [
            'title'    => 'Transport Dashboard',
            'kpis'     => [
                [
                    'label'     => 'Active vehicles',
                    'value'     => (string) $activeVehicles,
                    'icon'      => 'fa-truck',
                    'badge'     => 'Fleet',
                    'badgeType' => 'neutral',
                    'dept'      => 'Transport',
                ],
                [
                    'label'     => 'Active drivers',
                    'value'     => (string) $activeDrivers,
                    'icon'      => 'fa-id-card',
                    'badge'     => 'Staff',
                    'badgeType' => 'positive',
                    'dept'      => 'Drivers',
                ],
                [
                    'label'     => 'Open trips',
                    'value'     => (string) $openTrips,
                    'icon'      => 'fa-route',
                    'badge'     => 'Live',
                    'badgeType' => 'positive',
                    'dept'      => 'Scheduling',
                ],
                [
                    'label'     => 'In maintenance',
                    'value'     => (string) ($vehicleCounts['maintenance'] ?? 0),
                    'icon'      => 'fa-wrench',
                    'badge'     => ($vehicleCounts['maintenance'] ?? 0) > 0 ? 'Attention' : 'Clear',
                    'badgeType' => ($vehicleCounts['maintenance'] ?? 0) > 0 ? 'negative' : 'neutral',
                    'dept'      => 'Vehicles',
                ],
            ],
            'upcoming' => $upcoming,
        ]));
    }
}
