<?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\Award\Model\Award;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Behavior\HasTemplateOverride;
use CpCreation\VitiCore\Product\Model\Product;
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\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

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

    public ?string $id = 'award-modal';
    public bool $open = false;
    public string $title = '';
    public string $description = '';
    public ?string $imagePath = null;

    #[LiveListener('awardModal:open')]
    public function openAwardModal(
        #[LiveArg('productId')] Product $product,
        #[LiveArg('awardId')] Award $award,
    ): void {
        if (!$product->getAwards()->contains($award)) {
            return;
        }

        $this->title = $award->getTitle() ?? '';
        $this->description = $award->getDescription() ?? '';
        $this->imagePath = $award->getImage()?->getPath();
        $this->open = true;
    }

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