<?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) {
            $table->foreignId('person_id')->nullable()->after('academy_id')->constrained('people')->nullOnDelete();
            $table->foreignId('role_id')->nullable()->after('status')->constrained('roles')->nullOnDelete();
            $table->boolean('is_super_admin')->default(false)->after('role_id');
            $table->string('preferred_locale', 5)->default('ar')->after('is_super_admin');
            $table->string('preferred_timezone', 50)->default('Africa/Cairo')->after('preferred_locale');
            $table->timestamp('last_login_at')->nullable()->after('preferred_timezone');
            $table->string('last_login_ip', 45)->nullable()->after('last_login_at');
            $table->integer('login_count')->default(0)->after('last_login_ip');
            $table->smallInteger('failed_login_attempts')->default(0)->after('login_count');
            $table->timestamp('locked_until')->nullable()->after('failed_login_attempts');
            $table->boolean('force_password_change')->default(false)->after('locked_until');
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign(['person_id']);
            $table->dropForeign(['role_id']);
            $table->dropColumn([
                'person_id',
                'role_id',
                'is_super_admin',
                'preferred_locale',
                'preferred_timezone',
                'last_login_at',
                'last_login_ip',
                'login_count',
                'failed_login_attempts',
                'locked_until',
                'force_password_change',
            ]);
        });
    }
};
