<?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
    {
        // Catalogue of fee heads (Monthly, Admission, Transport, Lab, ...).
        Schema::create('fee_types', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->string('name', 100);
            $table->string('code', 30)->nullable();
            $table->enum('frequency', ['monthly', 'annual', 'one_time'])->default('monthly');
            $table->decimal('default_amount', 10, 2)->default(0);
            $table->boolean('discountable')->default(true); // sibling/scholarship can apply
            $table->boolean('is_active')->default(true)->index();
            $table->auditColumns();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['branch_id', 'name']);
        });

        // Per-class amount for a fee head within a session.
        Schema::create('fee_structures', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->foreignId('academic_session_id')->constrained('academic_sessions')->cascadeOnDelete();
            $table->foreignId('class_id')->constrained('classes')->cascadeOnDelete();
            $table->foreignId('fee_type_id')->constrained('fee_types')->cascadeOnDelete();
            $table->decimal('amount', 10, 2)->default(0);
            $table->auditColumns();
            $table->timestamps();

            $table->unique(['academic_session_id', 'class_id', 'fee_type_id'], 'fee_structure_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('fee_structures');
        Schema::dropIfExists('fee_types');
    }
};
