<?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 VariantLessGeneratorBreakpointsTest 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_media_query_for_mobile_overrides(): void
    {
        $draft = $this->seedDraft('button', 'phase13-bp-' . Str::random(6), [
            'base' => [
                'default' => [
                    'desktop' => ['typography' => ['font_size' => ['raw' => '16px']]],
                    'mobile'  => ['typography' => ['font_size' => ['raw' => '14px']]],
                ],
            ],
        ]);

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

        $this->assertMatchesRegularExpression(
            '/@media\s*\([^)]*max-width:\s*@mobile_width\s*\)\s*\{/',
            $output,
            "LESS output must contain '@media (max-width: @mobile_width) {' wrapper."
        );

        // Override property appears in output
        $this->assertStringContainsString('font-size: 14px', $output);

        // And specifically inside the @media block (not just at the outer selector)
        $this->assertSame(
            1,
            preg_match('/@media\s*\([^)]*max-width:\s*@mobile_width\s*\)\s*\{[^}]*font-size:\s*14px[^}]*\}/s', $output),
            "The '@media (max-width: @mobile_width) { ... }' block must contain the override property."
        );
    }

    public function test_emits_media_query_for_tablet_overrides(): void
    {
        $draft = $this->seedDraft('button', 'phase13-bp-' . Str::random(6), [
            'base' => [
                'default' => [
                    'desktop' => ['typography' => ['font_size' => ['raw' => '16px']]],
                    'tablet'  => ['typography' => ['font_size' => ['raw' => '15px']]],
                ],
            ],
        ]);

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

        $this->assertMatchesRegularExpression(
            '/@media\s*\([^)]*max-width:\s*@medium_width\s*\)\s*\{/',
            $output,
            "LESS output must contain '@media (max-width: @medium_width) {' wrapper for tablet."
        );
        $this->assertStringContainsString('font-size: 15px', $output);
    }

    public function test_omits_breakpoint_block_when_no_overrides(): void
    {
        $draft = $this->seedDraft('button', 'phase13-bp-' . Str::random(6), [
            'base' => [
                'default' => ['desktop' => [
                    'typography' => ['font_size' => ['raw' => '16px']],
                ]],
                // No tablet or mobile cells
            ],
        ]);

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

        $this->assertStringNotContainsString(
            '@media',
            $output,
            "No '@media' block should appear when there are no breakpoint overrides."
        );
        $this->assertStringContainsString('font-size: 16px', $output);
    }

    public function test_breakpoint_block_only_contains_overrides_not_base_copies(): void
    {
        $draft = $this->seedDraft('button', 'phase13-bp-' . Str::random(6), [
            'base' => [
                'default' => [
                    'desktop' => [
                        'background' => ['color' => ['raw' => '#ffffff']],
                        'typography' => ['font_size' => ['raw' => '16px']],
                    ],
                    'mobile' => [
                        'typography' => ['font_size' => ['raw' => '14px']],
                    ],
                ],
            ],
        ]);

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

        $this->assertSame(
            1,
            preg_match('/@media\s*\([^)]*max-width:\s*@mobile_width\s*\)\s*\{([^}]*)\}/s', $output, $m),
            "Output must contain a '@media (max-width: @mobile_width) { ... }' block."
        );
        $mobileBody = $m[1];

        $this->assertStringContainsString(
            'font-size: 14px',
            $mobileBody,
            "Mobile override must appear inside the @media block."
        );
        $this->assertStringNotContainsString(
            'background-color',
            $mobileBody,
            "Mobile block must NOT carry copies of base properties (no background-color since mobile didn't override it)."
        );
    }

    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 13 Breakpoints Probe',
            'properties_json' => $properties,
        ]);
    }
}
