<?php

declare(strict_types=1);

namespace App\Modules\Purchases\Presentation\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Modules\Purchases\Application\Services\SupplierOpenPurchaseInvoicesService;

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

    public function handle(): void
    {
        Auth::requireLogin();
        require_permission('supplier_payments.view');

        $partyId = (int) (Request::get('party_id', Request::get('supplier_id', 0)));
        $paymentId = (int) Request::get('payment_id', 0);

        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($this->service->fetch($partyId, $paymentId), JSON_UNESCAPED_UNICODE);
    }
}
