<?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
    {
        // A challan/bill — always tied to a family (combined family challan).
        Schema::create('fee_challans', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->string('challan_number', 40)->unique();
            $table->foreignId('family_id')->constrained('families')->cascadeOnDelete();
            $table->foreignId('academic_session_id')->nullable()->constrained('academic_sessions')->nullOnDelete();
            $table->string('title');                          // e.g. "June 2026 Fee"
            $table->date('billing_month')->nullable();
            $table->date('bill_date');
            $table->date('due_date');
            $table->decimal('total_amount', 12, 2)->default(0);   // gross (sum of item amounts)
            $table->decimal('discount_amount', 12, 2)->default(0);
            $table->decimal('fine_amount', 12, 2)->default(0);
            $table->decimal('paid_amount', 12, 2)->default(0);
            $table->enum('status', ['unpaid', 'partial', 'paid', 'waived'])->default('unpaid')->index();
            $table->string('notes')->nullable();
            $table->auditColumns();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['branch_id', 'status']);
            $table->index('due_date');
        });

        Schema::create('fee_challan_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('fee_challan_id')->constrained('fee_challans')->cascadeOnDelete();
            $table->foreignId('student_id')->constrained('students')->cascadeOnDelete();
            $table->foreignId('fee_type_id')->constrained('fee_types')->cascadeOnDelete();
            $table->decimal('amount', 10, 2)->default(0);     // gross head amount
            $table->decimal('discount', 10, 2)->default(0);
            $table->decimal('fine', 10, 2)->default(0);
            $table->string('detail')->nullable();             // e.g. "Sibling 25% + Merit 50%"
            $table->timestamps();
        });

        Schema::create('fee_payments', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->foreignId('fee_challan_id')->constrained('fee_challans')->cascadeOnDelete();
            $table->string('receipt_number', 40)->nullable();
            $table->decimal('amount', 12, 2);
            $table->date('payment_date');
            $table->enum('method', ['cash', 'bank', 'easypaisa', 'jazzcash', 'cheque'])->default('cash');
            $table->string('reference')->nullable();
            $table->foreignId('received_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('notes')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('fee_payments');
        Schema::dropIfExists('fee_challan_items');
        Schema::dropIfExists('fee_challans');
    }
};
