<?php

namespace App\Http\Controllers\Owner\BookingTourPassenger;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\BookingTourPassenger\Dto\BookingTourPassengerPrintDTO;
use App\Http\History\HistoryData;
use App\Http\History\HistoryTables;
use App\Modules\BookingTourPassenger\Application\Print\PrintBookingTourPassengerQuery;
use App\Modules\BookingTourPassenger\Application\Print\PrintBookingTourPassengerResponse;
use App\Modules\Shared\Domain\Bus\Command\CommandBus;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use App\Modules\Template\TourTemplate\Application\ListByTour\ListByTourTourTemplateQuery;
use App\Modules\Template\TourTemplate\Application\ListByTour\ListByTourTourTemplateResponses;
use App\Providers\Pdf\BookingTourPassengerTemplate\BookingTourPassengerTemplatePdf;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

final class BookingTourPassengerPrintController extends Controller
{
    private $domain;
    private HistoryData $history;

    public function __construct(
        private CommandBus $commandBus,
        private QueryBus $queryBus,
        private BookingTourPassengerTemplatePdf $bookingTourPassengerTemplatePdf,
        private LaravelDTOGenerator $dtoGenerator
    ) {
        $this->domain = "BookingTourPassenger";
        $this->history = new HistoryData(HistoryTables::BOOKING_TOUR_PASSENGER);
    }

    private function findBooking($Id)
    {
        $found = DB::select(
            'Call sp_booking_tour_passenger_index(?, ?)',
            [$Id, config('var.PATH_PUBLIC') . config('var.USER_COUNTRY_THUMB')]
        );

        return count($found) ? $found[0]->Id_Booking : null;
    }

    public function __invoke(Request $request, $Id)
    {
        /** @var BookingTourPassengerPrintDTO $dto */
        $dto = $this->dtoGenerator->generate($request->all(), $this->domain, BookingTourPassengerPrintDTO::class);

        $this->history->obtainOld($Id);

        /** @var PrintBookingTourPassengerResponse $update */
        $update = $this->queryBus->ask(new PrintBookingTourPassengerQuery(
            $Id
        ));

        $btp = $update->response()[0];

        /** @var ListByTourTourTemplateResponses $templateResponse */
        $templateResponse = $this->queryBus->ask(new ListByTourTourTemplateQuery($btp->Id_Tour));

        if ($request->input('Type') === 'Id_TourTemplate') {
            $template = $templateResponse->tourTemplate;
        }
        if ($request->input('Type') === 'Id_TourTemplate_Blank') {
            $template = $templateResponse->tourTemplateBlank;
        }
        if ($template === null) {
            return 'El tour no tiene template';
        }

        $this->history->obtainNew($Id);

        $this->history->update(
            $this->findBooking($Id),
            $this->history->oldValue,
            $this->history->newValueCache,
            $request
        );

        $this->bookingTourPassengerTemplatePdf->run(
            $template->TourTemplate_Pdf,
            $template,
            $btp
        );
    }
}
