<?php

namespace App\Http\Controllers\Owner\WebTypeClient;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\c_Admin_Session;
use App\Http\Controllers\Owner\WebTypeClient\Dto\WebTypeClientInsertDTO;
use App\Http\Helpers\ResponseBuilder;
use App\Modules\Image\Domain\ImageThumbnailData;
use App\Modules\Image\Infraestructure\BasicConfigImageStorage;
use App\Modules\Leads\WebTypeClient\Application\Insert\InsertWebTypeClientQuery;
use App\Modules\Leads\WebTypeClient\Application\Insert\InsertWebTypeClientResponse;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use Illuminate\Http\Request;
use App\Modules\HtlCamp\Domain\HtlCampIcon;
use App\Modules\HtlCamp\Domain\HtlCampIconThumb;

class WebTypeClientInsertController extends Controller
{

    private string $domain = 'WebTypeClient';

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

    public function __invoke(Request $request)
    {
        /** @var WebTypeClientInsertDTO $dto */
        $dto = $this->dtoGenerator->generate($request->all(), $this->domain, WebTypeClientInsertDTO::class);
        $adminSession = c_Admin_Session::check($request);
        // $idAdmin = 
        $thumbnail = new ImageThumbnailData(
            config("var.THUMB_W"),
            config("var.THUMB_H")
        );
        $image = (new BasicConfigImageStorage($request))->storeImage(
            'web_channel' . DIRECTORY_SEPARATOR . 'icon',
            'WebTypeClient_Icon',
            $thumbnail
        );
        /** @var InsertWebTypeClientResponse $response */
        $response = $this->queryBus->ask(new InsertWebTypeClientQuery(
            date('Y-m-d H:i:s'),
            date('Y-m-d H:i:s'),
            $dto->WebTypeClient_Name,
            isset($image) ? $image->getUrl() : '',
            isset($image) ? $image->getThumbnail() : '',
            $dto->WebTypeClient_Status,
            $adminSession['Id_Admin'],
            
        ));

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