<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Modal;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Behavior\HasTemplateOverride;
use CpCreation\VitiCore\Shop\InformationBlockModalFactory;
use CpCreation\VitiCore\Shop\Model\InformationBlock;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent('modal:ShopInformation', template: '@components/modal/ShopInformation.html.twig')]
final class ShopInformation
{
    use ComponentToolsTrait;
    use DefaultActionTrait;
    use HasTemplateOverride;

    public ?string $id = 'shop-information-modal';
    #[LiveProp]
    public bool $open = false;

    #[LiveProp]
    public string $title = '';

    #[LiveProp]
    public ?string $description = null;

    #[LiveProp]
    public string $modalTemplate = InformationBlock::MODAL_TEMPLATE_NONE;

    #[LiveProp]
    public ?string $imagePath = null;

    #[LiveProp]
    public ?string $partnerCode = null;

    #[LiveProp]
    public ?string $groupLabel = null;

    #[LiveProp]
    public bool $freeShipping = false;

    #[LiveProp]
    public ?string $freeShippingThresholdLabel = null;

    /**
     * @var list<array{value: string, label: string}>
     */
    #[LiveProp]
    public array $countries = [];

    /**
     * @var list<array{value: string, label: string, equivalency_label: string}>
     */
    #[LiveProp]
    public array $productTypes = [];

    #[LiveProp]
    public bool $showProductTypeFilter = false;

    /**
     * @var list<array<string, mixed>>
     */
    #[LiveProp]
    public array $shippingRows = [];

    #[LiveProp(writable: true)]
    public string $selectedCountryFilter = 'all';

    #[LiveProp(writable: true)]
    public string $selectedTypeFilter = 'all';

    public function __construct(
        private readonly InformationBlockModalFactory $informationBlockModalFactory,
    ) {
    }

    #[LiveListener('shopInformationModal:open')]
    public function openModal(
        #[LiveArg('informationBlockId')] ?InformationBlock $informationBlock = null,
    ): void {
        $this->resetState();

        if (
            !$informationBlock instanceof InformationBlock
            || !$informationBlock->isModalAction()
            || !$informationBlock->hasModalContent()
        ) {
            return;
        }

        $payload = $this->informationBlockModalFactory->build($informationBlock);

        $this->title = mb_trim((string) ($payload['title'] ?? ''));
        $this->description = isset($payload['description']) && \is_string($payload['description'])
            ? $payload['description']
            : null;
        $this->modalTemplate = \is_string($payload['template'] ?? null)
            ? $payload['template']
            : InformationBlock::MODAL_TEMPLATE_NONE;
        $this->imagePath = isset($payload['imagePath']) && \is_string($payload['imagePath'])
            ? $payload['imagePath']
            : null;
        $this->partnerCode = isset($payload['partnerCode']) && \is_string($payload['partnerCode'])
            ? $payload['partnerCode']
            : null;
        $this->groupLabel = isset($payload['groupLabel']) && \is_string($payload['groupLabel'])
            ? $payload['groupLabel']
            : null;
        $this->freeShipping = true === ($payload['freeShipping'] ?? false);
        $this->freeShippingThresholdLabel = isset($payload['freeShippingThresholdLabel']) && \is_string($payload['freeShippingThresholdLabel'])
            ? $payload['freeShippingThresholdLabel']
            : null;
        $this->countries = \is_array($payload['countries'] ?? null) ? array_values($payload['countries']) : [];
        $this->productTypes = \is_array($payload['productTypes'] ?? null) ? array_values($payload['productTypes']) : [];
        $this->showProductTypeFilter = true === ($payload['showProductTypeFilter'] ?? false);
        $this->shippingRows = \is_array($payload['shippingRows'] ?? null) ? array_values($payload['shippingRows']) : [];
        $this->selectedCountryFilter = 'all';
        $this->selectedTypeFilter = 'all';

        $this->open = '' !== $this->title
            || null !== $this->description
            || null !== $this->imagePath
            || InformationBlock::MODAL_TEMPLATE_NONE !== $this->modalTemplate;
    }

    #[LiveAction]
    public function close(): void
    {
        $this->resetState();
    }

    /**
     * @return list<array<string, mixed>>
     */
    public function getFilteredShippingRows(): array
    {
        return array_values(array_filter($this->shippingRows, function (array $row): bool {
            $matchesCountry = 'all' === $this->selectedCountryFilter
                || ($row['country_filter'] ?? null) === $this->selectedCountryFilter;
            $matchesType = 'all' === $this->selectedTypeFilter
                || ($row['type_slug'] ?? null) === $this->selectedTypeFilter;

            return $matchesCountry && $matchesType;
        }));
    }

    private function resetState(): void
    {
        $this->open = false;
        $this->title = '';
        $this->description = null;
        $this->modalTemplate = InformationBlock::MODAL_TEMPLATE_NONE;
        $this->imagePath = null;
        $this->partnerCode = null;
        $this->groupLabel = null;
        $this->freeShipping = false;
        $this->freeShippingThresholdLabel = null;
        $this->countries = [];
        $this->productTypes = [];
        $this->showProductTypeFilter = false;
        $this->shippingRows = [];
        $this->selectedCountryFilter = 'all';
        $this->selectedTypeFilter = 'all';
    }
}
