<?php

declare(strict_types=1);

namespace App\Support\Print;

use App\Support\Print\Contracts\DberpDocumentPrintPresenter;
use App\Support\Print\Documents\OtherStockInPrintPresenter;
use App\Support\Print\Documents\OtherStockOutPrintPresenter;
use App\Support\Print\Documents\PaymentOrderPrintPresenter;
use App\Support\Print\Documents\PurchaseOrderPrintPresenter;
use App\Support\Print\Documents\PurchaseReceiptPrintPresenter;
use App\Support\Print\Documents\PurchaseReturnPrintPresenter;
use App\Support\Print\Documents\ReceiptOrderPrintPresenter;
use App\Support\Print\Documents\SalesOrderPrintPresenter;
use App\Support\Print\Documents\SalesOutboundPrintPresenter;
use App\Support\Print\Documents\SalesReturnPrintPresenter;
use App\Support\Print\Documents\StocktakeOrderPrintPresenter;
use App\Support\Print\Documents\TransferOrderPrintPresenter;
use Illuminate\Contracts\Container\Container;

final class DberpDocumentPrintRegistry
{
    /**
     * @var array<string, class-string<DberpDocumentPrintPresenter>>
     */
    private const PRESENTERS = [
        PurchaseOrderPrintPresenter::DOCUMENT_KEY => PurchaseOrderPrintPresenter::class,
        PurchaseReceiptPrintPresenter::DOCUMENT_KEY => PurchaseReceiptPrintPresenter::class,
        SalesOrderPrintPresenter::DOCUMENT_KEY => SalesOrderPrintPresenter::class,
        SalesOutboundPrintPresenter::DOCUMENT_KEY => SalesOutboundPrintPresenter::class,
        TransferOrderPrintPresenter::DOCUMENT_KEY => TransferOrderPrintPresenter::class,
        StocktakeOrderPrintPresenter::DOCUMENT_KEY => StocktakeOrderPrintPresenter::class,
        ReceiptOrderPrintPresenter::DOCUMENT_KEY => ReceiptOrderPrintPresenter::class,
        PaymentOrderPrintPresenter::DOCUMENT_KEY => PaymentOrderPrintPresenter::class,
        PurchaseReturnPrintPresenter::DOCUMENT_KEY => PurchaseReturnPrintPresenter::class,
        SalesReturnPrintPresenter::DOCUMENT_KEY => SalesReturnPrintPresenter::class,
        OtherStockInPrintPresenter::DOCUMENT_KEY => OtherStockInPrintPresenter::class,
        OtherStockOutPrintPresenter::DOCUMENT_KEY => OtherStockOutPrintPresenter::class,
    ];

    public function __construct(
        private readonly Container $container,
    ) {}

    public function resolve(string $documentKey): DberpDocumentPrintPresenter
    {
        $class = self::PRESENTERS[$documentKey] ?? null;

        if ($class === null) {
            abort(404);
        }

        return $this->container->make($class);
    }

    public function supports(string $documentKey): bool
    {
        return isset(self::PRESENTERS[$documentKey]);
    }
}
