<?php

namespace App\Http\Controllers\Owner\WebContactMessage;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\WebContactMessage\Dto\WebContactMessageListDTO;
use Illuminate\Http\Request;
use App\Modules\Shared\Domain\ArrayUtils;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Domain\DataUtils;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use App\Modules\WebContactMessage\Application\List\ListWebContactMessageQueryResponse;
use App\Modules\WebContactMessage\Application\ListAll\ListAllWebContactMessageQuery;
use App\Providers\Leads\LeadListReport\LeadListReport;
use App\Providers\StorageProvider;

class WebContactMessageReportListAllController extends Controller
{
    private $domain = 'WebContactMessage';
    public function __construct(
        private QueryBus $queryBus,
        private StorageProvider $storageProvider,
        private LaravelDTOGenerator $dtoGenerator,
    ) {}

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

        /** @var ListWebContactMessageQueryResponse $response */
        $response = $this->queryBus->ask(new ListAllWebContactMessageQuery(
            $dto->DateStart,
            $dto->DateEnd,
            $dto->dateBy,

            $dto->AllStatuses == 2,
            $dto->AllWebPages == 2,
            $dto->AllAdminAssigners == 2,
            $dto->AllAdmins == 2,
            $dto->AllWebChannels == 2,
            $dto->AllPackages == 2,
            $dto->AllTours == 2,

            $dto->Statuses ?? [],
            $dto->IdWebPages ?? [],
            $dto->IdAdminAssigners ?? [],
            $dto->IdAdmins ?? [],
            $dto->IdWebChannels ?? [],
            $dto->IdPackages ?? [],
            $dto->IdTours ?? [],
        ));
        $res = $response->response();

        $keysToMap = [
            'WebChannel_Icon_Thumb',
            'WebChannel_Icon',
            'Admin_ImgProfile',
            'Admin_ImgProfile_Thumb',
            'Admin_Assigner_ImgProfile',
            'Admin_Assigner_ImgProfile_Thumb'
        ];
        foreach ($res as $object) {
            ArrayUtils::each($keysToMap, fn($key) => $this->storageProvider->appendPublicToObject($object, $key));
            $this->storageProvider->appendPublicCountryToObject($object, 'UserCountry_Img_Thumb');
        }

        (new LeadListReport())
            ->exportToExcel(
                ArrayUtils::map($res, [$this, 'richElement']),
            );
    }

    public function richElement($element)
    {
        $details = DataUtils::decodeMysqlJson($element, 'WebContactMessageDetail');
        $length = count($details);
        $added = (object)[];
        foreach ($details as $key => $detail) {
            $added->{'WebContactMessageDetail_' . ($key + 1)} = $detail;
        }
        $added->WebContactMessageDetail = $details;
        $added->WebContactMessageDetailCount = $length;
        return DataUtils::clone($element, $added);
    }
}
