<?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
    {
        Schema::create('dusun', function (Blueprint $table) {
            $table->id();
            $table->string('nama');
            $table->string('alamat')->nullable();
            $table->string('email')->nullable();
            $table->string('phone')->nullable();
            $table->enum('status', ['aktif', 'tidak_aktif'])->default('aktif');
            $table->timestamps();
        });

        Schema::create('penduduk', function (Blueprint $table) {
            $table->id();
            $table->string('nik')->unique(); // NIK biasanya unik
            $table->string('nama');
            $table->string('tempat_lahir');
            $table->date('tanggal_lahir'); // Gunakan tipe data date
            $table->enum('jenis_kelamin', ['laki', 'perempuan']);
            $table->string('alamat')->nullable();
            $table->foreignId('dusun_id')->constrained('dusun')->onDelete('cascade'); // Relasi ke tabel dusun
            $table->string('rt');
            $table->string('rw');
            $table->enum('agama', [
                'Islam',
                'Kristen Protestan',
                'Katolik',
                'Hindu',
                'Buddha',
                'Konghucu'
            ])->nullable();
            $table->enum('perkawinan', ['sudah', 'belum', 'cerai_mati', 'cerai_hidup']);
            $table->string('pekerjaan');
            $table->enum('status_penduduk', ['ada', 'meninggal', 'pindah'])->default('ada'); // Ubah nama kolom status agar tidak konflik
            $table->timestamps();
        });
        
        Schema::create('kartu_keluarga', function (Blueprint $table) {
            $table->id();
            $table->string('nomor_kk')->unique(); // Nomor KK biasanya unik
            $table->foreignId('kepala_keluarga_id')->constrained('penduduk')->onDelete('cascade'); // Relasi ke tabel penduduk
            $table->foreignId('dusun_id')->constrained('dusun')->onDelete('cascade'); // Relasi ke tabel dusun
            $table->string('rt');
            $table->string('rw');
            $table->string('kecamatan');
            $table->string('kabupaten');
            $table->string('provinsi');
            $table->timestamps();
        });

        Schema::create('anggota_kartu_keluarga', function (Blueprint $table) {
            $table->id();
            $table->foreignId('kartu_keluarga_id')->constrained('kartu_keluarga')->onDelete('cascade'); // Relasi ke tabel kartu_keluarga
            $table->foreignId('penduduk_id')->constrained('penduduk')->onDelete('cascade'); // Relasi ke tabel penduduk
            $table->string('hubungan');
            $table->timestamps();
        });

        Schema::create('kelahiran', function (Blueprint $table) {
            $table->id();
            $table->string('nama_bayi'); // Nama bayi
            $table->string('tempat_lahir');
            $table->date('tanggal_lahir'); // Gunakan tipe data date
            $table->enum('jenis_kelamin', ['laki', 'perempuan']);
            $table->enum('agama', [
                'Islam',
                'Kristen Protestan',
                'Katolik',
                'Hindu',
                'Buddha',
                'Konghucu'
            ])->nullable();
            $table->foreignId('kartu_keluarga_id')->constrained('kartu_keluarga')->onDelete('cascade'); // Relasi ke tabel kartu_keluarga
            $table->timestamps();
        });

        Schema::create('kematian', function (Blueprint $table) {
            $table->id();
            $table->foreignId('penduduk_id')->constrained('penduduk')->onDelete('cascade'); // Relasi ke tabel penduduk
            $table->date('tanggal_kematian'); // Gunakan tipe data date
            $table->string('penyebab');
            $table->timestamps();
        });

        Schema::create('pendatang', function (Blueprint $table) {
            $table->id();
            $table->string('nik')->unique();
            $table->string('nama');
            $table->string('tempat_lahir');
            $table->date('tanggal_lahir'); // Gunakan tipe data date
            $table->enum('jenis_kelamin', ['laki', 'perempuan']);
            $table->string('alamat_asal')->nullable(); // Alamat asal
            $table->string('alamat_tujuan')->nullable(); // Alamat tujuan
            $table->foreignId('dusun_id_tujuan')->nullable()->constrained('dusun')->onDelete('cascade'); // Dusun tujuan, bisa null jika alamat umum
            $table->enum('agama', [
                'Islam',
                'Kristen Protestan',
                'Katolik',
                'Hindu',
                'Buddha',
                'Konghucu'
            ])->nullable();
            $table->enum('perkawinan', ['sudah', 'belum', 'cerai_mati', 'cerai_hidup']);
            $table->string('pekerjaan');
            $table->date('tanggal_datang'); // Tanggal kedatangan
            $table->timestamps();
        });

        Schema::create('pindah', function (Blueprint $table) {
            $table->id();
            $table->foreignId('penduduk_id')->constrained('penduduk')->onDelete('cascade'); // Relasi ke tabel penduduk
            $table->date('tanggal_pindah'); // Gunakan tipe data date
            $table->string('alasan');
            $table->string('alamat_tujuan'); // Alamat tujuan pindah
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('pindah');
        Schema::dropIfExists('pendatang');
        Schema::dropIfExists('kematian');
        Schema::dropIfExists('kelahiran');
        Schema::dropIfExists('anggota_kartu_keluarga');
        Schema::dropIfExists('kartu_keluarga');
        Schema::dropIfExists('penduduk');
        Schema::dropIfExists('dusun');
    }
};