<?php

header("Content-Type: application/json");
require_once "config.php";

$data = [];

$sql = "
SELECT 
    c.id,
    c.title,
    c.image_link,
    COUNT(f.id) AS match_found
FROM category c
LEFT JOIN freefire f 
    ON f.match_type = c.title
GROUP BY c.id, c.title, c.image_link
ORDER BY c.id DESC
";

$result = $conn->query($sql);

if (!$result) {
    echo json_encode([
        "error" => $conn->error
    ]);
    exit;
}

while ($row = $result->fetch_assoc()) {
    $data[] = [
        "id" => (int)$row["id"],
        "title" => $row["title"],
        "image_link" => $row["image_link"],
        "match_found" => (int)$row["match_found"]
    ];
}

echo json_encode($data);

$conn->close();

?>