<?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\Collection;

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\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;

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

    #[LiveProp(writable: false)]
    public Product $product;

    #[LiveAction]
    public function showAwardModal(#[LiveArg('awardId')] ?Award $award = null): void
    {
        if (!$award instanceof Award || !$this->product->getAwards()->contains($award)) {
            return;
        }

        $this->emit('awardModal:open', [
            'productId' => $this->product->getId()->toString(),
            'awardId' => $award->getId()->toString(),
        ], 'modal:SimpleModalAward');
    }
}
