<?php

include_once __DIR__ . '/../secrets.php';
include '../db_connect.php';

// POST ডেটা গ্রহণ
$id = $_POST['id'];
$postid = $_POST['postid'];
$key = $_POST['key'];

$response = array();

// কী ভ্যালিডেশন
$security_key = decrypData($key);

if ($security_key === false) {
    $response['status'] = 'error';
    $response['message'] = 'Invalid encryption data';
    echo json_encode($response);
    exit;
}

if ($security_key == SECURITY_KEY_PLAIN) {
    $con = dbConnect(); 

    // কমেন্ট ডিলিট করুন
    $sql = "DELETE FROM comment_table WHERE id = '$id'";
    $result = mysqli_query($con, $sql);
    
    if ($result) {
 
        $count_sql = "SELECT COUNT(*) as total_comments FROM comment_table WHERE postid = '$postid'";
        $count_result = mysqli_query($con, $count_sql);
        
        if ($count_result) {
            $count_row = mysqli_fetch_assoc($count_result);
            $response['status'] = 'success';
            $response['message'] = 'Successfully';
            $response['total_comments'] = (int)$count_row['total_comments'];
        } else {
            $response['status'] = 'error';
            $response['message'] = 'Failed to count comments';
        }
        
    } else {
        $response['status'] = 'error';
        $response['message'] = 'Query Error: ' . mysqli_error($con);
    }

    echo json_encode($response);
} 

?>