<?php
namespace App\Scripts;
require_once __DIR__ . '/../vendor/autoload.php';

// Dotenv loading (manual since we're outside main entry)
$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();

use App\Config\Database;

$currencies = [
    ['name' => 'United States Dollar', 'code' => 'USD', 'symbol' => '$', 'rate' => 1.000000, 'is_base' => 1],
    ['name' => 'Euro', 'code' => 'EUR', 'symbol' => '€', 'rate' => 0.92, 'is_base' => 0],
    ['name' => 'British Pound', 'code' => 'GBP', 'symbol' => '£', 'rate' => 0.79, 'is_base' => 0],
    ['name' => 'Indian Rupee', 'code' => 'INR', 'symbol' => '₹', 'rate' => 83.12, 'is_base' => 0],
    ['name' => 'Japanese Yen', 'code' => 'JPY', 'symbol' => '¥', 'rate' => 151.41, 'is_base' => 0],
    ['name' => 'Australian Dollar', 'code' => 'AUD', 'symbol' => 'A$', 'rate' => 1.52, 'is_base' => 0],
    ['name' => 'Canadian Dollar', 'code' => 'CAD', 'symbol' => 'C$', 'rate' => 1.35, 'is_base' => 0],
    ['name' => 'Swiss Franc', 'code' => 'CHF', 'symbol' => 'Fr', 'rate' => 0.90, 'is_base' => 0],
    ['name' => 'Chinese Yuan', 'code' => 'CNY', 'symbol' => '¥', 'rate' => 7.23, 'is_base' => 0],
    ['name' => 'UAE Dirham', 'code' => 'AED', 'symbol' => 'د.إ', 'rate' => 3.67, 'is_base' => 0],
    ['name' => 'Saudi Riyal', 'code' => 'SAR', 'symbol' => '﷼', 'rate' => 3.75, 'is_base' => 0],
    ['name' => 'Singapore Dollar', 'code' => 'SGD', 'symbol' => 'S$', 'rate' => 1.35, 'is_base' => 0],
    ['name' => 'Hong Kong Dollar', 'code' => 'HKD', 'symbol' => 'HK$', 'rate' => 7.82, 'is_base' => 0]
];

$db = Database::getInstance();
$tenantId = 2; // Default demo organization ID

echo "Seeding currencies for Tenant ID: $tenantId\n";

foreach ($currencies as $c) {
    try {
        $stmt = $db->prepare("INSERT INTO currencies (tenant_id, name, code, symbol, exchange_rate, is_base) VALUES (?, ?, ?, ?, ?, ?)");
        $stmt->execute([$tenantId, $c['name'], $c['code'], $c['symbol'], $c['rate'], $c['is_base']]);
        echo "Inserted: {$c['code']} - {$c['name']}\n";
    } catch (\PDOException $e) {
        if ($e->getCode() == 23000 || str_contains($e->getMessage(), 'Duplicate entry')) {
            echo "Skipped: {$c['code']} (Already exists)\n";
        } else {
            echo "Error inserting {$c['code']}: " . $e->getMessage() . "\n";
        }
    }
}
echo "Seeding complete.\n";
