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

// ---------------------------------------------------------------
// APPOINTMENTS ROUTES
// ---------------------------------------------------------------
$appointmentsController = new AppointmentsController();

// ----------------------------------------------------------------
// CREATE NEW APPOINTMENT
// POST /appointments
// ----------------------------------------------------------------
if ($method === 'POST' && $uri === '/appointments') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $data = json_decode(file_get_contents('php://input'), true) ?? [];
    $data['created_by'] = $user_id; // enforce ownership

    $response = $appointmentsController->create($data, $user_id);
    logAndRespond($user_id, $user_type, '/appointments', $response, 'POST', $data);
}

// ----------------------------------------------------------------
// GET LIST (with filters, search, tabs)
// GET /appointments?tab=upcoming&search=john&type=call&priority=high&status=scheduled
// ----------------------------------------------------------------
if ($method === 'GET' && $uri === '/appointments') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;

    $filters = $_GET;
    $filters['own_only'] = ($user_type !== 'admin'); // agents see only own

    $response = $appointmentsController->getList($filters, $user_id);
    logAndRespond($user_id, $user_type, '/appointments', $response, 'GET', $_GET);
}

// ----------------------------------------------------------------
// GET SINGLE APPOINTMENT
// GET /appointments/{id}
// ----------------------------------------------------------------
if ($method === 'GET' && $uri === '/get-appointment') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $appointmentId = $_GET['id'] ?? null;

    $response = $appointmentsController->getOne($appointmentId, $user_id);
    logAndRespond($user_id, $user_type, "/get-appointment?id={$appointmentId}", $response, 'GET', []);
}

// ----------------------------------------------------------------
// UPDATE / RESCHEDULE
// PUT /appointments/{id}
// ----------------------------------------------------------------
if ($method === 'PUT' && $uri === '/update-appointment') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $appointmentId = $_GET['id'] ?? null;

    parse_str(file_get_contents('php://input'), $putData);
    $response = $appointmentsController->update($appointmentId, $putData, $user_id);
    logAndRespond($user_id, $user_type, "/update-appointments?id={$appointmentId}", $response, 'PUT', $putData);
}

// ----------------------------------------------------------------
// MARK AS COMPLETED (with optional outcome/notes)
// PATCH /appointments/{id}/complete
// ----------------------------------------------------------------
if ($method === 'PUT' && $uri === '/complete-appointment') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $appointmentId = $_GET['id'] ?? null;

    $data = json_decode(file_get_contents('php://input'), true) ?? [];
    $outcome = $data['outcome'] ?? null;
    $notes = $data['notes'] ?? null;

    $response = $appointmentsController->markComplete($appointmentId, $user_id, $outcome, $notes);
    logAndRespond($user_id, $user_type, "/complete-appointment?id={$appointmentId}", $response, 'PATCH', $data);
}

// ----------------------------------------------------------------
// CANCEL APPOINTMENT
// PATCH /appointments/{id}/cancel
// ----------------------------------------------------------------
if ($method === 'PUT' && $uri === '/cancel-appointment') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $appointmentId = $_GET['id'] ?? null;

    $response = $appointmentsController->cancel($appointmentId, $user_id);
    logAndRespond($user_id, $user_type, "/cancel-appointments?id={$appointmentId}", $response, 'PUT', []);
}

// ----------------------------------------------------------------
// MARK AS NO-SHOW
// PATCH /appointments/{id}/no-show
// ----------------------------------------------------------------
if ($method === 'PUT' && $uri === '/noshow-appointment') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $appointmentId = $_GET['id'] ?? null;

    $response = $appointmentsController->noShow($appointmentId, $user_id);
    logAndRespond($user_id, $user_type, "/noshow-appointments?id={$appointmentId}", $response, 'PATCH', []);
}

// ----------------------------------------------------------------
// SOFT DELETE
// DELETE /appointments/{id}
// ----------------------------------------------------------------
if ($method === 'PUT' && $uri === '/delete-appointment') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $appointmentId = $_GET['id'] ?? null;

    $response = $appointmentsController->delete($appointmentId, $user_id);
    logAndRespond($user_id, $user_type, "/delete-appointments?id={$appointmentId}", $response, 'DELETE', []);
}

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

    $response = $appointmentsController->getStatistics($user_id, $user_type);
    logAndRespond($user_id, $user_type, '/appointment-statistics', $response, 'GET', []);
}