<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Twig;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Marketing\ReviewSolicitationOptOut;
use PHPUnit\Framework\TestCase;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;

final class ReviewSolicitationOptOutComponentTest extends TestCase
{
    public function testComponentExposesAStableLiveAndTemplateOverrideContract(): void
    {
        $reflection = new \ReflectionClass(ReviewSolicitationOptOut::class);
        $componentAttribute = $reflection->getAttributes(AsLiveComponent::class)[0]->newInstance();
        $config = $componentAttribute->serviceConfig();

        self::assertSame('marketing:ReviewSolicitationOptOut', $config['key']);
        self::assertSame('@components/marketing/ReviewSolicitationOptOut.html.twig', $config['template']);
        self::assertTrue($config['live']);
        self::assertNotEmpty($reflection->getProperty('token')->getAttributes(LiveProp::class));
        self::assertNotEmpty($reflection->getProperty('saved')->getAttributes(LiveProp::class));
        self::assertNotEmpty($reflection->getProperty('template')->getAttributes(LiveProp::class));
        self::assertNotEmpty($reflection->getMethod('confirmOptOut')->getAttributes(LiveAction::class));
    }

    public function testDefaultViewKeepsAllLiveActionHooks(): void
    {
        $template = file_get_contents(
            __DIR__ . '/../../src/Bridge/Symfony/Bundle/Resources/views/components/marketing/ReviewSolicitationOptOut.html.twig',
        );

        self::assertIsString($template);
        self::assertStringContainsString('{{ attributes.defaults(', $template);
        self::assertStringContainsString('data-review-opt-out', $template);
        self::assertStringContainsString('data-action="live#action"', $template);
        self::assertStringContainsString('data-live-action-param="confirmOptOut"', $template);
        self::assertStringContainsString('data-loading="addAttribute(disabled)"', $template);
    }
}
