<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

use Tests\TestCase;

class PropertySchemaApiTest extends TestCase
{
    public function test_endpoint_returns_200_with_data_envelope(): void
    {
        $response = $this->getJson('/api/block-manager/property-schema');
        $response->assertStatus(200);
        $response->assertJsonStructure(['data']);

        $data = $response->json('data');
        $this->assertIsArray($data);
        $this->assertNotEmpty($data, 'Property-schema endpoint should return a non-empty panel list.');
    }

    public function test_endpoint_payload_lists_phase9_panels(): void
    {
        $data = $this->getJson('/api/block-manager/property-schema')->json('data');
        $ids = array_map(fn ($p) => $p['id'] ?? null, $data);

        foreach (['typography', 'box', 'border', 'background'] as $required) {
            $this->assertContains(
                $required,
                $ids,
                "API payload missing required Phase 9 panel '{$required}'."
            );
        }
    }

    public function test_endpoint_typography_panel_includes_color_property(): void
    {
        $data = $this->getJson('/api/block-manager/property-schema')->json('data');

        $typography = null;
        foreach ($data as $panel) {
            if (($panel['id'] ?? null) === 'typography') {
                $typography = $panel;
                break;
            }
        }
        $this->assertNotNull($typography, "API payload must include the 'typography' panel.");

        $colorProperty = null;
        foreach ($typography['properties'] ?? [] as $property) {
            if (($property['name'] ?? null) === 'color') {
                $colorProperty = $property;
                break;
            }
        }
        $this->assertNotNull($colorProperty, "Typography panel must include a 'color' property.");
        $this->assertContains(
            'color',
            $colorProperty['token_types'] ?? [],
            "Typography 'color' must accept color tokens so the picker can bind to /api/block-manager/tokens."
        );
    }
}
