<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Session;
use App\Services\BonLivraisonService;
use App\Services\DocumentEngine;
use App\Services\DocumentCycleService;
use Throwable;

final class BonLivraisonController extends Controller
{
    public function index(): void
    {
        $this->view('bons_livraison/index', [
            'title' => 'Bons de livraison',
            'bons' => (new BonLivraisonService())->list(),
        ]);
    }

    public function createFromDevis(): void
    {
        try {
            $id = (new BonLivraisonService())->createFromDevis((int) Request::post('devis_id', 0));
            (new DocumentCycleService())->log('DEVIS', (int)Request::post('devis_id', 0), 'TRANSFORME_BL', 'Transformé en bon de livraison', 'BL', $id);
            (new DocumentCycleService())->log('BL', $id, 'CREATION', 'Créé depuis un devis', 'DEVIS', (int)Request::post('devis_id', 0));
            Session::flash('success', 'Bon de livraison créé et stock décrémenté.');
            $this->redirect('bons_livraison_show&id=' . $id);
        } catch (Throwable $exception) {
            Session::flash('error', $exception->getMessage());
            $this->redirect('devis');
        }
    }

    public function pdf(): void
    {
        try {
            $data = (new BonLivraisonService())->findWithLines((int) Request::get('id', 0));
            (new DocumentEngine())->streamBonLivraison($data['bon'], $data['lignes']);
        } catch (Throwable $exception) {
            Session::flash('error', $exception->getMessage());
            $this->redirect('bons_livraison');
        }
    }

    public function show(): void
    {
        try {
            $data = (new BonLivraisonService())->findWithLines((int) Request::get('id', 0));
            $this->view('bons_livraison/show', [
                'title' => 'Bon de livraison ' . $data['bon']['numero'],
                'bon' => $data['bon'],
                'lignes' => $data['lignes'],
                'cycleLinks' => ($cycle = new DocumentCycleService())->links('BL', (int)$data['bon']['id']),
                'cycleHistory' => $cycle->forDocument('BL', (int)$data['bon']['id']),
                'cycleStatus' => $cycle->status('BL', $data['bon'], $cycle->links('BL', (int)$data['bon']['id'])),
            ]);
        } catch (Throwable $exception) {
            Session::flash('error', $exception->getMessage());
            $this->redirect('bons_livraison');
        }
    }

    public function markDelivered(): void
    {
        try {
            $id = (int) Request::post('id', 0);
            (new BonLivraisonService())->markDelivered($id, (string) Request::post('receptionnaire', ''));
            (new DocumentCycleService())->log('BL', $id, 'LIVRE', 'Bon marqué comme livré');
            Session::flash('success', 'Bon marqué comme livré.');
            $this->redirect('bons_livraison_show&id=' . $id);
        } catch (Throwable $exception) {
            Session::flash('error', $exception->getMessage());
            $this->redirect('bons_livraison');
        }
    }
}
