<?php
require_once __DIR__ . '/../config.php';
$env = require __DIR__ . '/../.env.php';

$results = [];

// Sadece 6 ve 7 Aralık 2025
$startDate = '2025-12-06';
$endDate   = '2025-12-07';

// daily_matches
$sql = "SELECT 
            id,
            country,
            league,
            date,
            time,
            home_team,
            away_team,
            home_score,
            away_score,
            question,
            created_at,
            correct_option_id,
            win_points,
            lose_points,
            is_started
        FROM daily_matches
        WHERE date >= :startDate AND date <= :endDate
        ORDER BY date ASC, time ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute([
    ':startDate' => $startDate,
    ':endDate'   => $endDate,
]);
$results['daily_matches'] = $stmt->fetchAll(PDO::FETCH_ASSOC);

// daily_match_options -> sadece bu tarihlerdeki maçların seçenekleri
$sql = "SELECT 
            id,
            match_id,
            option_text
        FROM daily_match_options
        WHERE match_id IN (
            SELECT id FROM daily_matches 
            WHERE date >= :startDate AND date <= :endDate
        )
        ORDER BY id ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute([
    ':startDate' => $startDate,
    ':endDate'   => $endDate,
]);
$results['daily_match_options'] = $stmt->fetchAll(PDO::FETCH_ASSOC);

// matches -> sadece 6–7 Aralık
$sql = "SELECT * 
        FROM matches 
        WHERE date >= :startDate AND date <= :endDate
        ORDER BY id ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute([
    ':startDate' => $startDate,
    ':endDate'   => $endDate,
]);
$results['matches'] = $stmt->fetchAll(PDO::FETCH_ASSOC);

// combines -> sadece 6–7 Aralık
$sql = "SELECT 
            id,
            name,
            date,
            type,
            price,
            active,
            canEveryoneSee
        FROM combines
        WHERE date >= :startDate AND date <= :endDate
        ORDER BY date ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute([
    ':startDate' => $startDate,
    ':endDate'   => $endDate,
]);
$results['combines'] = $stmt->fetchAll(PDO::FETCH_ASSOC);

// combine_matches -> sadece 6–7 Aralık'taki kuponlara bağlı maçlar
$sql = "SELECT 
            id,
            combine_id,
            match_id
        FROM combine_matches
        WHERE combine_id IN (
            SELECT id FROM combines 
            WHERE date >= :startDate AND date <= :endDate
        )
        ORDER BY id ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute([
    ':startDate' => $startDate,
    ':endDate'   => $endDate,
]);
$results['combine_matches'] = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode(array_merge(["success" => true], $results));