<?php
require_once 'config.php';

// Fungsi untuk generate angka acak berdasarkan prize 1
function generateRelatedNumber($first_prize) {
    // Logika generate angka terkait prize 1
    // Contoh sederhana: ambil 2 digit pertama dan 2 digit acak
    $prefix = substr($first_prize, 0, 2);
    $suffix = str_pad(mt_rand(0, 99), 2, '0', STR_PAD_LEFT);
    return $prefix . $suffix;
}

// Ambil semua result hari ini yang belum lengkap
$today = date('Y-m-d');
$results = $db->query("SELECT * FROM lottery_results 
                      WHERE draw_date = '$today' 
                      AND (second_prize = '0000' OR third_prize = '0000' 
                      OR starter_prize = '0000' OR consolation_prize = '0000')")
             ->fetchAll(PDO::FETCH_ASSOC);

$current_time = date('H:i:s');

foreach ($results as $result) {
    $updates = [];
    
    // Generate prize 2 jika waktunya sudah lewat
    if ($current_time >= $result['generate_time_2nd'] && $result['second_prize'] == '0000') {
        $updates['second_prize'] = generateRelatedNumber($result['first_prize']);
    }
    
    // Generate prize 3 jika waktunya sudah lewat
    if ($current_time >= $result['generate_time_3rd'] && $result['third_prize'] == '0000') {
        $updates['third_prize'] = generateRelatedNumber($result['first_prize']);
    }
    
    // Generate starter prize jika waktunya sudah lewat
    if ($current_time >= $result['generate_time_starter'] && $result['starter_prize'] == '0000') {
        $updates['starter_prize'] = generateRelatedNumber($result['first_prize']);
    }
    
    // Generate consolation prize jika waktunya sudah lewat
    if ($current_time >= $result['generate_time_consolation'] && $result['consolation_prize'] == '0000') {
        $updates['consolation_prize'] = generateRelatedNumber($result['first_prize']);
    }
    
    // Update database jika ada yang perlu diupdate
    if (!empty($updates)) {
        $setParts = [];
        foreach ($updates as $col => $val) {
            $setParts[] = "$col = '$val'";
        }
        $setClause = implode(', ', $setParts);
        
        $db->query("UPDATE lottery_results SET $setClause 
                   WHERE id = {$result['id']}");
    }
}
$generate_schedule = [
    'consolation' => ['start' => '14:15:00', 'end' => '14:16:59'],
    'starter' => ['start' => '14:17:00', 'end' => '14:19:59'],
    'third' => ['start' => '14:20:00', 'end' => '14:22:59'],
    'second' => ['start' => '14:23:00', 'end' => '14:25:59'],
    'first' => ['start' => '14:26:00', 'end' => '14:30:00']
];

$current_time = date('H:i:s');

foreach ($results as $result) {
    $updates = [];
    
    // Generate Consolation Prize (14:15:01-14:16:59)
    if ($current_time >= $generate_schedule['consolation']['start'] && 
        $current_time <= $generate_schedule['consolation']['end'] &&
        $result['consolation_prize'] == '0000') {
        $updates['consolation_prize'] = generateRelatedNumber($result['first_prize']);
    }
    
    // Generate Starter Prize (14:17:00-14:19:59)
    if ($current_time >= $generate_schedule['starter']['start'] && 
        $current_time <= $generate_schedule['starter']['end'] &&
        $result['starter_prize'] == '0000') {
        $updates['starter_prize'] = generateRelatedNumber($result['first_prize']);
    }
    
    // ... dan seterusnya untuk prize lainnya
}