<?php

namespace App\Http\Controllers\Owner\Report\Booking;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\Report\Booking\Dto\ReportSalesBySellerDTO;
use Illuminate\Http\Request;
use App\Http\Helpers\ResponseBuilder;
use App\Modules\Report\Booking\Application\SaleBySellerMonthly\ReportSaleBySellerMonthlyQuery;
use App\Modules\Report\Booking\Application\SaleBySellerMonthly\ReportSaleBySellerMonthlyResponse;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Domain\Transaction;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;

class ReportSalesBySellerMonthlyController extends Controller
{
    private $domain;

    public function __construct(
        private QueryBus $queryBus,
        private Transaction $transaction,
        private LaravelDTOGenerator $dtoGenerator
    ) {
        $this->domain = "ReportBooking";
    }

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

        // $this->transaction->begin();

        /** @var ReportSaleBySellerMonthlyResponse $response */
        $response = $this->queryBus->ask(new ReportSaleBySellerMonthlyQuery(
            $dto->DateStart,
            $dto->DateEnd,
            $dto->Type,
            $dto->ShowBy,
            $dto->GroupBy,
            $dto->OnlySold,
            $dto->Admins,
            $dto->Tours ?? [],
            $dto->Packages ?? [],
        ));

        // $this->transaction->commit();
        return ResponseBuilder::Response(ResponseBuilder::buildSelectResponse($response->data(), '', $this->domain));
    }
}
