<?php
declare(strict_types=1);
require_once __DIR__ . '/../helpers.php';
require_post();

$adminUserId = as_int(get_post('admin_user_id'));
require_admin($adminUserId);
$symbol = get_post('currency_symbol');
$code = strtoupper(get_post('currency_code'));
$name = get_post('currency_name');

if ($symbol === '' || $code === '' || $name === '') {
    respond(['success' => false, 'message' => 'Currency symbol, code, and name are required'], 422);
}

$conn = db();
$stmt = $conn->prepare('UPDATE store_settings SET currency_symbol = ?, currency_code = ?, currency_name = ?, updated_by = ?, updated_at = NOW() WHERE id = 1');
$stmt->bind_param('sssi', $symbol, $code, $name, $adminUserId);
$stmt->execute();

log_activity($adminUserId, 'UPDATE_CURRENCY', 'Changed currency to ' . $symbol . ' ' . $code . ' (' . $name . ')');
respond(['success' => true, 'message' => 'Currency settings updated']);
