<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

use Illuminate\Support\Facades\Route;
use Tests\TestCase;

/**
 * The Designer surface is now the "Elements" tab of the unified shell, served
 * from /admin/block-manager/elements. The legacy /designer URL is kept as an
 * alias that lands on the same Elements tab.
 */
class DesignerAdminMountTest extends TestCase
{
    public function test_elements_route_is_registered(): void
    {
        $found = collect(Route::getRoutes())->contains(
            fn ($route) => $route->uri() === 'admin/block-manager/elements'
        );

        $this->assertTrue(
            $found,
            'Elements admin route /admin/block-manager/elements is not registered — '
            . 'web.php should mount AdminController@elements.'
        );
    }

    public function test_elements_index_returns_200(): void
    {
        $this->withoutMiddleware()
            ->get('/admin/block-manager/elements')
            ->assertStatus(200);
    }

    public function test_elements_index_emits_elements_initial_tab(): void
    {
        $body = $this->withoutMiddleware()
            ->get('/admin/block-manager/elements')
            ->getContent();

        $this->assertStringContainsString(
            'id="block-manager-app"',
            $body,
            'Elements shell must emit the SPA mount div.'
        );
        $this->assertStringContainsString(
            'data-initial-tab="elements"',
            $body,
            'Elements shell must set data-initial-tab="elements" so the shell opens on the Elements tab.'
        );
    }

    public function test_elements_show_route_with_id_includes_draft_id_data_attr(): void
    {
        $response = $this->withoutMiddleware()->get('/admin/block-manager/elements/42');
        $response->assertStatus(200);

        $body = $response->getContent();
        $this->assertStringContainsString(
            'data-draft-id="42"',
            $body,
            'Elements per-draft route must pass the draft id via the data-draft-id attribute on the mount div.'
        );
    }

    public function test_legacy_designer_alias_still_serves_elements_tab(): void
    {
        $body = $this->withoutMiddleware()
            ->get('/admin/block-manager/designer')
            ->assertStatus(200)
            ->getContent();

        $this->assertStringContainsString(
            'data-initial-tab="elements"',
            $body,
            'Legacy /designer URL must still resolve to the Elements tab for back-compat.'
        );
    }
}
