<?php

include '../db_connect.php';

$postid = $_POST['postid'];
$key = $_POST['key'];

$security_key = decrypData($key);

if ($security_key == 'Jobayer121415') {

    $con = dbConnect();

    $response = array();

    // SQL query to count comments under specific postid
    $sql = "SELECT COUNT(*) AS total_comments FROM comment_table WHERE postid = '$postid'";
    $result = mysqli_query($con, $sql);

    if ($result) {
        $row = mysqli_fetch_assoc($result);
        $response['total_comments'] = $row['total_comments'];
        echo json_encode($response);
    } else {
        echo json_encode(['error' => 'Query Error']);
    }

} else {
    echo json_encode(['error' => 'Invalid key']);
}

function decrypData($text) {
    $encoded = $text;
    $decoded = base64_decode($encoded);
    $decrypted = openssl_decrypt($decoded, 'AES-128-ECB', '0175526800812345', OPENSSL_RAW_DATA);
    return $decrypted;
}

?>
