<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

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

class PreviewApiBreakpointTest 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_breakpoint_mobile_applies_mobile_overrides(): void
    {
        $draft = $this->seedDraft('button');

        $response = $this->postJson("/api/block-manager/variants/{$draft->id}/preview", [
            'breakpoint' => 'mobile',
            'properties_json' => [
                'base' => [
                    'default' => [
                        'desktop' => ['typography' => ['font_size' => ['raw' => '16px']]],
                        'mobile'  => ['typography' => ['font_size' => ['raw' => '14px']]],
                    ],
                ],
            ],
        ]);
        $response->assertStatus(200);

        $css = $response->json('data.css');
        $this->assertStringContainsString(
            'font-size: 14px',
            $css,
            "Preview with breakpoint=mobile must apply the mobile override."
        );
    }

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

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

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

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

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

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

        // Desktop background passes through (not overridden by mobile)
        $this->assertStringContainsString(
            'background-color: #ffffff',
            $css,
            "Breakpoint preview must still render desktop properties not touched by the override."
        );

        // Mobile font-size wins
        $this->assertStringContainsString(
            'font-size: 14px',
            $css,
            "Mobile 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'    => 'phase13-preview-' . Str::random(8),
            'label'           => 'Phase 13 Preview Breakpoint Probe',
            'properties_json' => null,
        ]);
    }
}
