<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

test('macros table has expected columns', function () {
    expect(Schema::hasTable('macros'))->toBeTrue();
    expect(Schema::hasColumns('macros', [
        'id', 'uuid', 'name', 'color', 'trigger_on_startup', 'image_type',
        'action_count', 'hidden_at', 'last_imported_at', 'last_imported_filename',
        'created_at', 'updated_at',
    ]))->toBeTrue();
});

test('macro_collections table has expected columns', function () {
    expect(Schema::hasTable('macro_collections'))->toBeTrue();
    expect(Schema::hasColumns('macro_collections', ['id', 'uuid', 'name', 'last_imported_at', 'created_at', 'updated_at']))->toBeTrue();
});

test('macro_collection_macros junction has expected columns', function () {
    expect(Schema::hasTable('macro_collection_macros'))->toBeTrue();
    expect(Schema::hasColumns('macro_collection_macros', ['id', 'macro_collection_id', 'macro_id', 'order', 'created_at', 'updated_at']))->toBeTrue();
});

test('deleting a collection cascades to junction rows', function () {
    $collId = DB::table('macro_collections')->insertGetId(['uuid' => 'COLL-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Test', 'created_at' => now(), 'updated_at' => now()]);
    $macroId = DB::table('macros')->insertGetId(['uuid' => 'AAAA-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Test', 'created_at' => now(), 'updated_at' => now()]);
    DB::table('macro_collection_macros')->insert(['macro_collection_id' => $collId, 'macro_id' => $macroId, 'order' => 0, 'created_at' => now(), 'updated_at' => now()]);

    DB::table('macro_collections')->where('id', $collId)->delete();

    expect(DB::table('macro_collection_macros')->where('macro_collection_id', $collId)->count())->toBe(0);
});

test('macros uuid is unique', function () {
    DB::table('macros')->insert(['uuid' => 'SAME-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'First', 'created_at' => now(), 'updated_at' => now()]);
    expect(fn () => DB::table('macros')->insert(['uuid' => 'SAME-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Second', 'created_at' => now(), 'updated_at' => now()]))
        ->toThrow(\Exception::class);
});
