<?php
session_start();
require_once 'config.php';
checkLogin();

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Ambil ID dari URL
$market_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

echo "<h2>Test Delete Market ID: $market_id</h2>";

// Tampilkan semua market untuk referensi
echo "<h3>Daftar Market yang Tersedia:</h3>";
$stmt = $db->query("SELECT id, market_name, display_name FROM markets ORDER BY id");
$all_markets = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo "<ul>";
foreach ($all_markets as $m) {
    $display = $m['display_name'] ?? $m['market_name'];
    echo "<li>ID: <strong>{$m['id']}</strong> - {$m['market_name']} ({$display})</li>";
}
echo "</ul>";

if ($market_id > 0) {
    try {
        // Cek data market
        $stmt = $db->prepare("SELECT * FROM markets WHERE id = ?");
        $stmt->execute([$market_id]);
        $market = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($market) {
            echo "<h3>Data Market yang Akan Dihapus:</h3>";
            echo "<pre>";
            print_r($market);
            echo "</pre>";
            
            $market_name = $market['market_name'];
            
            // Cek data result
            $stmt = $db->prepare("SELECT COUNT(*) FROM lottery_results WHERE market = ?");
            $stmt->execute([$market_name]);
            $result_count = $stmt->fetchColumn();
            echo "<p>Jumlah data result: <strong>$result_count</strong></p>";
            
            if ($result_count > 0) {
                // Tampilkan sample data result
                $stmt = $db->prepare("SELECT * FROM lottery_results WHERE market = ? LIMIT 3");
                $stmt->execute([$market_name]);
                $sample_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
                echo "<p>Sample data result:</p>";
                echo "<pre>";
                print_r($sample_results);
                echo "</pre>";
            }
            
            // Konfirmasi
            echo "<h3>Proses Hapus:</h3>";
            
            // 1. Hapus result
            $stmt = $db->prepare("DELETE FROM lottery_results WHERE market = ?");
            $stmt->execute([$market_name]);
            $deleted_results = $stmt->rowCount();
            echo "<p style='color:green'>✓ Berhasil hapus <strong>$deleted_results</strong> data result</p>";
            
            // 2. Hapus market
            $stmt = $db->prepare("DELETE FROM markets WHERE id = ?");
            $stmt->execute([$market_id]);
            $deleted_market = $stmt->rowCount();
            
            if ($deleted_market > 0) {
                echo "<p style='color:green'>✓ Berhasil hapus market!</p>";
                echo "<h3 style='color:green'>✅ SUKSES! Market berhasil dihapus</h3>";
                $_SESSION['success_message'] = 'Market "' . $market_name . '" berhasil dihapus dengan ' . $deleted_results . ' data result!';
            } else {
                echo "<p style='color:orange'>⚠ Market sudah terhapus atau tidak ditemukan!</p>";
            }
            
        } else {
            echo "<p style='color:red'>❌ Market dengan ID <strong>$market_id</strong> tidak ditemukan!</p>";
            echo "<p>Silakan pilih ID yang valid dari daftar di atas.</p>";
        }
        
    } catch (PDOException $e) {
        echo "<p style='color:red'>❌ Error: " . $e->getMessage() . "</p>";
    }
} else {
    echo "<p style='color:orange'>⚠ Silakan masukkan ID market yang valid.</p>";
    echo "<p>Contoh: <code>test_delete_direct.php?id=1</code></p>";
}

echo "<br>";
echo "<a href='list_markets.php' class='btn btn-primary'>Lihat Daftar Market</a>";
echo " | ";
echo "<a href='add_result.php' class='btn btn-secondary'>Kembali ke Add Result</a>";
?>