<?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('users', function (Blueprint $table) {
            $table->id();
            $table->string('uid', 50)->index()->nullable();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('wallet_address')->nullable()->unique()->index();
            $table->string('wallet_type', 20)->default('metamask')->index();
            $table->string('password');
            $table->enum('gender', ['male', 'female', 'other'])->nullable();
            $table->enum('role', ['user', 'admin', 'moderator'])->default('user');
            $table->enum('status', ['active', 'suspended', 'pending', 'banned'])->default('active');
            $table->enum('kyc_status', ['pending', 'reviewing', 'approved', 'rejected'])->default('pending');
            $table->string('avatar')->nullable();
            $table->timestamp('last_login_at')->nullable();
            $table->boolean('is_admin')->default(false);
            $table->string('referral_code')->unique()->nullable();
            $table->unsignedBigInteger('referred_by')->nullable();
            $table->integer('total_referrals')->default(0);
            $table->integer('active_referrals')->default(0);
            $table->json('ai_risk_profile')->nullable();
            $table->enum('risk_tolerance', ['conservative', 'moderate', 'aggressive'])->default('moderate');
            $table->decimal('ai_score', 5, 2)->default(0);
            $table->string('nonce', 64)->nullable();
            $table->timestamp('nonce_expires_at')->nullable();
            $table->rememberToken();
            $table->timestamps();

            $table->foreign('referred_by')->references('id')->on('users')->onDelete('set null');
        });

        Schema::create('password_reset_tokens', function (Blueprint $table) {
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->foreignId('user_id')->nullable()->index();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->longText('payload');
            $table->integer('last_activity')->index();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('users');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('sessions');
    }
};

<?php

use App\Enums\Wallet\Status;
use App\Enums\Wallet\Type;
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('wallets', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->index()->constrained()->onDelete('cascade');
            $table->string('name');
            $table->string('address')->unique();
            $table->decimal('balance', 20, 8)->default(0);
            $table->decimal('bonus_balance', 20, 8)->default(0);
            $table->decimal('total_invested', 20, 8)->default(0);
            $table->decimal('total_withdrawn', 20, 8)->default(0);
            $table->decimal('total_earnings', 20, 8)->default(0);
            $table->decimal('total_referral_bonus', 20, 8)->default(0);
            $table->enum('status', ['inactive', 'active', 'locked'])->default('active')->index();
            $table->timestamp('last_activity')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('wallets');
    }
};
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
   <?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('transactions', function (Blueprint $table) {
               $table->id();
               $table->string('transaction_id', 50)->unique()->index();
               $table->foreignId('user_id')->constrained()->onDelete('cascade');
               $table->enum('type', [
                   'deposit',
                   'withdrawal',
                   'transfer',
                   'credit',
                   'debit',
                   'bonus',
                   'stake',
                   'unstake',
                   'staking_reward',
               ]);
               $table->decimal('amount', 20, 8);
               $table->decimal('fee', 20, 8)->default(0);
               $table->decimal('post_balance', 20, 8);
               $table->enum('status', [
                   'pending',
                   'completed',
                   'failed',
                   'cancelled'
               ])->default('pending');
               $table->text('details')->nullable();
               $table->timestamps();

               $table->index(['user_id', 'type']);
               $table->index(['user_id', 'status']);
               $table->index(['created_at']);
           });
       }

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


    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('transactions');
    }
};
<?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('deposits', function (Blueprint $table) {
            $table->id();
            $table->string('trx')->unique();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('payment_gateway_id')->nullable();
            $table->decimal('amount', 28, 8);
            $table->decimal('charge', 28, 8)->default(0);
            $table->decimal('final_amount', 28, 8);
            $table->decimal('deposit_amount', 28, 8)->default(0);
            $table->string('currency', 10);
            $table->enum('status', ['pending', 'approved', 'rejected'])->default('pending');
            $table->text('admin_response')->nullable();
            $table->json('payment_details')->nullable();
            $table->string('transaction_id')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('rejected_at')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users');
            $table->foreignId('rejected_by')->nullable()->constrained('users');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('deposits');
    }
};
<?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('withdrawals', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('withdrawal_gateway_id')->constrained()->onDelete('cascade');
            $table->string('trx')->unique();
            $table->decimal('amount', 20, 8);
            $table->decimal('charge', 20, 8)->default(0);
            $table->decimal('final_amount', 20, 8)->default(0);
            $table->decimal('withdrawal_amount', 20, 8)->default(0);
            $table->string('currency', 10);
            $table->decimal('conversion_rate', 20, 8)->default(0);
            $table->enum('status', ['pending', 'approved', 'rejected'])->default('pending');
            $table->json('user_data');
            $table->text('admin_response')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('rejected_at')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users')->onDelete('set null');
            $table->foreignId('rejected_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('withdrawals');
    }
};
<?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('investment_plans', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->string('slug', 100)->unique();
            $table->text('description');
            $table->json('features')->nullable();

            $table->decimal('min_amount', 20, 8);
            $table->decimal('max_amount', 20, 8);
            $table->decimal('interest_rate', 8, 4);
            $table->enum('interest_type', ['fixed', 'compound'])->default('fixed');
            $table->enum('return_type', ['hourly', 'daily', 'weekly', 'monthly'])->default('daily');
            $table->integer('duration_days');
            $table->enum('principal_return', ['yes', 'no', 'after_maturity'])->default('yes');

            $table->boolean('auto_reinvest_available')->default(false);
            $table->decimal('auto_reinvest_rate', 5, 2)->default(0);
            $table->boolean('partial_withdrawal_allowed')->default(false);
            $table->decimal('early_withdrawal_fee', 5, 2)->default(0);

            $table->integer('max_investors')->default(0);
            $table->integer('current_investors')->default(0);
            $table->decimal('total_cap', 20, 8)->default(0);
            $table->decimal('current_invested', 20, 8)->default(0);

            $table->enum('risk_level', ['low', 'medium', 'high'])->default('medium');
            $table->json('ai_recommendation_score')->nullable();
            $table->decimal('success_rate', 5, 2)->default(0);

            $table->string('color_scheme', 7)->default('#3b82f6');
            $table->json('badges')->nullable();
            $table->integer('sort_order')->default(0);
            $table->boolean('featured')->default(false);

            $table->enum('status', ['active', 'inactive', 'coming_soon'])->default('active');
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();

            $table->decimal('total_profit_paid', 20, 8)->default(0);
            $table->integer('total_completed_investments')->default(0);

            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('investment_plans');
    }
};
<?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('user_investments', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('plan_id');
            $table->string('investment_id', 20)->unique();
            $table->decimal('amount', 20, 8);
            $table->decimal('expected_return', 20, 8);
            $table->decimal('earned_amount', 20, 8)->default(0);
            $table->decimal('withdrawn_amount', 20, 8)->default(0);
            $table->decimal('remaining_principal', 20, 8)->default(0);

            $table->enum('status', ['pending', 'active', 'completed', 'cancelled'])->default('pending');
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('next_return_at')->nullable();
            $table->timestamp('maturity_date')->nullable();
            $table->timestamp('completed_at')->nullable();

            $table->integer('total_returns_expected');
            $table->integer('returns_paid')->default(0);
            $table->decimal('return_amount', 20, 8)->default(0);
            $table->json('return_schedule')->nullable();

            $table->boolean('auto_reinvest')->default(false);
            $table->decimal('reinvest_percentage', 5, 2)->default(0);
            $table->decimal('reinvested_amount', 20, 8)->default(0);

            $table->json('ai_prediction_data')->nullable();
            $table->decimal('ai_confidence_score', 5, 2)->default(0);

            $table->text('notes')->nullable();
            $table->string('source', 50)->default('web');

            $table->timestamps();
            $table->softDeletes();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('plan_id')->references('id')->on('investment_plans')->onDelete('cascade');

            $table->index(['user_id', 'status']);
            $table->index(['plan_id', 'status']);
            $table->index('investment_id');
            $table->index(['status', 'next_return_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('user_investments');
    }
};
<?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('referral_settings', function (Blueprint $table) {
            $table->id();
            $table->integer('level');
            $table->decimal('commission_rate', 5, 2);
            $table->enum('commission_type', ['percentage', 'fixed'])->default('percentage');
            $table->decimal('fixed_amount', 10, 2)->default(0);
            $table->enum('applies_to', ['deposit', 'profit', 'both'])->default('deposit');
            $table->integer('min_referrals_required')->default(0);
            $table->decimal('min_investment_required', 20, 8)->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique('level');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('referral_settings');
    }
};
<?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('user_referrals', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('referrer_id');
            $table->unsignedBigInteger('referred_id');
            $table->integer('level');
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->decimal('total_commission_earned', 20, 8)->default(0);
            $table->decimal('total_referral_investment', 20, 8)->default(0);
            $table->integer('generation_count')->default(0);
            $table->timestamps();

            $table->foreign('referrer_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('referred_id')->references('id')->on('users')->onDelete('cascade');

            $table->unique(['referrer_id', 'referred_id']);
            $table->index(['referrer_id', 'level']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('user_referrals');
    }
};
<?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('referral_commissions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('referrer_id');
            $table->unsignedBigInteger('referred_id');
            $table->unsignedBigInteger('investment_id')->nullable();
            $table->integer('level');

            $table->decimal('base_amount', 20, 8);
            $table->decimal('commission_rate', 5, 2);
            $table->decimal('commission_amount', 20, 8);
            $table->string('currency', 10)->default('USD');

            $table->enum('type', ['deposit', 'investment_profit', 'reinvestment']);
            $table->enum('status', ['pending', 'approved', 'paid'])->default('pending');
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('paid_at')->nullable();

            $table->text('description')->nullable();
            $table->json('calculation_details')->nullable();
            $table->timestamps();

            $table->foreign('referrer_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('referred_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('investment_id')->references('id')->on('user_investments')->onDelete('set null');

            $table->index(['referrer_id', 'status']);
            $table->index(['level', 'type']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('referral_commissions');
    }
};
<?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('referral_bonus', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->text('description');
            $table->enum('type', ['milestone', 'achievement', 'loyalty']);

            $table->integer('min_active_referrals')->default(0);
            $table->decimal('min_team_investment', 20, 8)->default(0);
            $table->integer('time_frame_days')->default(0);

            $table->enum('reward_type', ['fixed_amount', 'percentage', 'upgrade']);
            $table->decimal('reward_amount', 20, 8)->default(0);
            $table->string('reward_details')->nullable();

            $table->boolean('is_active')->default(true);
            $table->boolean('is_recurring')->default(false);
            $table->integer('max_claims_per_user')->default(1);

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('referral_bonus');
    }
};
<?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('user_referral_achievements', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('bonus_id');
            $table->enum('status', ['earned', 'claimed'])->default('earned');
            $table->decimal('reward_amount', 20, 8);
            $table->timestamp('earned_at');
            $table->timestamp('claimed_at')->nullable();
            $table->json('achievement_data')->nullable();
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('bonus_id')->references('id')->on('referral_bonus')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('user_referral_achievements');
    }
};
<?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('ai_recommendations', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->enum('type', ['investment_plan', 'reinvestment']);
            $table->string('title');
            $table->text('description');
            $table->json('recommendation_data');
            $table->decimal('confidence_score', 5, 2);
            $table->enum('priority', ['low', 'medium', 'high'])->default('medium');
            $table->enum('status', ['active', 'dismissed', 'applied'])->default('active');
            $table->boolean('is_read')->default(false);
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->index(['user_id', 'status']);
        });
    }

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

<?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('staking_pools', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('token_symbol', 10);
            $table->text('description');

            $table->decimal('min_stake_amount', 20, 8);
            $table->decimal('max_stake_amount', 20, 8)->nullable();
            $table->decimal('apy_rate', 8, 4);
            $table->integer('lock_days');

            $table->decimal('total_pool_size', 20, 8)->default(0);
            $table->decimal('current_staked', 20, 8)->default(0);

            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->boolean('auto_compound')->default(false);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('staking_pools');
    }
};
<?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('user_stakes', function (Blueprint $table) {
            $table->id();
            $table->string('stake_id', 20)->unique();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('pool_id')->constrained('staking_pools')->onDelete('cascade');

            $table->decimal('stake_amount', 20, 8);
            $table->decimal('current_balance', 20, 8);
            $table->decimal('total_rewards', 20, 8)->default(0);

            $table->decimal('apy_rate', 8, 4);
            $table->timestamp('staked_at');
            $table->timestamp('unlock_at')->nullable();

            $table->enum('status', ['active', 'completed', 'cancelled'])->default('active');
            $table->boolean('auto_compound')->default(false);

            $table->timestamps();

            $table->index(['user_id', 'status']);
            $table->index(['pool_id', 'status']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('user_stakes');
    }
};
<?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('staking_rewards', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('stake_id')->constrained('user_stakes')->onDelete('cascade');

            $table->decimal('reward_amount', 20, 8);
            $table->date('reward_date');
            $table->enum('status', ['pending', 'paid', 'compounded'])->default('pending');

            $table->timestamps();

            $table->index(['user_id', 'reward_date']);
            $table->index(['stake_id', 'status']);
        });
    }

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

