<?php

namespace Mtc\Plugins\BlockManager\Tests\Feature;

use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Tests\TestCase;

class MigrationsTest extends TestCase
{
    private array $blockUuids = [];
    private array $draftUuids = [];

    protected function tearDown(): void
    {
        if (!empty($this->blockUuids) && Schema::hasTable('block_manager_blocks')) {
            DB::table('block_manager_blocks')->whereIn('uuid', $this->blockUuids)->delete();
        }
        if (!empty($this->draftUuids) && Schema::hasTable('block_manager_variant_drafts')) {
            DB::table('block_manager_variant_drafts')->whereIn('uuid', $this->draftUuids)->delete();
        }
        parent::tearDown();
    }

    public function test_block_manager_blocks_table_exists(): void
    {
        $this->assertTrue(
            Schema::hasTable('block_manager_blocks'),
            'Migration {date}_create_block_manager_blocks has not been run.'
        );

        $this->assertTrue(Schema::hasColumns('block_manager_blocks', [
            'id',
            'uuid',
            'slug',
            'name',
            'description',
            'category_id',
            'icon',
            'structure_json',
            'last_shipped_at',
            'shipped_block_registry_id',
            'created_at',
            'updated_at',
        ]));
    }

    public function test_block_manager_blocks_slug_is_unique(): void
    {
        $slug = 'phase1-uniq-' . Str::random(8);
        $uuidA = (string) Str::uuid();
        $uuidB = (string) Str::uuid();
        $this->blockUuids[] = $uuidA;
        $this->blockUuids[] = $uuidB;

        DB::table('block_manager_blocks')->insert([
            'uuid' => $uuidA,
            'slug' => $slug,
            'name' => 'Phase 1 Uniqueness Probe A',
            'structure_json' => json_encode(['elements' => []]),
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $this->expectException(QueryException::class);

        DB::table('block_manager_blocks')->insert([
            'uuid' => $uuidB,
            'slug' => $slug, // duplicate
            'name' => 'Phase 1 Uniqueness Probe B',
            'structure_json' => json_encode(['elements' => []]),
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    public function test_block_manager_variant_drafts_table_exists(): void
    {
        $this->assertTrue(
            Schema::hasTable('block_manager_variant_drafts'),
            'Migration {date}_create_block_manager_variant_drafts has not been run.'
        );

        $this->assertTrue(Schema::hasColumns('block_manager_variant_drafts', [
            'id',
            'uuid',
            'element_type',
            'variant_slug',
            'label',
            'properties_json',
            'last_published_at',
            'published_twig_path',
            'published_less_path',
            'created_at',
            'updated_at',
        ]));
    }

    public function test_block_manager_variant_drafts_slug_is_unique_per_element(): void
    {
        $variantSlug = 'phase1-uniq-' . Str::random(8);
        $uuidA = (string) Str::uuid();
        $uuidB = (string) Str::uuid();
        $this->draftUuids[] = $uuidA;
        $this->draftUuids[] = $uuidB;

        DB::table('block_manager_variant_drafts')->insert([
            'uuid' => $uuidA,
            'element_type' => 'button',
            'variant_slug' => $variantSlug,
            'label' => 'Phase 1 Probe A',
            'properties_json' => json_encode(['base' => []]),
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $this->expectException(QueryException::class);

        DB::table('block_manager_variant_drafts')->insert([
            'uuid' => $uuidB,
            'element_type' => 'button',
            'variant_slug' => $variantSlug, // duplicate within element_type
            'label' => 'Phase 1 Probe B',
            'properties_json' => json_encode(['base' => []]),
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
