<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('admin_sites', function (Blueprint $table): void {
            $table->string('settlement_timezone', 64)
                ->default('UTC')
                ->after('currency_code');
        });

        $defaultTimezone = (string) config('lottery.settlement.default_timezone', config('app.timezone', 'UTC'));
        if (! in_array($defaultTimezone, timezone_identifiers_list(), true)) {
            $defaultTimezone = 'UTC';
        }

        DB::table('admin_sites')->update([
            'settlement_timezone' => $defaultTimezone,
        ]);
    }

    public function down(): void
    {
        Schema::table('admin_sites', function (Blueprint $table): void {
            $table->dropColumn('settlement_timezone');
        });
    }
};
