<?php
// ---------------------------------------------------------------
// TEAM MANAGEMENT ROUTES (Using Existing Structure)
// ---------------------------------------------------------------
require_once 'controllers/TeamManagementController.php';

$teamController = new TeamManagementController();

// LIST TEAM MEMBERS
if ($method === 'GET' && $uri === '/team-members') {
    $decoded = authenticate();
    $user_id = $decoded->user_id ?? null;
    $user_role = $decoded->user_type ?? null;

    $filters = $_GET;
    $response = $teamController->getTeamMembers($filters, $user_id, $user_role);
    logAndRespond($user_id, $user_role, '/team-members', $response, 'GET', $_GET);
}

// ASSIGN WORK (uses your /invoices-assign)
if ($method === 'POST' && $uri === '/team-assign-work') {
    $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) ?? [];
    $response = $teamController->assignWork($data, $user_id, $user_name);
    logAndRespond($user_id, $user_role, '/team-assign-work', $response, 'POST', $data);
}

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

    $response = $teamController->getAssignments($user_id, $user_role);
    logAndRespond($user_id, $user_role, '/team-assignments', $response, 'GET', []);
}

// SET TARGET
if ($method === 'POST' && $uri === '/team-set-target') {
    $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) ?? [];
    $response = $teamController->setTarget($data, $user_id, $user_name);
    logAndRespond($user_id, $user_role, '/team-set-target', $response, 'POST', $data);
}