<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Hapus duplikat terlebih dahulu (simpan yang pertama)
        DB::statement('
            DELETE h1 FROM holidays h1
            INNER JOIN holidays h2
            WHERE h1.id > h2.id
            AND h1.date = h2.date
            AND (h1.company_id = h2.company_id OR (h1.company_id IS NULL AND h2.company_id IS NULL))
        ');

        // Tambah unique constraint
        Schema::table('holidays', function (Blueprint $table) {
            $table->unique(['date', 'company_id'], 'holidays_date_company_unique');
        });
    }

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