<?php
// /api/messages.php
require_once __DIR__ . '/middleware.php';
require_once __DIR__ . '/../Controllers/MessageController.php';

$action = $_GET['action'] ?? $_POST['action'] ?? 'list';
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;

$ctrl = new Controllers\MessageController();
header('Content-Type: application/json');

switch($action){
  case 'create':
    $user = api_authenticate_or_die();
    echo json_encode($ctrl->create($input, $user));
    break;
  case 'list':
  default:
    $page = intval($_GET['page'] ?? 1);
    $per = intval($_GET['per'] ?? 25);
    echo json_encode($ctrl->list($page, $per, $_GET));
    break;
}

?>