<?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
{
    /** Units per 1 USD on 2026-10-06 (Frankfurter/ECB). The daily fx:refresh replaces them. */
    private const INITIAL_RATES = [
        'AUD' => 1.4322, 'BRL' => 4.9686, 'CAD' => 1.425, 'CHF' => 0.83051, 'CNY' => 6.7046, 'CZK' => 21.657,
        'DKK' => 6.633, 'EUR' => 0.88739, 'GBP' => 0.75322, 'HKD' => 7.8479, 'HUF' => 323.85, 'IDR' => 17841,
        'ILS' => 3.048, 'INR' => 96.42, 'ISK' => 121.75, 'JPY' => 158.09, 'KRW' => 1338.78, 'MXN' => 17.9448,
        'MYR' => 4.085, 'NOK' => 9.5643, 'NZD' => 1.7802, 'PHP' => 62.74, 'PLN' => 3.8735, 'RON' => 4.7484,
        'SEK' => 9.9765, 'SGD' => 1.2771, 'THB' => 33.575, 'TRY' => 49.179, 'ZAR' => 16.4903,
    ];

    public function up(): void
    {
        Schema::create('exchange_rates', function (Blueprint $table) {
            $table->string('currency', 3)->primary();
            $table->decimal('rate_per_usd', 18, 6);
            $table->timestamp('fetched_at');
            $table->timestamps();
        });

        $now = now();

        DB::table('exchange_rates')->insert(collect(self::INITIAL_RATES)->map(fn (float|int $rate, string $currency): array => [
            'currency' => $currency,
            'rate_per_usd' => $rate,
            'fetched_at' => '2026-10-06 00:00:00',
            'created_at' => $now,
            'updated_at' => $now,
        ])->values()->all());
    }

    public function down(): void
    {
        Schema::dropIfExists('exchange_rates');
    }
};
