<?php
require dirname(__DIR__).'/includes/bootstrap.php';
$id=$_GET['id']??'';$u=current_user();$owner='';$ownerId='';
if($u){$owner='user_id';$ownerId=$u['id'];}else{$token=$_COOKIE['bvx_guest']??'';if(!$token)json_out(['error'=>'Not found'],404);$s=$pdo->prepare('SELECT id FROM guest_sessions WHERE token_hash=? AND updated_at>=NOW()-INTERVAL 24 HOUR');$s->execute([hash('sha256',$token)]);$g=$s->fetch();if(!$g)json_out(['error'=>'Not found'],404);$owner='guest_id';$ownerId=$g['id'];}
$q=$pdo->prepare("SELECT id FROM conversations WHERE id=? AND $owner=?");$q->execute([$id,$ownerId]);if(!$q->fetch())json_out(['error'=>'Not found'],404);$m=$pdo->prepare('SELECT role,content,created_at FROM messages WHERE conversation_id=? ORDER BY id');$m->execute([$id]);json_out(['id'=>$id,'messages'=>$m->fetchAll()]);
