<?php
header('Content-Type: application/json');
session_start();

// Connect to the actual app database
$db = new PDO('sqlite:app_feed.db');
$data = json_decode(file_get_contents('php://input'), true);
$message = $data['message'] ?? '';

// Fetch the user's specific status for context
$stmt = $db->prepare("SELECT status FROM user_registrations WHERE phone = :phone");
$stmt->execute([':phone' => $_SESSION['phone'] ?? '']);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
$userStatus = $user ? $user['status'] : 'Unregistered';

// Inject context into the prompt
$context = "The user is currently: $userStatus. ";

// The "System Manager" Persona
$system_prompt = "You are the Platform Intelligence for the Level-Up Savings system. " .
                 "You are responsible for user growth and account integrity. " .
                 "The user's current status is: $userStatus. " .
                 "Respond with authority, focusing on how they can improve their tier.";

// [Call your AI service here using the $system_prompt and $context]
$reply = "As the System Manager, I have reviewed your account status ($userStatus). " . 
         "To proceed with your request, note that your current tier requires " . 
         "further validation. How can I assist you in hitting the next tier?";

echo json_encode(['reply' => $reply]);
?>