<?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('jenis_pinjaman', function (Blueprint $table) {
            $table->id();
            $table->string('nama_pinjaman');
            $table->string('kode_pinjaman', 10)->unique();
            $table->decimal('bunga_persen', 5, 2)->default(0);
            $table->text('keterangan')->nullable();
            $table->timestamps();
        });

        Schema::create('pinjaman', function (Blueprint $table) {
            $table->id();
            $table->string('no_transaksi', 50)->unique();
            $table->date('tanggal');
            $table->foreignId('id_member')->constrained('members')->onDelete('cascade');
            $table->foreignId('jenis_pinjaman_id')->constrained('jenis_pinjaman');
            $table->decimal('jumlah_pinjaman', 15, 2);
            $table->integer('tenor')->comment('dalam bulan');
            $table->decimal('bunga_persen', 5, 2);
            $table->decimal('angsuran_per_bulan', 15, 2);
            $table->decimal('total_bunga', 15, 2);
            $table->decimal('total_bayar', 15, 2);
            $table->boolean('is_lunas')->default(false);
            $table->date('tanggal_lunas')->nullable();
            $table->text('keterangan')->nullable();
            $table->unsignedBigInteger('create_user')->nullable();
            $table->timestamp('create_time')->useCurrent();
            $table->unsignedBigInteger('update_user')->nullable();
            $table->timestamp('update_time')->nullable();

            $table->index(['id_member', 'is_lunas']);
        });

        Schema::create('angsuran', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pinjaman_id')->constrained('pinjaman')->onDelete('cascade');
            $table->integer('angsuran_ke');
            $table->date('tanggal_jatuh_tempo');
            $table->decimal('jumlah_angsuran', 15, 2);
            $table->decimal('jumlah_bayar', 15, 2)->default(0);
            $table->date('tanggal_bayar')->nullable();
            $table->enum('status', ['belum_bayar', 'sebagian', 'lunas'])->default('belum_bayar');
            $table->timestamps();

            $table->index(['pinjaman_id', 'angsuran_ke']);
        });

        Schema::create('pengajuan', function (Blueprint $table) {
            $table->id();
            $table->string('nik', 50);
            $table->foreignId('jenis_pinjaman_id')->nullable()->constrained('jenis_pinjaman');
            $table->foreignId('jenis_simpanan_id')->nullable()->constrained('jenis_simpanan');
            $table->decimal('jumlah', 15, 2);
            $table->integer('tenor')->nullable()->comment('untuk pinjaman');
            $table->string('tujuan');
            $table->text('keterangan')->nullable();
            $table->integer('is_proses')->default(0)->comment('0=pending, 1=approved, 2=rejected, 3=banding');
            $table->date('tanggal');
            $table->timestamps();

            $table->foreign('nik')->references('nik')->on('members')->onDelete('cascade');
            $table->index(['nik', 'is_proses']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('pengajuan');
        Schema::dropIfExists('angsuran');
        Schema::dropIfExists('pinjaman');
        Schema::dropIfExists('jenis_pinjaman');
    }
};
