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

$escalationsController = new EscalationsController();

// Authenticate for all other routes

// Escalation Routes
// if ($method === 'POST' && $uri === '/escalations') {
//     $decoded = authenticate();
//     $user_id = $decoded->user_id ?? null;
//     $user_type = $decoded->user_type ?? null;
//     $data = json_decode(file_get_contents('php://input'), true);
//     $response = $escalationsController->createEscalation($data);
//     logAndRespond($user_id, $user_type, '/escalations', $response, 'POST', $data);
// }

// if ($method === 'GET' && $uri === '/escalations') {
//     $decoded = authenticate();
//     $user_id = $decoded->user_id ?? null;
//     $user_type = $decoded->user_type ?? null;
//     $response = $escalationsController->getEscalations($_GET);
//     logAndRespond($user_id, $user_type, '/escalations', $response, 'GET', $_GET);
// }

// if ($method === 'PUT' && $uri === '/escalations-resolve') {
//     $decoded = authenticate();
//     $user_id = $decoded->user_id ?? null;
//     $user_type = $decoded->user_type ?? null;
//     $escalationId = $_GET['id'] ?? null;
//     $data = json_decode(file_get_contents('php://input'), true);
//     $data['id'] = $escalationId;
//     $response = $escalationsController->resolveEscalation($data);
//     logAndRespond($user_id, $user_type, $uri, $response, 'PUT', $data);
// }

// CREATE
if ($method === 'POST' && $uri === '/escalations') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;
    $user_name = $decoded->name ?? 'Unknown';

    $data = json_decode(file_get_contents('php://input'), true) ?? [];
    $data['escalated_to_name'] = $data['escalated_to_name'] ?? 'Supervisor';

    $response = $escalationsController->create($data, $user_id, $user_name);
    logAndRespond($user_id, $user_role, '/escalations', $response, 'POST', $data);
}

// LIST
if ($method === 'GET' && $uri === '/escalations') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;
    $user_role_id = $_GET['user_role_id'] ?? null;

    $filters = $_GET;
    $response = $escalationsController->getList($filters, $user_id, $user_role_id);
    logAndRespond($user_id, $user_role, '/escalations', $response, 'GET', $_GET);
}

// GET ONE
if ($method === 'GET' && $uri === '/get-escalations') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;
    $user_role_id = $_GET['user_role_id'] ?? null;
    $id = $_GET['id'] ?? null;

    $response = $escalationsController->getOne($id, $user_id, $user_role_id);
    logAndRespond($user_id, $user_role, "/get-escalations?id={$id}", $response, 'GET', []);
}

// ADD COMMENT
if ($method === 'POST' && $uri === '/escalations-comments') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;
    $user_name = $decoded->name ?? 'Unknown';
    $id = $_GET['id'] ?? null;

    $data = json_decode(file_get_contents('php://input'), true) ?? [];
    $response = $escalationsController->addComment($id, $data, $user_id, $user_name);
    logAndRespond($user_id, $user_role, "/escalations-comments?id={$id}", $response, 'POST', $data);
}

// RESOLVE
if ($method === 'POST' && $uri === '/escalations-resolve') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;
    $id = $_GET['id'] ?? null;

    $data = json_decode(file_get_contents('php://input'), true) ?? [];
    $response = $escalationsController->resolve($id, $data, $user_id);
    logAndRespond($user_id, $user_role, "/escalations-resolve?id={$id}", $response, 'PATCH', $data);
}

// UPDATE STATUS
if ($method === 'POST' && $uri === '/escalations-status') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;
    $id = $_GET['id'] ?? null;

    $data = json_decode(file_get_contents('php://input'), true) ?? [];
    $status = $data['status'] ?? '';
    $response = $escalationsController->updateStatus($id, $status, $user_id, $user_role);
    logAndRespond($user_id, $user_role, "/escalations-status?id={$id}", $response, 'POST', $data);
}

// DELETE
if ($method === 'DELETE' && $uri === '/delete-escalations') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;
    $id = $_GET['id'] ?? null;

    $response = $escalationsController->delete($id, $user_id, $user_role);
    logAndRespond($user_id, $user_role, "/delete-escalations?id={$id}", $response, 'DELETE', []);
}