<?php

namespace Mtc\Plugins\BlockManager\Tests\Unit;

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

class VariantLessGeneratorPanelsTest extends TestCase
{
    private VariantLessGenerator $service;

    /** @var string[] */
    private array $createdUuids = [];

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = app(VariantLessGenerator::class);
    }

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

    public function test_emits_layout_properties_as_css(): void
    {
        $draft = $this->seedDraft('button', 'phase15-layout-' . Str::random(6), [
            'base' => ['default' => ['desktop' => [
                'layout' => [
                    'display' => ['raw' => 'flex'],
                    'gap'     => ['raw' => '16px'],
                ],
            ]]],
        ]);

        $output = $this->service->generate($draft);

        $this->assertStringContainsString('display: flex', $output);
        $this->assertStringContainsString('gap: 16px', $output);
    }

    public function test_emits_box_shadow_and_opacity(): void
    {
        $draft = $this->seedDraft('button', 'phase15-effects-' . Str::random(6), [
            'base' => ['default' => ['desktop' => [
                'effects' => [
                    'box_shadow' => ['raw' => '0 4px 12px rgba(0,0,0,0.1)'],
                    'opacity'    => ['raw' => '0.6'],
                ],
            ]]],
        ]);

        $output = $this->service->generate($draft);

        $this->assertStringContainsString('box-shadow: 0 4px 12px rgba(0,0,0,0.1)', $output);
        $this->assertStringContainsString('opacity: 0.6', $output);
    }

    public function test_wraps_blur_value_as_filter_blur(): void
    {
        $draft = $this->seedDraft('button', 'phase15-blur-' . Str::random(6), [
            'base' => ['default' => ['desktop' => [
                'effects' => ['blur' => ['raw' => '4px']],
            ]]],
        ]);

        $output = $this->service->generate($draft);

        $this->assertStringContainsString(
            'filter: blur(4px)',
            $output,
            "Effects 'blur' raw value must be wrapped as 'filter: blur(<v>);'."
        );
    }

    public function test_appends_custom_css_inside_variant_block(): void
    {
        $slug = 'phase15-custom-' . Str::random(6);
        $draft = $this->seedDraft('button', $slug, [
            'base' => ['default' => ['desktop' => [
                'custom_css' => [
                    'css' => ['raw' => 'transform: rotate(2deg); z-index: 10;'],
                ],
            ]]],
        ]);

        $output = $this->service->generate($draft);

        // Custom CSS appears verbatim
        $this->assertStringContainsString('transform: rotate(2deg); z-index: 10;', $output);

        // AND is inside the variant's outer rule block
        $this->assertSame(
            1,
            preg_match(
                '/\.bm-button--' . preg_quote($slug, '/') . '\s*\{([^}]*transform:\s*rotate\(2deg\);\s*z-index:\s*10;)/s',
                $output
            ),
            "Custom CSS must be appended inside the .bm-button--{slug} { … } block, not outside."
        );
    }

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

        return BlockManagerVariantDraft::create([
            'uuid'            => $uuid,
            'element_type'    => $elementType,
            'variant_slug'    => $variantSlug,
            'label'           => 'Phase 15 Panels Probe',
            'properties_json' => $properties,
        ]);
    }
}
