<?php
require_once 'config.php';

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

$market = $_POST['market'] ?? '';
$prizeType = $_POST['prize_type'] ?? '';
$number = $_POST['number'] ?? '';

if (empty($market) || empty($prizeType) || empty($number)) {
    echo json_encode(['success' => false, 'message' => 'Invalid data']);
    exit;
}

try {
    $stmt = $db->prepare("UPDATE lottery_results 
                         SET {$prizeType}_prize = ? 
                         WHERE market = ? 
                         ORDER BY draw_date DESC 
                         LIMIT 1");
    $stmt->execute([$number, $market]);
    
    echo json_encode(['success' => true]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}