<?php

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

return new class extends Migration
{
    /**
     * Historique des rendements/intérêts calculés
     */
    public function up(): void
    {
        if (Schema::hasTable('investment_interests')) {
            return;
        }
        
        Schema::create('investment_interests', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_investment_id')->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            
            $table->decimal('amount', 15, 2); // Montant intérêt/rendement
            $table->decimal('rate', 5, 2); // % appliqué
            
            $table->enum('period', ['daily', 'weekly', 'monthly', 'yearly']);
            $table->timestamp('calculated_at');
            $table->timestamp('credited_at')->nullable(); // Quand transféré au portefeuille
            
            $table->enum('status', ['pending', 'credited', 'cancelled'])->default('pending');
            
            $table->timestamps();
            
            $table->index(['user_investment_id', 'calculated_at']);
            $table->index('status');
        });
    }

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