<?php

namespace App\Modules\CommercialInvoice\Application\Insert;

use App\Http\History\HistoryTables;
use App\Modules\Booking\Domain\IdBooking;
use App\Modules\BookingExtraPassenger\Domain\IdBookingExtraPassenger;
use App\Modules\BookingUpgradePassenger\Domain\IdBookingUpgradePassenger;
use App\Modules\CommercialInvoice\Domain\CommercialInvoice;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceAmountWithIgv;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceAmountWithoutIgv;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceCodeReservation;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceDate;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceDateUpdate;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceIssueDate;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceNumberSerie;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceRepository;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceStatus;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceType;
use App\Modules\CommercialInvoice\Domain\CommercialInvoiceVersion;
use App\Modules\CommercialInvoice\Domain\IdCommercialInvoice;
use App\Modules\History\Application\Insert\InsertHistoryQuery;
use App\Modules\History\Domain\HistoryOperation;
use App\Modules\Shared\Domain\Bus\Event\EventBus;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Domain\Transaction;

final class CommercialInvoiceInserter
{ 
    public function __construct(
        private CommercialInvoiceRepository $repository,
        private EventBus $eventBus,
        private QueryBus $queryBus,
        private Transaction $transaction
    ) {}

    public function __invoke(
        IdCommercialInvoice $id,
        CommercialInvoiceDate $date,
        CommercialInvoiceDateUpdate $dateUpdate,
        CommercialInvoiceIssueDate $issueDate,
        CommercialInvoiceCodeReservation $codeReservation,
        CommercialInvoiceNumberSerie $numberSerie,
        CommercialInvoiceType $type,
        CommercialInvoiceAmountWithoutIgv $amountWithoutIgv,
        CommercialInvoiceAmountWithIgv $amountWithIgv,
        CommercialInvoiceStatus $status,
        CommercialInvoiceVersion $version,
        IdBookingUpgradePassenger $idBookingUpgradePassenger,
        IdBookingExtraPassenger $idBookingExtraPassenger,
        IdBooking $idBooking,
        int $idAdmin
    ) {
        $this->transaction->begin();
        $model = CommercialInvoice::create(
            $id, $date, $dateUpdate, $issueDate, $codeReservation,
            $numberSerie, $type, $amountWithoutIgv, $amountWithIgv,
            $status, $version, $idBookingUpgradePassenger, $idBookingExtraPassenger, $idBooking
        );
        $id = $this->repository->insert($model);
        $model->setId($id);
        $this->queryBus->ask(new InsertHistoryQuery(
            json_encode([]),
            json_encode([$model->toArray()]),
            HistoryOperation::INSERT,
            $id->value(),
            $id->value(),
            HistoryTables::COMMERCIAL_INVOICE,
            $idAdmin,
        ));
        $this->transaction->commit();
        return $id->value();
    }
}
