<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Tabel Pembayaran: tarif_pembayaran, tagihan, transaksi_pembayaran, detail_pembayaran
     */
    public function up(): void
    {
        // ─── Tarif Pembayaran ────────────────────────────────
        // SPP = bulanan, DPP = sekali bayar per tahun ajaran
        // DPP punya tarif berbeda untuk siswa internal vs external
        Schema::create('tarif_pembayaran', function (Blueprint $table) {
            $table->id();
            $table->enum('jenis', ['SPP', 'DPP']);
            $table->string('nama');              // "SPP Kelas 10", "DPP Internal SMA"
            $table->decimal('nominal', 15, 2);   // jumlah tarif
            $table->foreignId('kelas_id')->nullable()->constrained('kelas')->onDelete('set null');
            $table->foreignId('tahun_ajaran_id')->constrained('tahun_ajaran')->onDelete('cascade');
            $table->enum('kategori_siswa', ['internal', 'external'])->nullable(); // khusus DPP
            $table->boolean('is_bulanan')->default(false); // true untuk SPP
            $table->timestamps();
        });

        // ─── Tagihan ────────────────────────────────────────
        // Tagihan per siswa, bisa SPP (per bulan) atau DPP (sekali)
        Schema::create('tagihan', function (Blueprint $table) {
            $table->id();
            $table->foreignId('siswa_id')->constrained('siswa')->onDelete('cascade');
            $table->foreignId('tarif_pembayaran_id')->constrained('tarif_pembayaran')->onDelete('restrict');
            $table->foreignId('kelas_id')->constrained('kelas')->onDelete('restrict');
            $table->foreignId('tahun_ajaran_id')->constrained('tahun_ajaran')->onDelete('cascade');
            $table->unsignedTinyInteger('bulan')->nullable(); // 1-12 untuk SPP, null untuk DPP
            $table->unsignedSmallInteger('tahun')->nullable(); // tahun kalender
            $table->decimal('jumlah_tagihan', 15, 2);
            $table->decimal('jumlah_terbayar', 15, 2)->default(0);
            $table->enum('status', ['belum_bayar', 'kurang', 'lunas'])->default('belum_bayar');
            $table->date('tanggal_jatuh_tempo')->nullable();
            $table->timestamps();

            // Index untuk query laporan
            $table->index(['siswa_id', 'tahun_ajaran_id', 'status']);
            $table->index(['kelas_id', 'tahun_ajaran_id']);
        });

        // ─── Transaksi Pembayaran ────────────────────────────
        // Satu transaksi bisa membayar beberapa tagihan sekaligus
        Schema::create('transaksi_pembayaran', function (Blueprint $table) {
            $table->id();
            $table->string('kode_transaksi')->unique(); // auto-generated: TRX-20250416-0001
            $table->foreignId('siswa_id')->constrained('siswa')->onDelete('restrict');
            $table->foreignId('user_id')->constrained('users')->onDelete('restrict'); // petugas
            $table->date('tanggal_bayar');
            $table->decimal('total_bayar', 15, 2);
            $table->enum('metode_pembayaran', ['tunai', 'transfer', 'qris']);
            $table->text('keterangan')->nullable();
            $table->timestamps();

            $table->index(['siswa_id', 'tanggal_bayar']);
        });

        // ─── Detail Pembayaran ──────────────────────────────
        // Relasi many-to-many antara transaksi dan tagihan
        Schema::create('detail_pembayaran', function (Blueprint $table) {
            $table->id();
            $table->foreignId('transaksi_pembayaran_id')->constrained('transaksi_pembayaran')->onDelete('cascade');
            $table->foreignId('tagihan_id')->constrained('tagihan')->onDelete('restrict');
            $table->decimal('jumlah_bayar', 15, 2); // jumlah yang dialokasikan ke tagihan ini
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('detail_pembayaran');
        Schema::dropIfExists('transaksi_pembayaran');
        Schema::dropIfExists('tagihan');
        Schema::dropIfExists('tarif_pembayaran');
    }
};
