<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('contribution_fees', function (Blueprint $table) {
            $table->id();
            $table->foreignId('membership_category_id')->constrained()->onDelete('cascade');
            $table->string('name', 191);
            $table->text('description')->nullable();
            $table->decimal('amount', 15, 2);
            $table->enum('currency', ['USD', 'SSP', 'KES', 'UGX'])->default('SSP');
            $table->enum('cycle', ['monthly', 'quarterly', 'biannual', 'annual'])->default('monthly');
            $table->boolean('is_optional')->default(false);
            $table->boolean('is_active')->default(true);
            $table->integer('due_day')->default(1); // Day of month for due date
            $table->date('effective_from')->nullable();
            $table->date('effective_until')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
            
            $table->index('membership_category_id');
            $table->index('is_active');
            $table->index('cycle');
        });

        // Penalty configuration
        Schema::create('contribution_penalties', function (Blueprint $table) {
            $table->id();
            $table->foreignId('membership_category_id')->constrained()->onDelete('cascade');
            $table->string('name', 191);
            $table->text('description')->nullable();
            $table->enum('type', ['fixed', 'percentage'])->default('fixed');
            $table->decimal('amount', 15, 2)->default(0);
            $table->decimal('percentage', 5, 2)->default(0);
            $table->integer('grace_period_days')->default(0);
            $table->boolean('is_active')->default(true);
            $table->boolean('apply_recurring')->default(false); // Apply penalty every period
            $table->integer('max_penalty_days')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
            
            $table->index('membership_category_id');
            $table->index('is_active');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('contribution_penalties');
        Schema::dropIfExists('contribution_fees');
    }
};
