<?php
include "config.php";
session_start();

$pid = $_SESSION['user']['pid'] ?? 0;

$data = [];

try {
    $data = [
        "kyc" => 0,
        "lien" => 0,
        "comply" => 0,
        "cctv" => 0,
        "facebook" => 0,
        "instagram" => 0,
        "whatsapp" => 0,
        "twitter" => 0,
        "snapchat" => 0,
        "google" => 0,
        "domain" => 0,
        "cdr" => 0
    ];

    // All queries grouped in one associative array
    $queries = [
        "kyc" => "SELECT COUNT(*) AS total FROM kyc WHERE type = 'KYC' AND policestation_id = '$pid'",
        "lien" => "SELECT COUNT(*) AS total FROM kyc WHERE type = 'LIEN AMOUNT' AND policestation_id = '$pid'",
        "comply" => "SELECT COUNT(*) AS total FROM comply_order WHERE pid = '$pid'",
        "cctv" => "SELECT COUNT(*) AS total FROM cctv WHERE pid = '$pid'",

        // Social Media platforms
        "facebook" => "SELECT COUNT(*) AS total FROM social_media WHERE type = 'facebook' AND pid = '$pid'",
        "instagram" => "SELECT COUNT(*) AS total FROM social_media WHERE type = 'instagram' AND pid = '$pid'",
        "whatsapp" => "SELECT COUNT(*) AS total FROM social_media WHERE type = 'whatsapp' AND pid = '$pid'",
        "twitter" => "SELECT COUNT(*) AS total FROM social_media WHERE type = 'twitter' AND pid = '$pid'",
        "snapchat" => "SELECT COUNT(*) AS total FROM social_media WHERE type = 'snapchat' AND pid = '$pid'",
        "google" => "SELECT COUNT(*) AS total FROM social_media WHERE type = 'google' AND pid = '$pid'",

        // Domain and CDR
        "domain" => "SELECT COUNT(*) AS total FROM domain_data WHERE pid = '$pid'",
        "cdr" => "SELECT COUNT(*) AS total FROM cdr WHERE pid = '$pid'",
    ];

    foreach ($queries as $key => $sql) {
        $result = mysqli_query($con, $sql);
        $row = mysqli_fetch_assoc($result);
        $data[$key] = (int) $row['total'];
    }

    echo json_encode($data);

} catch (Exception $e) {
    echo json_encode(["error" => "Something went wrong"]);
}
?>