<?php
include '../db_connect.php';

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

$security_key = decrypData($key);

if ($security_key == 'Jobayer121415') {
    $response = array();
    $con = dbConnect();

    $sql = "SELECT id, userid, postid, comment, time, name, profile
            FROM comment_table 
            WHERE postid = '$postid'  
            ORDER BY time ASC";

    $result = mysqli_query($con, $sql);

    if (mysqli_num_rows($result) > 0) {
        $response["status"] = true;
        $response["comments"] = array();

        while ($row = mysqli_fetch_assoc($result)) {
            $comment = array();
            $user_id = $row["userid"];

            // ✅ ইউজারের স্ট্যাটাস signup_table থেকে খুঁজে আনা
            $status = "";
            $stmt = $con->prepare("SELECT status FROM singup_table WHERE id = ?");
            $stmt->bind_param("i", $user_id);
            $stmt->execute();
            $resultStatus = $stmt->get_result();
            if ($statusRow = $resultStatus->fetch_assoc()) {
                $status = $statusRow["status"];
            }

            $comment["id"] = $row["id"];
            $comment["user_id"] = $user_id;
            $comment["post_id"] = $row["postid"];
            $comment["comment_text"] = $row["comment"];
            $comment["comment_time"] = $row["time"];
            $comment["user_profile"] = $row["profile"];
            $comment["user_name"] = $row["name"];
            $comment["user_status"] = $status; // ✅ নতুন যোগ

            array_push($response["comments"], $comment);
        }

        $response["total_comments"] = count($response["comments"]);
    } else {
        $response["status"] = false;
        $response["message"] = "No comments found for this post.";
        $response["total_comments"] = 0;
    }

    echo json_encode($response);
}

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