<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('tagihan', function (Blueprint $table) {
            // Drop existing index if it exists – ignore exception if not present
            try {
                $table->dropUnique('tagihan_unique_composite');
            } catch (\Exception $e) {
                // index may not exist, safe to ignore
            }

            // Re‑create composite unique index (MySQL treats NULL as distinct, which is fine)
            $table->unique([
                'siswa_id',
                'kelas_id',
                'tarif_pembayaran_id',
                'tahun_ajaran_id',
                'bulan',
            ], 'tagihan_unique_composite');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('tagihan', function (Blueprint $table) {
            $table->dropUnique('tagihan_unique_composite');
        });
    }
};
