<?php
/**
 * سودانا — Flow Session Status
 * GET /api/flow/status.php?session_id=...
 */
require_once __DIR__ . '/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';
require_once __DIR__ . '/../../engine/Logger.php';

require_once '../../engine/StepController.php';

$sessionId = trim($_GET['session_id'] ?? '');
if (!$sessionId) Kernel::fail('session_id_required', '', 400);

$status = StepController::getSessionStatus($sessionId);
if (!$status) Kernel::fail('session_not_found', '', 404);

// Don't expose context_data to unauthorized users
$ctx = Kernel::handle(['auth' => 'optional']);
$u = $ctx['user'];
if (($u['id'] ?? 0) != ($status['user_id'] ?? -1)) {
    unset($status['context_data']);
}

json_response(['success'=>true,'session'=>$status]);
