<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

use Tests\TestCase;

class ElementCatalogueApiTest extends TestCase
{
    public function test_endpoint_returns_200(): void
    {
        $this->getJson('/api/block-manager/elements')->assertStatus(200);
    }

    public function test_endpoint_payload_is_keyed_by_data(): void
    {
        $response = $this->getJson('/api/block-manager/elements');

        $response->assertJsonStructure(['data']);
        $this->assertIsArray($response->json('data'));
    }

    public function test_endpoint_returns_all_starter_elements(): void
    {
        $data = $this->getJson('/api/block-manager/elements')->json('data');
        $types = array_map(fn ($e) => $e['type'], $data);

        foreach (['heading', 'paragraph', 'button'] as $expected) {
            $this->assertContains(
                $expected,
                $types,
                "Element catalogue endpoint did not include '{$expected}'."
            );
        }
    }

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

        foreach ($data as $element) {
            foreach (['type', 'label', 'category', 'icon', 'inputs', 'variants', 'default_variant'] as $key) {
                $this->assertArrayHasKey(
                    $key,
                    $element,
                    "API payload for element '{$element['type']}' is missing '{$key}'."
                );
            }
            $this->assertIsArray($element['inputs']);
            $this->assertIsArray($element['variants']);
        }
    }
}
