<?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
    {
        // Admin-configurable sibling discount ladder (1st child 0%, 2nd 25%, ...).
        Schema::create('sibling_discount_rules', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->unsignedTinyInteger('child_order');     // 1 = eldest
            $table->enum('type', ['percentage', 'fixed'])->default('percentage');
            $table->decimal('value', 10, 2)->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

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

        // Per-student concessions / scholarships / free-student support.
        Schema::create('fee_concessions', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->foreignId('student_id')->constrained('students')->cascadeOnDelete();
            $table->foreignId('fee_type_id')->nullable()->constrained('fee_types')->nullOnDelete(); // null = all heads
            $table->enum('scholarship_type', ['merit', 'orphan', 'staff_child', 'need_based', 'custom'])->default('custom');
            $table->enum('type', ['percentage', 'fixed'])->default('percentage');
            $table->decimal('value', 10, 2)->default(0);
            $table->string('reason')->nullable();
            $table->boolean('is_active')->default(true)->index();
            $table->auditColumns();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('fee_concessions');
        Schema::dropIfExists('sibling_discount_rules');
    }
};
