<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

use Tests\TestCase;

/**
 * Verifies that the /api/block-manager/preview endpoint correctly renders
 * container elements with nested children via BlockPreviewService.
 *
 * Containers are declared with "accepts_children": true in their element.json
 * and use a single "default" variant. Layout/styling is no longer variant-based;
 * it is driven per-instance via `style_props` → a generated `.bm-ci-…` class.
 * BlockPreviewService renders the children recursively and stamps the container
 * div with both `bm-container` and the per-instance class.
 */
class ContainerPreviewApiTest extends TestCase
{
    public function test_container_renders_with_base_and_instance_class(): void
    {
        $response = $this->postJson('/api/block-manager/preview', [
            'structure_json' => [
                'elements' => [
                    [
                        'instance_id'  => 'ctr-1',
                        'element_type' => 'container',
                        'variant'      => 'default',
                        'children'     => [],
                    ],
                ],
            ],
        ]);

        $response->assertStatus(200);
        $response->assertJsonStructure(['data' => ['html', 'css', 'css_hrefs']]);

        $html = $response->json('data.html');
        $this->assertStringContainsString(
            'data-bm-instance="ctr-1"',
            $html,
            'Container must be wrapped with its data-bm-instance attribute.'
        );
        $this->assertStringContainsString(
            'bm-container',
            $html,
            'Container must render the base bm-container class.'
        );
        $this->assertStringContainsString(
            'bm-ci-ctr-1',
            $html,
            'Container div must carry its stable per-instance class.'
        );
    }

    public function test_container_nests_heading_and_button_children(): void
    {
        $response = $this->postJson('/api/block-manager/preview', [
            'structure_json' => [
                'elements' => [
                    [
                        'instance_id'  => 'ctr-2',
                        'element_type' => 'container',
                        'variant'      => 'default',
                        'children'     => [
                            [
                                'instance_id'  => 'child-heading',
                                'element_type' => 'heading',
                                'variant'      => 'default',
                            ],
                            [
                                'instance_id'  => 'child-button',
                                'element_type' => 'button',
                                'variant'      => 'primary',
                            ],
                        ],
                    ],
                ],
            ],
        ]);

        $response->assertStatus(200);

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

        $this->assertStringContainsString('data-bm-instance="ctr-2"', $html);
        $this->assertStringContainsString('data-bm-instance="child-heading"', $html);
        $this->assertStringContainsString('data-bm-instance="child-button"', $html);
        $this->assertStringContainsString(
            'data-bm-parent="ctr-2"',
            $html,
            'Child elements must carry data-bm-parent pointing to the container instance.'
        );

        $containerPos = strpos($html, 'data-bm-instance="ctr-2"');
        $headingPos   = strpos($html, 'data-bm-instance="child-heading"');
        $buttonPos    = strpos($html, 'data-bm-instance="child-button"');
        $this->assertGreaterThan($containerPos, $headingPos);
        $this->assertGreaterThan($containerPos, $buttonPos);
    }

    public function test_container_children_render_actual_element_html(): void
    {
        $response = $this->postJson('/api/block-manager/preview', [
            'structure_json' => [
                'elements' => [
                    [
                        'instance_id'  => 'ctr-3',
                        'element_type' => 'container',
                        'variant'      => 'default',
                        'children'     => [
                            [
                                'instance_id'  => 'inner-btn',
                                'element_type' => 'button',
                                'variant'      => 'primary',
                            ],
                        ],
                    ],
                ],
            ],
        ]);

        $response->assertStatus(200);
        $this->assertStringContainsString(
            'bm-button--primary',
            $response->json('data.html'),
            'Child button must render its variant CSS class inside the container.'
        );
    }

    public function test_empty_container_renders_drop_target_placeholder(): void
    {
        $response = $this->postJson('/api/block-manager/preview', [
            'structure_json' => [
                'elements' => [
                    [
                        'instance_id'  => 'ctr-empty',
                        'element_type' => 'container',
                        'variant'      => 'default',
                        'children'     => [],
                    ],
                ],
            ],
        ]);

        $response->assertStatus(200);
        $this->assertStringContainsString(
            'bm-container__empty',
            $response->json('data.html'),
            'Empty container must render the drop-target placeholder element.'
        );
    }

    public function test_style_props_produce_scoped_css_in_response(): void
    {
        $response = $this->postJson('/api/block-manager/preview', [
            'structure_json' => [
                'elements' => [
                    [
                        'instance_id'  => 'ctr-styled',
                        'element_type' => 'container',
                        'variant'      => 'default',
                        'children'     => [],
                        'style_props'  => [
                            'base' => [
                                'default' => [
                                    'desktop' => [
                                        'layout' => [
                                            'display' => ['raw' => 'flex'],
                                            'gap'     => ['raw' => '24px'],
                                        ],
                                    ],
                                    'mobile' => [
                                        'layout' => [
                                            'flex_direction' => ['raw' => 'column'],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ]);

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

        $this->assertStringContainsString('.bm-ci-ctr-styled', $css, 'CSS must be scoped to the per-instance class.');
        $this->assertStringContainsString('display: flex', $css);
        $this->assertStringContainsString('gap: 24px', $css);
        $this->assertStringContainsString('@media (max-width:', $css, 'Breakpoint values must emit a media query.');
        $this->assertStringContainsString('flex-direction: column', $css);
    }

    public function test_multiple_containers_at_root_level_render_independently(): void
    {
        $response = $this->postJson('/api/block-manager/preview', [
            'structure_json' => [
                'elements' => [
                    [
                        'instance_id'  => 'ctr-a',
                        'element_type' => 'container',
                        'variant'      => 'default',
                        'children'     => [
                            ['instance_id' => 'ch-a1', 'element_type' => 'heading', 'variant' => 'default'],
                        ],
                    ],
                    [
                        'instance_id'  => 'ctr-b',
                        'element_type' => 'container',
                        'variant'      => 'default',
                        'children'     => [
                            ['instance_id' => 'ch-b1', 'element_type' => 'button', 'variant' => 'primary'],
                        ],
                    ],
                ],
            ],
        ]);

        $response->assertStatus(200);
        $html = $response->json('data.html');

        foreach (['ctr-a', 'ctr-b', 'ch-a1', 'ch-b1'] as $id) {
            $this->assertStringContainsString("data-bm-instance=\"{$id}\"", $html);
        }
        $this->assertStringContainsString('data-bm-parent="ctr-a"', $html);
        $this->assertStringContainsString('data-bm-parent="ctr-b"', $html);
    }
}
