<?php

declare(strict_types=1);

namespace App\Modules\Sales\Presentation\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Modules\Sales\Application\Services\QuotationPriceService;

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

    public function handle(): void
    {
        Auth::requireLogin();
        header('Content-Type: application/json; charset=utf-8');
        $this->service->ensureSchema();

        echo json_encode(
            $this->service->resolve(
                (int) Request::get('item_id', 0),
                (int) Request::get('party_id', 0),
                trim((string) Request::get('invoice_date', date('Y-m-d'))),
            ),
            JSON_UNESCAPED_UNICODE
        );
    }
}
