<?php

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

test('macro_assignments table has expected columns', function () {
    expect(Schema::hasTable('macro_assignments'))->toBeTrue();
    expect(Schema::hasColumns('macro_assignments', ['id', 'part_type', 'macro_id', 'position', 'label_id', 'order', 'created_at', 'updated_at']))->toBeTrue();
});

test('macro_assignments restrictOnDelete prevents deleting referenced macro', function () {
    $macroId = DB::table('macros')->insertGetId(['uuid' => 'AABB-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Test', 'created_at' => now(), 'updated_at' => now()]);
    $labelId = DB::table('labels')->insertGetId(['name' => 'Copyright', 'created_at' => now(), 'updated_at' => now()]);
    DB::table('macro_assignments')->insert(['part_type' => 'song', 'macro_id' => $macroId, 'position' => 'by_label', 'label_id' => $labelId, 'order' => 0, 'created_at' => now(), 'updated_at' => now()]);

    expect(fn () => DB::table('macros')->where('id', $macroId)->delete())
        ->toThrow(\Exception::class);
});

test('macro_assignments allows nullable label_id', function () {
    $macroId = DB::table('macros')->insertGetId(['uuid' => 'CCBB-1111-2222-3333-FFFFFFFFFFFF', 'name' => 'Test2', 'created_at' => now(), 'updated_at' => now()]);
    DB::table('macro_assignments')->insert(['part_type' => 'sermon', 'macro_id' => $macroId, 'position' => 'all_slides', 'label_id' => null, 'order' => 0, 'created_at' => now(), 'updated_at' => now()]);

    expect(DB::table('macro_assignments')->where('macro_id', $macroId)->value('label_id'))->toBeNull();
});
