<?php

namespace App\Http\Controllers\Owner\WebChannel;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\c_Admin_Session;
use App\Http\Controllers\Owner\WebChannel\Dto\WebChannelUpdateDTO;
use App\Http\Helpers\ResponseBuilder;
use App\Modules\Image\Domain\ImageThumbnailData;
use App\Modules\Image\Infraestructure\BasicConfigImageStorage;
use App\Modules\Leads\WebChannel\Application\Index\IndexWebChannelQuery;
use App\Modules\Leads\WebChannel\Application\Update\UpdateWebChannelQuery;
use App\Modules\Leads\WebChannel\Application\WebChannelResponse;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use Illuminate\Http\Request;

class WebChannelUpdateController extends Controller
{

    private string $domain = 'WebChannel';

    public function __construct(
        private QueryBus $queryBus,
        private LaravelDTOGenerator $dtoGenerator
    ) {}

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


        /** @var WebChannelResponse $response*/
        $response = $this->queryBus->ask(new IndexWebChannelQuery($request->input('Id_WebChannel')));

        // para modificar tambien la imagen
        $thumbnail = new ImageThumbnailData(
            config("var.THUMB_W"),
            config("var.THUMB_H")
        );
        $image = (new BasicConfigImageStorage($request))->storeImage(
            'web_channel' . DIRECTORY_SEPARATOR . 'icon',
            'WebChannel_Icon',
            $thumbnail
        );
        $this->queryBus->ask(new UpdateWebChannelQuery(
            $dto->Id_WebChannel,
            $dto->WebChannel_Name,
            isset($image) ? $image->getUrl() : $response->WebChannel_Icon,
            isset($image) ? $image->getThumbnail() : $response->WebChannel_Icon_Thumb,
            $dto->WebChannel_Version,
            $dto->WebChannel_Status,
            $adminSession['Id_Admin'],
            $dto->Webchannel_Booking_Type,
            $dto->WebChannel_Order,
        ));

        return ResponseBuilder::Response([
            "Response_Status"           => 200,
            "Response_Code"             => 200,
            "Response_Data"             => [],
            "Response_Domain"           => $this->domain,
            "Response_Message"          => 'Tipo de servicio actualizado con éxito',
            "Response_Error_Message"    => '',
            "Response_Error_Reason"     => ''
        ]);
    }
}
