<?php
class FleetAnalyticsController extends Controller
{
    public function indexAction()
    {
        [$from, $to] = $this->resolveRange();
        $m = new FleetAnalytics();
        $this->view->setLayout($this->roleLayout());
        $this->view->render('FleetAnalytics/index', [
            'page_title' => 'Fleet Analytics',
            'from'       => $from,
            'to'         => $to,
            'preset'     => (string)($_GET['preset'] ?? 'ytd'),
            'summary'    => $m->fleetSummary($from, $to),
            'top_trucks' => array_slice($m->truckPnl($from, $to), 0, 5),
            'top_customers' => array_slice($m->customerRevenue($from, $to), 0, 5),
        ]);
    }

    public function truckPnlAction()
    {
        [$from, $to] = $this->resolveRange();
        $m = new FleetAnalytics();
        $this->view->setLayout($this->roleLayout());
        $this->view->render('FleetAnalytics/truckPnl', [
            'page_title' => 'Truck P&L',
            'from'       => $from,
            'to'         => $to,
            'preset'     => (string)($_GET['preset'] ?? 'ytd'),
            'rows'       => $m->truckPnl($from, $to),
        ]);
    }

    public function customerRevenueAction()
    {
        [$from, $to] = $this->resolveRange();
        $m = new FleetAnalytics();
        $this->view->setLayout($this->roleLayout());
        $this->view->render('FleetAnalytics/customerRevenue', [
            'page_title' => 'Customer Revenue',
            'from'       => $from,
            'to'         => $to,
            'preset'     => (string)($_GET['preset'] ?? 'ytd'),
            'rows'       => $m->customerRevenue($from, $to),
        ]);
    }

    public function driverScorecardsAction()
    {
        [$from, $to] = $this->resolveRange();
        $m = new FleetAnalytics();
        $this->view->setLayout($this->roleLayout());
        $this->view->render('FleetAnalytics/driverScorecards', [
            'page_title' => 'Driver Scorecards',
            'from'       => $from,
            'to'         => $to,
            'preset'     => (string)($_GET['preset'] ?? 'ytd'),
            'rows'       => $m->driverScorecards($from, $to),
        ]);
    }

    /**
     * Truck deep-drill matrix — every truck × every metric in one sortable
     * table. This is the report a Fleet Manager uses to decide who to
     * retire, who to reward, and which trucks are burning cash.
     */
    public function matrixAction()
    {
        [$from, $to] = $this->resolveRange();
        $m = new FleetAnalytics();
        $this->view->setLayout($this->roleLayout());
        $this->view->render('FleetAnalytics/matrix', [
            'page_title' => 'Truck Matrix',
            'from'       => $from,
            'to'         => $to,
            'preset'     => (string)($_GET['preset'] ?? 'ytd'),
            'rows'       => $m->truckMatrix($from, $to),
        ]);
    }

    /**
     * Single-truck deep-drill: monthly trend, fuel history, trip list,
     * work-order timeline, incident breakdown.
     */
    public function truckAction($truckId = 0)
    {
        [$from, $to] = $this->resolveRange();
        $m = new FleetAnalytics();
        $d = $m->truckDeepDrill((int)$truckId, $from, $to);
        if (empty($d['truck'])) {
            $this->view->setLayout($this->roleLayout());
            $this->view->render('generals/fb_page', [
                'page_title' => 'Not found',
                'fb_msg'     => '<div class="alert alert-danger">Truck not found.</div>',
            ]);
            return;
        }
        $this->view->setLayout($this->roleLayout());
        $this->view->render('FleetAnalytics/truck', [
            'page_title' => 'Truck · ' . $d['truck']['reg_no'],
            'from'       => $from,
            'to'         => $to,
            'preset'     => (string)($_GET['preset'] ?? 'ytd'),
            'd'          => $d,
        ]);
    }

    /** GET params: preset=ytd|this_month|last_month|this_quarter|last_12m|all_time OR from + to */
    private function resolveRange(): array
    {
        $from = trim((string)($_GET['from'] ?? ''));
        $to   = trim((string)($_GET['to']   ?? ''));
        if ($from && $to) return [$from, $to];
        return FleetAnalytics::presetRange((string)($_GET['preset'] ?? 'ytd'));
    }
}
