<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('players', function (Blueprint $table): void {
            $table->string('auth_source', 16)->default('main_site_sso')->after('site_player_id');
            $table->string('funding_mode', 16)->default('wallet')->after('auth_source');
            $table->string('password_hash', 255)->nullable()->after('username');
            $table->unsignedSmallInteger('login_failed_count')->default(0)->after('last_login_at');
            $table->timestamp('login_locked_until')->nullable()->after('login_failed_count');
        });

        DB::table('players')->update([
            'auth_source' => 'main_site_sso',
            'funding_mode' => 'wallet',
        ]);

        Schema::table('players', function (Blueprint $table): void {
            $table->index(['site_code', 'auth_source', 'username'], 'idx_players_site_auth_username');
        });
    }

    public function down(): void
    {
        Schema::table('players', function (Blueprint $table): void {
            $table->dropIndex('idx_players_site_auth_username');
            $table->dropColumn([
                'auth_source',
                'funding_mode',
                'password_hash',
                'login_failed_count',
                'login_locked_until',
            ]);
        });
    }
};
