<?php

declare(strict_types=1);

use App\Enums\AdminUserStatus;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * 用户与会话（合并原 extend_users_for_admin_access：后台 login_account、状态等）。
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('login_account', 64)->comment('登录账号');
            $table->string('email')->nullable();
            $table->string('nickname', 80)->nullable()->comment('昵称');
            $table->string('status', 20)->default(AdminUserStatus::Enabled->value)->comment('后台状态');
            $table->boolean('is_bootstrap_install_admin')
                ->default(false)
                ->comment('安装向导创建的首任管理员，禁止在后台调整角色与权限项直绑');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->timestamp('last_login_at')->nullable()->comment('最后登录时间');
            $table->rememberToken();
            $table->timestamps();

            $table->unique('login_account', 'users_login_account_unique');
            $table->index('status', 'idx_users_status');
            $table->index('last_login_at', 'idx_users_last_login');
        });

        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();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('sessions');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('users');
    }
};
