<?php

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

return new class extends Migration
{
    public function up()
    {
        if (Schema::hasTable('savings_interest_history')) return;
        
        Schema::create('savings_interest_history', function (Blueprint $table) {
            $table->id();
            $table->foreignId('savings_wallet_id')->constrained()->onDelete('cascade');
            $table->decimal('amount', 15, 2);
            $table->decimal('rate', 5, 2);
            $table->date('calculation_date');
            $table->timestamps();
            
            // Shorter composite index name to avoid MySQL identifier length limits
            $table->index(['savings_wallet_id', 'calculation_date'], 'sih_wallet_date_idx');
        });
    }

    public function down()
    {
        Schema::dropIfExists('savings_interest_history');
    }
};

