<?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
    {
        // Hapus tabel surat_pindah yang lama jika ada
        Schema::dropIfExists('surat_pindah');

        // Buat tabel surat_pindah baru dengan skema yang dimodifikasi
        Schema::create('surat_pindah', function (Blueprint $table) {
            $table->id();

            // Kolom data penduduk yang pindah (input manual static)
            $table->string('nama_pemohon');
            $table->string('nik_pemohon')->unique();
            $table->string('tempat_lahir_pemohon')->nullable();
            $table->date('tanggal_lahir_pemohon')->nullable();
            $table->enum('jenis_kelamin_pemohon', ['Laki-laki', 'Perempuan'])->nullable();
            $table->string('agama_pemohon')->nullable();
            $table->string('pekerjaan_pemohon')->nullable();
            $table->string('alamat_asal_pemohon');
            $table->string('rt_asal_pemohon')->nullable();
            $table->string('rw_asal_pemohon')->nullable();

            $table->string('nomor_surat')->unique()->nullable();
            $table->string('alamat_tujuan');
            $table->string('rt_tujuan')->nullable();
            $table->string('rw_tujuan')->nullable();
            $table->string('dusun_tujuan')->nullable();
            $table->string('desa_tujuan');
            $table->string('kecamatan_tujuan');
            $table->string('kabupaten_tujuan');
            $table->string('provinsi_tujuan');
            $table->date('tanggal_pindah');
            $table->text('alasan_pindah');

            $table->enum('status', ['menunggu_acc_admin_desa', 'disetujui', 'ditolak'])->default('menunggu_acc_admin_desa');
            $table->string('file_path')->nullable();
            $table->text('catatan_penolakan')->nullable();
            $table->foreignId('disetujui_oleh')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('tanggal_disetujui')->nullable();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Hapus tabel surat_pindah jika rollback
        Schema::dropIfExists('surat_pindah');

        // Jika Anda ingin mengembalikan tabel lama saat rollback,
        // Anda perlu membuat skema tabel lama di sini.
        // Contoh:
        // Schema::create('surat_pindah', function (Blueprint $table) {
        //     $table->id();
        //     $table->foreignId('penduduk_id')->constrained('penduduk')->cascadeOnDelete();
        //     $table->string('nomor_surat')->unique()->nullable();
        //     // ... kolom lama lainnya
        //     $table->timestamps();
        // });
    }
};