<?php

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

class CreateWithdrawalsTableWithSecurity extends Migration
{
    public function up()
    {
        if (Schema::hasTable('withdrawals')) {
            return;
        }

        Schema::create('withdrawals', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->decimal('amount', 15, 2);
            $table->enum('status', ['pending', 'approved', 'paid', 'rejected', 'cancelled'])->default('pending');
            $table->enum('payment_method', ['bank_transfer', 'mobile_money', 'wallet'])->default('bank_transfer');
            
            // Sécurité - 2FA
            $table->boolean('two_fa_verified')->default(false);
            $table->timestamp('two_fa_verified_at')->nullable();
            
            // Sécurité - KYC
            $table->enum('kyc_status', ['not_verified', 'pending', 'verified', 'rejected'])->default('not_verified');
            $table->timestamp('kyc_verified_at')->nullable();
            
            // Données bancaires
            $table->string('bank_account_number')->nullable();
            $table->string('bank_name')->nullable();
            $table->string('account_holder_name')->nullable();
            
            // Détection fraude
            $table->boolean('fraud_check_passed')->default(false);
            $table->text('fraud_check_details')->nullable();
            
            // Audit
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->text('rejection_reason')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamp('paid_at')->nullable();
            
            $table->timestamps();
            $table->softDeletes();
            
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
        });
    }

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