<?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
    {
        // 1. TABEL Masjid
        Schema::create('Masjid', function (Blueprint $table) {
            $table->id('id_masjid');
            $table->string('nama_masjid');
            $table->string('alamat_masjid');
            $table->integer('tahun_aktif');
            $table->timestamps();
        });

        // 2. TABEL Wilayah
        Schema::create('Wilayah', function (Blueprint $table) {
            $table->id('id_wilayah');
            $table->string('rt');
            $table->string('rw');
            $table->string('nama_wilayah');
            $table->unsignedBigInteger('id_masjid');
            $table->timestamps();
        });

        // 3. TABEL DataKK
        Schema::create('DataKK', function (Blueprint $table) {
            $table->id('id_kk');
            $table->string('nik_kepala_kk')->unique();
            $table->unsignedBigInteger('id_masjid');
            $table->unsignedBigInteger('id_wilayah');
            $table->string('nama_kepala_kk');
            $table->text('alamat_rumah');
            $table->integer('jumlah_tanggungan');
            $table->timestamps();
        });

        // 4. TABEL DataMustahik
        Schema::create('DataMustahik', function (Blueprint $table) {
            $table->id('id_mustahik');
            $table->date('tanggal_pengajuan');
            $table->string('nik_kepala_kk');
            $table->string('nama_mustahik');
            $table->date('tgl_lahir');
            $table->string('no_hp');
            $table->enum('status_hubungan', ['Kepala Keluarga', 'Istri', 'Anak', 'Lainnya']);
            $table->string('pekerjaan');
            $table->decimal('pendapatan_bulanan', 12, 2);
            $table->enum('status_kepemilikan_rumah', ['Milik Sendiri', 'Sewa', 'Menumpang', 'Lainnya']);
            $table->enum('status_kondisi_warga', ['Mampu', 'Tidak Mampu']);
            $table->enum('kategori_asnaf', ['Fakir', 'Miskin', 'Gharim', 'Ibnu Sabil']);
            $table->enum('status', ['Pending', 'Disetujui', 'Ditolak'])->default('Pending');
            $table->text('catatan_amil')->nullable();
            $table->unsignedBigInteger('id_masjid');
            $table->timestamps();
        });

        // 5. TABEL MustahikHistory
        Schema::create('MustahikHistory', function (Blueprint $table) {
            $table->id('id_history');
            $table->unsignedBigInteger('id_mustahik');
            $table->integer('tahun');
            $table->string('nama_mustahik');
            $table->string('no_hp');
            $table->date('tgl_lahir');
            $table->string('pekerjaan_saat_itu');
            $table->decimal('pendapatan_bulanan', 12, 2);
            $table->string('status_kepemilikan_rumah');
            $table->string('status_kondisi_warga');
            $table->string('kategori_asnaf');
            $table->string('status_approval');
            $table->text('catatan_amil')->nullable();
            $table->unsignedBigInteger('diupdate_oleh');
            $table->date('tanggal_update');
            $table->timestamps();
        });

        // 6. TABEL PenerimaanZakat
        Schema::create('PenerimaanZakat', function (Blueprint $table) {
            $table->id('id_penerimaan');
            $table->unsignedBigInteger('id_masjid');
            $table->string('nama_penyetor');
            $table->string('atas_name');
            $table->integer('jumlah_jiwa');
            $table->enum('jenis_zakat', ['uang', 'beras']);
            $table->decimal('total_bayar', 12, 2);
            $table->enum('metode_pembayaran', ['cash', 'qris', 'transfer']);
            $table->date('tanggal_terima');
            $table->unsignedBigInteger('petugas_id');
            $table->string('keterangan')->nullable();
            $table->timestamps();
        });

        // 7. TABEL DistribusiZakat
        Schema::create('DistribusiZakat', function (Blueprint $table) {
            $table->id('id_distribusi');
            $table->unsignedBigInteger('id_mustahik');
            $table->unsignedBigInteger('id_masjid');
            $table->date('tanggal_distribusi');
            $table->decimal('jumlah_distribusi', 12, 2);
            $table->enum('status_terima', ['sudah', 'belum']);
            $table->unsignedBigInteger('petugas_id');
            $table->string('keterangan')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('DistribusiZakat');
        Schema::dropIfExists('PenerimaanZakat');
        Schema::dropIfExists('MustahikHistory');
        Schema::dropIfExists('DataMustahik');
        Schema::dropIfExists('DataKK');
        Schema::dropIfExists('users');
        Schema::dropIfExists('Wilayah');
        Schema::dropIfExists('Masjid');
    }
};