<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            if (!Schema::hasColumn('users', 'role')) {
                if (!Schema::hasColumn('users', 'role')) { $table->enum('role', ['admin', 'sub_admin', 'seller', 'user', 'affiliate', 'accountant', 'marketing'])->default('user'); }
            }
            if (!Schema::hasColumn('users', 'rank_id')) {
                if (!Schema::hasColumn('users', 'rank_id')) { $table->foreignId('rank_id')->nullable(); }
            }
            if (!Schema::hasColumn('users', 'preferred_language')) {
                if (!Schema::hasColumn('users', 'preferred_language')) { $table->string('preferred_language', 5)->default('fr'); }
            }
            if (!Schema::hasColumn('users', 'preferred_currency')) {
                if (!Schema::hasColumn('users', 'preferred_currency')) { $table->string('preferred_currency', 3)->default('XOF'); }
            }
            if (!Schema::hasColumn('users', 'two_factor_enabled')) {
                if (!Schema::hasColumn('users', 'two_factor_enabled')) { $table->boolean('two_factor_enabled')->default(false); }
            }
            if (!Schema::hasColumn('users', 'two_factor_secret')) {
                if (!Schema::hasColumn('users', 'two_factor_secret')) { $table->string('two_factor_secret')->nullable(); }
            }
            if (!Schema::hasColumn('users', 'kyc_status')) {
                if (!Schema::hasColumn('users', 'kyc_status')) { $table->enum('kyc_status', ['pending', 'submitted', 'approved', 'rejected'])->default('pending'); }
            }
            if (!Schema::hasColumn('users', 'kyc_documents')) {
                if (!Schema::hasColumn('users', 'kyc_documents')) { $table->json('kyc_documents')->nullable(); }
            }
            if (!Schema::hasColumn('users', 'total_loyalty_points')) {
                if (!Schema::hasColumn('users', 'total_loyalty_points')) { $table->integer('total_loyalty_points')->default(0); }
            }
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $columns = ['role', 'rank_id', 'preferred_language', 'preferred_currency', 
                       'two_factor_enabled', 'two_factor_secret', 'kyc_status', 
                       'kyc_documents', 'total_loyalty_points'];
            foreach ($columns as $column) {
                if (Schema::hasColumn('users', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};


