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

// $followUpsController = new FollowUpsController();

// // Authenticate for all other routes


// // Follow-Up Process Routes
// if ($method === 'POST' && $uri === '/followups-initiate') {
//     $decoded = authenticate();
//     $user_id = $decoded->user_id ?? null;
//     $user_type = $decoded->user_type ?? null;
//     $data = json_decode(file_get_contents('php://input'), true);
//     $response = $followUpsController->initiateFollowUp($data);
//     logAndRespond($user_id, $user_type, '/followups-initiate', $response, 'POST', $data);
// }

// if ($method === 'POST' && $uri === '/followups-call') {
//     $decoded = authenticate();
//     $user_id = $decoded->user_id ?? null;
//     $user_type = $decoded->user_type ?? null;
//     $data = json_decode(file_get_contents('php://input'), true);
//     $response = $followUpsController->recordCall($data);
//     logAndRespond($user_id, $user_type, '/followups-call', $response, 'POST', $data);
// }

// if ($method === 'POST' && $uri === '/followups-reschedule') {
//     $decoded = authenticate();
//     $user_id = $decoded->user_id ?? null;
//     $user_type = $decoded->user_type ?? null;
//     $data = json_decode(file_get_contents('php://input'), true);
//     $response = $followUpsController->rescheduleFollowUp($data);
//     logAndRespond($user_id, $user_type, '/followups-reschedule', $response, 'POST', $data);
// }

// if ($method === 'POST' && $uri === '/followups-close') {
//     $decoded = authenticate();
//     $user_id = $decoded->user_id ?? null;
//     $user_type = $decoded->user_type ?? null;
//     $data = json_decode(file_get_contents('php://input'), true);
//     $response = $followUpsController->closeFollowUp($data);
//     logAndRespond($user_id, $user_type, '/followups-close', $response, 'POST', $data);
// }
?>
<?php
// Router file, e.g., api.php or index.php
require_once 'controllers/FollowUpsController.php';
require_once 'helpers/auth_helper.php';
require_once 'helpers/audit_trail.php';
require_once 'helpers/response_helper.php';

$followUpsController = new FollowUpsController();

// Authenticate for all routes
$decoded = authenticate();
$user_id = $decoded->user_id ?? null;
$user_type = $decoded->user_type ?? null;

// Routes
if ($method === 'POST' && $uri === '/followups') {
    // Create follow-up: /followups
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $followUpsController->createFollowUp($data);
    logAndRespond($user_id, $user_type, '/followups', $response, 'POST', $data);
}

if ($method === 'GET' && $uri === '/followups' ) {
    // List follow-ups: /followups
    $params = $_GET;
    if ($user_type === 'ipo') { // Assuming role_id 3 is ipo
        $params['ipo_id'] = $user_id; // Restrict to own
    }
    $response = $followUpsController->getFollowUps($params);
    logAndRespond($user_id, $user_type, '/followups', $response, 'GET', $params);
}

if ($method === 'GET' && $uri === '/get-single-followup' && isset($_GET['id'])) {
    // Get single: /followups/{id}
    $id = $_GET['id'];
    $response = $followUpsController->getSingleFollowUp($id);
    // Add ownership check if needed
    logAndRespond($user_id, $user_type, "/get-single-followup?id=$id", $response, 'GET');
}

if ($method === 'PUT' && $uri === '/update-followups') {
    // Update: /followups/{id}
    $id = $_GET['id'] ?? null;
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $followUpsController->updateFollowUp($id, $data);
    logAndRespond($user_id, $user_type, "/update-followups?id=$id", $response, 'PUT', $data);
}

if ($method === 'POST' && $uri === '/followups-close') {
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $followUpsController->closeFollowUp($data);
    logAndRespond($user_id, $user_type, '/followups-close', $response, 'POST', $data);
}

if ($method === 'POST' && $uri === '/followups-calls') {
    // Record call: /followups/{id}/calls
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $followUpsController->recordCall($data);
    logAndRespond($user_id, $user_type, "/followups-calls", $response, 'POST', $data);
}

if ($method === 'GET' && $uri === '/followups-statistics') {
    // Statistics: /followups/statistics
    $params = $_GET;
    if ($user_type === 'ipo') {
        $params['ipo_id'] = $user_id;
    }
    $response = $followUpsController->getFollowUpsStatistics($params);
    logAndRespond($user_id, $user_type, '/followups-statistics', $response, 'GET', $params);
}

if ($method === 'POST' && $uri === '/followups-reschedule') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $followUpsController->rescheduleFollowUp($data);
    logAndRespond($user_id, $user_type, '/followups-reschedule', $response, 'POST', $data);
}
?>