<?php
require_once '../includes/config.php';
requireLogin();
$user = getCurrentUser();
$db = getDB();

$data = json_decode(file_get_contents('php://input'), true);
$code = strtoupper(trim($data['currency'] ?? ''));

if (!$code) jsonResponse(false, 'Currency code required');

// Verify exists and active
$cur = $db->prepare("SELECT * FROM currencies WHERE code=? AND is_active=1");
$cur->execute([$code]);
$currency = $cur->fetch();

if (!$currency) jsonResponse(false, 'Invalid currency');

$db->prepare("UPDATE users SET preferred_currency=? WHERE id=?")->execute([$code, $user['id']]);
jsonResponse(true, 'Currency updated!', ['currency' => $currency]);
