<?php

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

return new class extends Migration
{
    /**
     * Jalankan migrasi untuk membuat tabel program dan persyaratannya.
     */
    public function up(): void
    {
        // 1. Tabel Utama Program Kegiatan Bantuan (Ditambahkan batas rentang waktu!)
        Schema::create('kegiatan', function (Blueprint $table) {
            $table->id();
            $table->string('nama_kegiatan', 150);
            $table->char('tahun_anggaran', 4);
            $table->text('deskripsi')->nullable();
            $table->dateTime('pendaftaran_mulai')->nullable();   // Waktu pembukaan usulan
            $table->dateTime('pendaftaran_selesai')->nullable(); // Waktu penutupan usulan
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // 2. Tabel Item Barang
        Schema::create('barang_kegiatan', function (Blueprint $table) {
            $table->id();
            $table->foreignId('kegiatan_id')->constrained('kegiatan')->onDelete('cascade');
            $table->string('nama_barang', 150);
            $table->decimal('volume_per_hektar', 8, 2)->default(1.00);
            $table->string('satuan', 50);
            $table->timestamps();
        });

        // 3. Tabel Persyaratan yang Harus Diupload oleh Kabupaten
        Schema::create('syarat_kegiatan', function (Blueprint $table) {
            $table->id();
            $table->foreignId('kegiatan_id')->constrained('kegiatan')->onDelete('cascade');
            $table->string('nama_syarat', 150);
            $table->enum('tipe_file', ['pdf', 'image', 'polygon']);
            $table->boolean('is_required')->default(true);
            $table->decimal('bobot_nilai', 5, 2);
            $table->timestamps();
        });

        // 4. Tabel Alokasi Kuota Luas Maksimal Kabupaten
        Schema::create('kuota_kabupaten', function (Blueprint $table) {
            $table->id();
            $table->foreignId('kegiatan_id')->constrained('kegiatan')->onDelete('cascade');
            $table->foreignId('regency_id')->constrained('regencies')->onDelete('cascade');
            $table->decimal('kuota_luas', 10, 2);
            $table->timestamps();
        });
    }

    /**
     * Batalkan migrasi.
     */
    public function down(): void
    {
        Schema::dropIfExists('kuota_kabupaten');
        Schema::dropIfExists('syarat_kegiatan');
        Schema::dropIfExists('barang_kegiatan');
        Schema::dropIfExists('kegiatan');
    }
};