<?php

namespace Database\Seeders;

use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ExchangeRateSeeder extends Seeder
{
    // php artisan db:seed --class=ExchangeRateSeeder
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Clear previous data (optional)
        // DB::table('currency_rates')->truncate();

        // Base currency
        $systemCurrency = 'LKR';

        // Target currencies and approximate rates
        $targetCurrencies = [
            'USD' => 0.0031,
            'EUR' => 0.0028,
            'GBP' => 0.0024,
        ];

        // Generate data for past 14 days
        for ($i = 14; $i >= 0; $i--) {
            $date = Carbon::now()->subDays($i);

            // Optionally add slight random fluctuation for realism
            $rates = [];
            foreach ($targetCurrencies as $code => $rate) {
                $fluctuation = mt_rand(-5, 5) / 10000; // ±0.0005
                $rates[$code] = round($rate + $fluctuation, 6);
            }

            DB::table('exchange_rates')->insert([
                'base_currency' => $systemCurrency,
                'rates' => json_encode($rates),
                'recorded_at' => $date,
                'updated_by' => 'seed', // indicate it’s seeded data
                'created_at' => $date,
                'updated_at' => $date,
            ]);
        }

        $this->command->info('Currency rates seeded for past 2 weeks.');
    }
}
