<?php
// routes/contacts.php — Contacts module (shared, org-wide directory)
require_once 'controllers/ContactsController.php';
require_once 'helpers/auth_helper.php';
require_once 'helpers/audit_trail.php';
require_once 'helpers/response_helper.php';

$contactsController = new ContactsController();

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

    $data = json_decode(file_get_contents('php://input'), true) ?? [];
    $response = $contactsController->create($data, $user_id);
    logAndRespond($user_id, $user_role, '/contacts-create', $response, 'POST', $data);
}

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

    $response = $contactsController->getList($_GET);
    logAndRespond($user_id, $user_role, '/contacts-list', $response, 'GET', $_GET);
}

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

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

// UPDATE
if ($method === 'PUT' && $uri === '/update-contact') {
    $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 = $contactsController->update($id, $data);
    logAndRespond($user_id, $user_role, "/update-contact?id={$id}", $response, 'PUT', $data);
}

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

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

// TOGGLE FAVORITE
if ($method === 'POST' && $uri === '/contact-favorite') {
    $decoded = authenticate();
    $user_id   = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;
    $id = $_GET['id'] ?? null;

    $response = $contactsController->toggleFavorite($id);
    logAndRespond($user_id, $user_role, "/contact-favorite?id={$id}", $response, 'POST', []);
}

// BULK IMPORT
if ($method === 'POST' && $uri === '/contacts-import') {
    $decoded = authenticate();
    $user_id   = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;

    $data = json_decode(file_get_contents('php://input'), true) ?? [];
    $rows = $data['contacts'] ?? [];
    $response = $contactsController->bulkImport($rows, $user_id);
    logAndRespond($user_id, $user_role, '/contacts-import', $response, 'POST', ['count' => count($rows)]);
}

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

    $response = $contactsController->getGroups();
    logAndRespond($user_id, $user_role, '/contacts-groups', $response, 'GET', []);
}

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

    $response = $contactsController->getStatistics();
    logAndRespond($user_id, $user_role, '/contacts-statistics', $response, 'GET', []);
}
