<?php

namespace Mtc\Plugins\BlockManager\Tests\Unit;

use Mtc\Plugins\BlockManager\Services\PropertySchemaService;
use Tests\TestCase;

class PropertySchemaServicePhase15Test extends TestCase
{
    private PropertySchemaService $service;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = app(PropertySchemaService::class);
    }

    public function test_panels_now_includes_layout_effects_custom_css(): void
    {
        $ids = array_map(fn ($p) => $p['id'], $this->service->panels());

        foreach (['typography', 'box', 'border', 'background', 'layout', 'effects', 'custom_css'] as $required) {
            $this->assertContains(
                $required,
                $ids,
                "Phase 15 requires the '{$required}' panel to be present in PropertySchemaService::panels()."
            );
        }
    }

    public function test_layout_panel_anchor_properties(): void
    {
        $layout = $this->findPanel('layout');
        $this->assertNotNull($layout, "Layout panel must be present.");

        $byName = $this->indexByName($layout['properties']);

        $this->assertArrayHasKey('display', $byName, "Layout panel must include a 'display' property.");
        $this->assertSame('select', $byName['display']['input']);
        $this->assertIsArray($byName['display']['options'] ?? null);
        $this->assertNotEmpty(
            $byName['display']['options'],
            "Layout 'display' must declare a non-empty options[] (e.g. block / flex / grid)."
        );

        $this->assertArrayHasKey('gap', $byName, "Layout panel must include a 'gap' property.");
    }

    public function test_effects_panel_anchor_properties(): void
    {
        $effects = $this->findPanel('effects');
        $this->assertNotNull($effects, "Effects panel must be present.");

        $names = array_map(fn ($p) => $p['name'], $effects['properties']);

        foreach (['box_shadow', 'opacity', 'blur'] as $required) {
            $this->assertContains(
                $required,
                $names,
                "Effects panel must include a '{$required}' property."
            );
        }
    }

    public function test_custom_css_panel_has_css_textarea(): void
    {
        $panel = $this->findPanel('custom_css');
        $this->assertNotNull($panel, "Custom CSS panel must be present.");

        $byName = $this->indexByName($panel['properties']);
        $this->assertArrayHasKey('css', $byName, "Custom CSS panel must declare a 'css' property.");
        $this->assertSame(
            'textarea',
            $byName['css']['input'],
            "The custom_css.css property must declare input=textarea so the SPA renders a textarea field."
        );
    }

    private function findPanel(string $id): ?array
    {
        foreach ($this->service->panels() as $panel) {
            if (($panel['id'] ?? null) === $id) {
                return $panel;
            }
        }
        return null;
    }

    private function indexByName(array $properties): array
    {
        $out = [];
        foreach ($properties as $property) {
            $out[$property['name']] = $property;
        }
        return $out;
    }
}
