<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

use Illuminate\Support\Str;
use Mtc\Plugins\BlockManager\Models\BlockManagerVariantDraft;
use Tests\TestCase;

class PreviewApiStateTest extends TestCase
{
    /** @var string[] */
    private array $createdUuids = [];

    protected function tearDown(): void
    {
        if (!empty($this->createdUuids)) {
            BlockManagerVariantDraft::whereIn('uuid', $this->createdUuids)->delete();
        }
        parent::tearDown();
    }

    public function test_preview_with_hover_state_applies_hover_overrides(): void
    {
        $draft = $this->seedDraft('button');

        $response = $this->postJson("/api/block-manager/variants/{$draft->id}/preview", [
            'state' => 'hover',
            'properties_json' => [
                'base' => [
                    'default' => ['desktop' => [
                        'background' => ['color' => ['raw' => '#ffffff']],
                    ]],
                    'hover' => ['desktop' => [
                        'background' => ['color' => ['raw' => '#abcdef']],
                    ]],
                ],
            ],
        ]);
        $response->assertStatus(200);

        $css = $response->json('data.css');
        $this->assertStringContainsString(
            'background-color: #abcdef',
            $css,
            "Preview with state=hover must apply the hover override."
        );
    }

    public function test_preview_default_state_when_state_parameter_omitted(): void
    {
        $draft = $this->seedDraft('button');

        // No state parameter — preview must ignore the hover cell.
        $response = $this->postJson("/api/block-manager/variants/{$draft->id}/preview", [
            'properties_json' => [
                'base' => [
                    'default' => ['desktop' => [
                        'typography' => ['color' => ['raw' => '#000000']],
                    ]],
                    'hover' => ['desktop' => [
                        'typography' => ['color' => ['raw' => '#ff0000']],
                    ]],
                ],
            ],
        ]);
        $response->assertStatus(200);

        $css = $response->json('data.css');
        $this->assertStringNotContainsString(
            '#ff0000',
            $css,
            "Without a 'state' parameter, the hover cell must be ignored."
        );
        $this->assertStringContainsString(
            'color: #000000',
            $css,
            "Without a 'state' parameter, the default cell is rendered."
        );
    }

    public function test_preview_with_state_falls_back_to_base_for_non_overridden_properties(): void
    {
        $draft = $this->seedDraft('button');

        // Hover overrides font_size only; background.color should fall back to base.
        $response = $this->postJson("/api/block-manager/variants/{$draft->id}/preview", [
            'state' => 'hover',
            'properties_json' => [
                'base' => [
                    'default' => ['desktop' => [
                        'background' => ['color' => ['raw' => '#ffffff']],
                        'typography' => ['font_size' => ['raw' => '16px']],
                    ]],
                    'hover' => ['desktop' => [
                        'typography' => ['font_size' => ['raw' => '20px']],
                    ]],
                ],
            ],
        ]);
        $response->assertStatus(200);

        $css = $response->json('data.css');

        // Base background passes through (not overridden by hover)
        $this->assertStringContainsString(
            'background-color: #ffffff',
            $css,
            "Hover state preview must still render base properties not touched by the override."
        );

        // Hover font-size wins
        $this->assertStringContainsString(
            'font-size: 20px',
            $css,
            "Hover override on font-size must appear in the preview CSS."
        );
    }

    private function seedDraft(string $elementType): BlockManagerVariantDraft
    {
        $uuid = (string) Str::uuid();
        $this->createdUuids[] = $uuid;

        return BlockManagerVariantDraft::create([
            'uuid'            => $uuid,
            'element_type'    => $elementType,
            'variant_slug'    => 'phase12-preview-' . Str::random(8),
            'label'           => 'Phase 12 Preview State Probe',
            'properties_json' => null,
        ]);
    }
}
