<?php

class CarrierController extends Controller
{
    private Carrier $carriers;

    public function __construct()
    {
        $this->carriers = new Carrier();
    }

    public function index(): void
    {
        Auth::requireRole(['owner','admin','dispatcher','accountant']);
        $search = trim((string) ($_GET['search'] ?? ''));
        $carriers = $this->carriers->all($search ?: null);
        $this->view('carriers/index', compact('carriers','search'));
        clear_old();
    }

    public function create(): void
    {
        Auth::requireRole(['owner','admin','dispatcher']);
        $carrier = null;
        $this->view('carriers/create', compact('carrier'));
    }

    public function store(): void
    {
        Auth::requireRole(['owner','admin','dispatcher']);
        Csrf::verify();
        if (trim((string) ($_POST['company_name'] ?? '')) === '') {
            set_old($_POST);
            $this->flash('danger', 'Carrier company name is required.');
            $this->redirect('/carriers/create');
        }
        $id = $this->carriers->create($_POST);
        log_activity('carrier_created', 'Created carrier ' . trim((string) $_POST['company_name']));
        $this->flash('success', 'Carrier created successfully.');
        $this->redirect('/carriers/show?id=' . $id);
    }

    public function show(): void
    {
        Auth::requireRole(['owner','admin','dispatcher','accountant']);
        $carrier = $this->carriers->find((int) ($_GET['id'] ?? 0));
        if (!$carrier) {
            $this->flash('danger', 'Carrier not found.');
            $this->redirect('/carriers');
        }
        $loads = $this->carriers->loads((int) $carrier['id']);
        $equipment = $this->carriers->equipment((int) $carrier['id']);
        $documents = (new Document())->forRelation('carrier', (int) $carrier['id']);
        $this->view('carriers/show', compact('carrier','loads','equipment','documents'));
    }

    public function edit(): void
    {
        Auth::requireRole(['owner','admin','dispatcher']);
        $carrier = $this->carriers->find((int) ($_GET['id'] ?? 0));
        if (!$carrier) {
            $this->flash('danger', 'Carrier not found.');
            $this->redirect('/carriers');
        }
        $this->view('carriers/edit', compact('carrier'));
    }

    public function update(): void
    {
        Auth::requireRole(['owner','admin','dispatcher']);
        Csrf::verify();
        $id = (int) ($_POST['id'] ?? 0);
        if (!$id || trim((string) ($_POST['company_name'] ?? '')) === '') {
            set_old($_POST);
            $this->flash('danger', 'Carrier company name is required.');
            $this->redirect('/carriers/edit?id=' . $id);
        }
        $this->carriers->update($id, $_POST);
        log_activity('carrier_updated', 'Updated carrier ' . trim((string) $_POST['company_name']));
        $this->flash('success', 'Carrier updated successfully.');
        $this->redirect('/carriers/show?id=' . $id);
    }

    public function delete(): void
    {
        Auth::requireRole(['owner','admin']);
        Csrf::verify();
        $this->carriers->delete((int) ($_POST['id'] ?? 0));
        log_activity('carrier_deleted', 'Deleted a carrier');
        $this->flash('success', 'Carrier deleted successfully.');
        $this->redirect('/carriers');
    }
}
