<?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_wallets')) return;
        
        Schema::create('savings_wallets', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->decimal('balance', 15, 2)->default(0);
            $table->decimal('total_deposited', 15, 2)->default(0);
            $table->decimal('total_interest_earned', 15, 2)->default(0);
            $table->boolean('is_locked')->default(false);
            $table->integer('lock_duration_months')->nullable();
            $table->timestamp('locked_at')->nullable();
            $table->timestamp('unlock_at')->nullable();
            $table->decimal('interest_rate', 5, 2)->default(0);
            $table->timestamps();
            
            $table->index(['user_id', 'is_locked']);
            $table->index('unlock_at');
        });
    }

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

