<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('kode_akses_kantor', function (Blueprint $table) {
            $table->id();
            $table->foreignId('unit_kerja_id')->constrained('unit_kerja')->cascadeOnDelete();
            $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();
            $table->string('kode', 32)->unique();
            $table->unsignedInteger('batas_penggunaan');
            $table->unsignedInteger('jumlah_digunakan')->default(0);
            $table->dateTime('kedaluwarsa_pada');
            $table->boolean('aktif')->default(true);
            $table->timestamps();

            $table->index(['unit_kerja_id', 'aktif', 'kedaluwarsa_pada']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('kode_akses_kantor');
    }
};
