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

$invoicesController = new InvoicesController();

// Authenticate for all other routes


// Invoice Management Routes
if ($method === 'GET' && $uri === '/invoices-open') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $response = $invoicesController->getAllOpenInvoices($_GET);
    logAndRespond($user_id, $user_type, '/invoices-open', $response, 'GET', $_GET);
}

if ($method === 'GET' && $uri === '/invoices-active') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $response = $invoicesController->getActiveInvoices($_GET);
    logAndRespond($user_id, $user_type, '/invoices-active', $response, 'GET', $_GET);
}

if ($method === 'GET' && $uri === '/invoices-closed') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $response = $invoicesController->getClosedInvoices($_GET);
    logAndRespond($user_id, $user_type, '/invoices-closed', $response, 'GET', $_GET);
}

if ($method === 'POST' && $uri === '/invoices-assign') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $invoiceId = $_GET['id'] ?? null;
    $data = json_decode(file_get_contents('php://input'), true);
    $data['id'] = $invoiceId;
    $response = $invoicesController->assignInvoices($data);
    logAndRespond($user_id, $user_type, $uri, $response, 'POST', $data);
}

if ($method === 'GET' && $uri === '/invoice-statistics') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $response = $invoicesController->getInvoiceStatistics($_GET);
    logAndRespond($user_id, $user_type, '/invoice-statistics', $response, 'GET', []);
}
 
if ($method === 'GET' && $uri === '/team-invoice-statistics') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_type = $decoded->user_type ?? null;
    $response = $invoicesController->getTeamInvoiceStatistics($_GET);
    logAndRespond($user_id, $user_type, '/team-invoice-statistics', $response, 'GET', []);
}