<?php

namespace Modules\Transport\Services;

use App\Models\TransportDriverModel;
use App\Models\TransportLocationModel;
use App\Models\TransportScheduleModel;
use App\Models\TransportVehicleModel;

class TransportReportService
{
    protected TransportVehicleModel $vehicles;
    protected TransportDriverModel $drivers;
    protected TransportScheduleModel $schedules;
    protected TransportLocationModel $locations;

    public function __construct()
    {
        $this->vehicles   = new TransportVehicleModel();
        $this->drivers    = new TransportDriverModel();
        $this->schedules  = new TransportScheduleModel();
        $this->locations  = new TransportLocationModel();
    }

    /**
     * @param array<string, mixed> $get
     * @return array<string, mixed>
     */
    public function resolveFilters(array $get): array
    {
        $dateFrom = trim((string) ($get['date_from'] ?? ''));
        $dateTo   = trim((string) ($get['date_to'] ?? ''));

        if ($dateFrom === '') {
            $dateFrom = date('Y-m-01');
        }
        if ($dateTo === '') {
            $dateTo = date('Y-m-d');
        }

        return [
            'date_from' => $dateFrom,
            'date_to'   => $dateTo,
            'status'    => trim((string) ($get['status'] ?? '')) ?: null,
        ];
    }

    /**
     * @param array<string, mixed> $filters
     * @return array<string, mixed>
     */
    public function summary(array $filters): array
    {
        $vehicleCounts  = $this->vehicles->countByStatus();
        $driverCounts   = $this->drivers->countByStatus();
        $scheduleCounts = $this->schedules->countByStatus();

        $tripQuery = $this->schedules->scopeForList(null, $filters['status'], $filters['date_from'], $filters['date_to'])
            ->orderBy('transport_schedules.schedule_date', 'DESC');

        $trips = $tripQuery->findAll(50);

        $completedInPeriod = (new TransportScheduleModel())
            ->where('status', 'completed')
            ->where('schedule_date >=', $filters['date_from'])
            ->where('schedule_date <=', $filters['date_to'])
            ->countAllResults();

        $expiringLicenses = $this->drivers->scopeForList(null, 'active')
            ->where('transport_drivers.license_expiry IS NOT NULL')
            ->where('transport_drivers.license_expiry <=', date('Y-m-d', strtotime('+30 days')))
            ->orderBy('transport_drivers.license_expiry', 'ASC')
            ->findAll(20);

        $maintenanceVehicles = $this->vehicles->scopeForList(null, 'maintenance')
            ->orderBy('transport_vehicles.registration_number', 'ASC')
            ->findAll(20);

        return [
            'vehicleCounts'       => $vehicleCounts,
            'driverCounts'        => $driverCounts,
            'scheduleCounts'      => $scheduleCounts,
            'completedInPeriod'   => $completedInPeriod,
            'locationCount'       => $this->locations->where('status', 'active')->countAllResults(),
            'trips'               => $trips,
            'expiringLicenses'    => $expiringLicenses,
            'maintenanceVehicles' => $maintenanceVehicles,
        ];
    }
}
