<?php
declare(strict_types=1);
require_once __DIR__ . '/../db/config.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'error' => 'Invalid method']);
    exit;
}

$data = [
    'discord_token' => $_POST['discord_token'] ?? '',
    'client_id' => $_POST['client_id'] ?? '',
    'voice_channel_id' => $_POST['voice_channel_id'] ?? '',
    'sahur_time' => $_POST['sahur_time'] ?? '03:30',
];

try {
    $db = db();
    foreach ($data as $key => $value) {
        $stmt = $db->prepare('INSERT INTO bot_settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?');
        $stmt->execute([$key, $value, $value]);
    }
    echo json_encode(['success' => true]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
