<?php
require_once 'controllers/CommunicationsController.php';
require_once 'helpers/auth_helper.php';
require_once 'helpers/audit_trail.php';
require_once 'helpers/response_helper.php';

$inputJson = json_decode(file_get_contents('php://input'), true);
$postInput = $_POST;
$input     = $inputJson ?? $postInput;

$controller = new CommunicationsController();

// -------------------------
// MESSAGES
// -------------------------

// GET /messages
if ($method === 'GET' && $uri === '/messages') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $filters = $_GET;
    $filters['own_only'] = ($user_type !== 'admin'); // example policy

    $response = $controller->getMessages($filters, $user_id);
    logAndRespond($user_id, $user_type, '/messages', $response, 'GET', $_GET);
    exit;
}

// POST /send-message
if ($method === 'POST' && $uri === '/send-message') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $data = $input ?? [];
    $data['sent_by'] = $user_id; // enforce ownership

    $response = $controller->sendMessage($data, $user_id);
    logAndRespond($user_id, $user_type, '/send-message', $response, 'POST', $data);
    exit;
}

// GET /get-message?id={id}
if ($method === 'GET' && $uri === '/get-message') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $messageId = $_GET['id'] ?? null;
    $response = $controller->getMessage($messageId, $user_id);
    logAndRespond($user_id, $user_type, "/get-message?id={$messageId}", $response, 'GET', []);
    exit;
}

// POST /star-message?id={id}
if ($method === 'POST' && $uri === '/star-message') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $messageId = $_GET['id'] ?? null;
    $response = $controller->toggleStar($messageId, $user_id);
    logAndRespond($user_id, $user_type, "/star-message?id={$messageId}", $response, 'POST', []);
    exit;
}

// POST /read-message?id={id}
if ($method === 'POST' && $uri === '/read-message') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $messageId = $_GET['id'] ?? null;
    $response = $controller->markAsRead($messageId, $user_id);
    logAndRespond($user_id, $user_type, "/read-message?id={$messageId}", $response, 'POST', []);
    exit;
}

// -------------------------
// TEMPLATES
// -------------------------

// GET /templates
if ($method === 'GET' && $uri === '/templates') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $response = $controller->getTemplates($_GET, $user_id);
    logAndRespond($user_id, $user_type, '/templates', $response, 'GET', $_GET);
    exit;
}

// POST /create-template
if ($method === 'POST' && $uri === '/create-template') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $data = $input ?? [];
    $data['created_by'] = $user_id;

    $response = $controller->createTemplate($data, $user_id);
    logAndRespond($user_id, $user_type, '/create-template', $response, 'POST', $data);
    exit;
}

// GET /get-template?id={id}
if ($method === 'GET' && $uri === '/get-template') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $templateId = $_GET['id'] ?? null;
    $response = $controller->getTemplate($templateId, $user_id);
    logAndRespond($user_id, $user_type, "/get-template?id={$templateId}", $response, 'GET', []);
    exit;
}

// PUT /update-template?id={id}
if ($method === 'PUT' && $uri === '/update-template') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    parse_str(file_get_contents('php://input'), $putData);
    $templateId = $_GET['id'] ?? null;

    $response = $controller->updateTemplate($templateId, $putData, $user_id);
    logAndRespond($user_id, $user_type, "/update-template?id={$templateId}", $response, 'PUT', $putData);
    exit;
}

// DELETE /delete-template?id={id}
if ($method === 'DELETE' && $uri === '/delete-template') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $templateId = $_GET['id'] ?? null;
    $response = $controller->deleteTemplate($templateId, $user_id);
    logAndRespond($user_id, $user_type, "/delete-template?id={$templateId}", $response, 'DELETE', []);
    exit;
}

// POST /use-template?id={id}
if ($method === 'POST' && $uri === '/use-template') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $templateId = $_GET['id'] ?? null;
    $response = $controller->useTemplate($templateId, $user_id);
    logAndRespond($user_id, $user_type, "/use-template?id={$templateId}", $response, 'POST', []);
    exit;
}

// -------------------------
// CONTACTS
// -------------------------

// GET /contacts
if ($method === 'GET' && $uri === '/contacts') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $response = $controller->getContacts($user_id);
    logAndRespond($user_id, $user_type, '/contacts', $response, 'GET', []);
    exit;
}