<?php

declare(strict_types=1);

namespace App\Modules\Sales\Presentation\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Core\Response;
use App\Modules\Sales\Application\Services\SalesViewService;

final class SalesViewController
{
    public function __construct(private ?SalesViewService $service = null)
    {
        $this->service ??= new SalesViewService(db());
    }

    public function show(): void
    {
        Auth::requireLogin();
        $id = (int) Request::get('id', 0);
        $data = $this->service->load($id);
        if (!$data['inv']) {
            Response::flash('danger', 'الفاتورة غير موجودة');
            Response::redirect('/sales');
        }

        Response::view('pages/sales/view', $data + [
            'page_title' => 'عرض الفاتورة',
            'breadcrumbs' => [
                ['label'=>'لوحة التحكم','url'=>'/admin/dashboard'],
                ['label'=>'عرض الفاتورة'],
            ],
        ]);
    }
}
