<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Repositories\ReportRepository;
use App\Services\DeadlineEnforcementService;

final class TrackingController extends Controller
{
    public function index(): string
    {
        $report = (new ReportRepository())->findByTracking('CG-2026-1258');

        return $this->view('tracking/index', [
            'title' => 'Проследяване',
            'report' => $report,
            'enforcement' => (new DeadlineEnforcementService())->nextActions($report ?? []),
        ]);
    }

    public function show(): string
    {
        if ($response = $this->ensureCsrf()) {
            return $response;
        }

        $tracking = trim((string) ($this->input()['tracking_number'] ?? ''));
        $report = (new ReportRepository())->findByTracking($tracking);

        return $this->json([
            'ok' => (bool) $report,
            'report' => $report,
            'enforcement' => $report ? (new DeadlineEnforcementService())->nextActions($report) : null,
        ]);
    }
}
