<?php
declare(strict_types=1);

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

require_once __DIR__ . '/../db.php';

header('Content-Type: application/json; charset=utf-8');

if (empty($_SESSION['user_id'])) {
    http_response_code(401);
    echo json_encode(['ok'=>false]);
    exit;
}

$pdo = db();

$userId = (int)$_SESSION['user_id'];
$roomId = (int)($_POST['room_id'] ?? 0);

if($roomId<=0){
echo json_encode(['ok'=>false]);
exit;
}

try{

$st=$pdo->prepare("
DELETE FROM chat_room_members
WHERE room_id=? AND employee_id=?
");

$st->execute([$roomId,$userId]);

echo json_encode(['ok'=>true]);

}catch(Throwable $e){

http_response_code(500);
echo json_encode([
'ok'=>false,
'error'=>$e->getMessage()
]);

}