<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

use Illuminate\Support\Str;
use Mtc\Modules\BlockRegistry\Models\BlockRegistryField;
use Mtc\Plugins\BlockManager\Models\BlockManagerBlock;
use Tests\TestCase;

class CustomFieldValidationApiTest extends TestCase
{
    /** @var string[] */
    private array $createdUuids = [];

    protected function tearDown(): void
    {
        if (!empty($this->createdUuids)) {
            BlockManagerBlock::whereIn('uuid', $this->createdUuids)->delete();
        }
        parent::tearDown();
    }

    public function test_store_rejects_invalid_custom_field_type(): void
    {
        $response = $this->postJson('/api/block-manager', [
            'name'           => 'Phase6 Reject ' . Str::random(4),
            'structure_json' => [
                'elements' => [],
                'fields_panel' => [
                    'custom' => [
                        ['name' => 'borked', 'type' => 'not-a-real-type', 'required' => false],
                    ],
                ],
            ],
        ]);

        $response->assertStatus(422);

        // No row should have been persisted
        $payload = $response->json('data');
        if (is_array($payload) && isset($payload['uuid'])) {
            $this->createdUuids[] = $payload['uuid'];
        }
    }

    public function test_update_rejects_invalid_custom_field_type(): void
    {
        $block = $this->seedBlock();

        $response = $this->putJson("/api/block-manager/{$block->id}", [
            'structure_json' => [
                'elements' => [],
                'fields_panel' => [
                    'custom' => [
                        ['name' => 'borked', 'type' => 'made-up-thing', 'required' => false],
                    ],
                ],
            ],
        ]);

        $response->assertStatus(422);
    }

    public function test_update_accepts_each_block_registry_field_type(): void
    {
        $block = $this->seedBlock();

        foreach (array_keys(BlockRegistryField::FIELD_TYPES) as $type) {
            $response = $this->putJson("/api/block-manager/{$block->id}", [
                'structure_json' => [
                    'elements' => [],
                    'fields_panel' => [
                        'custom' => [
                            ['name' => 'probe_' . $type, 'label' => 'Probe', 'type' => $type, 'required' => false],
                        ],
                    ],
                ],
            ]);

            $this->assertSame(
                200,
                $response->status(),
                "Custom field type '{$type}' should be accepted (FIELD_TYPES lists it). Got status {$response->status()}."
            );
        }
    }

    private function seedBlock(): BlockManagerBlock
    {
        $uuid = (string) Str::uuid();
        $this->createdUuids[] = $uuid;

        return BlockManagerBlock::create([
            'uuid'           => $uuid,
            'slug'           => 'phase6-cfv-' . Str::random(8),
            'name'           => 'Phase6 Custom-field Validation Probe',
            'structure_json' => ['elements' => []],
        ]);
    }
}
