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

$performanceController = new PerformanceController();

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

// Routes
if ($method === 'GET' && $uri === '/performance-summary') {
    $response = $performanceController->getSummaryMetrics($_GET, $user_id);
    logAndRespond($user_id, $user_role, '/performance-summary', $response, 'GET', $_GET);
}

if ($method === 'GET' && $uri === '/performance-targets') {
    $response = $performanceController->getTargets();
    logAndRespond($user_id, $user_role, '/performance-targets', $response, 'GET', $_GET);
}

if ($method === 'POST' && $uri === '/performance-targets') {
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $performanceController->createTarget($data);
    logAndRespond($user_id, $user_role, '/performance-targets', $response, 'POST', $data);
}

if ($method === 'PUT' && $uri === '/performance-targets') {
    $id = $_GET['id'];
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $performanceController->updateTarget($id, $data);
    logAndRespond($user_id, $user_role, "/performance-targets?id=$id", $response, 'PUT', $data);
}

if ($method === 'DELETE' && $uri === '/performance-targets') {
    $id = $_GET['id'];
    $response = $performanceController->deleteTarget($id);
    logAndRespond($user_id, $user_role, "/performance-targets?id=$id", $response, 'DELETE', []);
}

if ($method === 'GET' && $uri === '/performance-reviews') {
    $response = $performanceController->getReviews();
    logAndRespond($user_id, $user_role, '/performance-reviews', $response, 'GET', $_GET);
}

if ($method === 'PUT' && $uri === '/performance-reviews-conduct') {
    $id = $_GET['id'];
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $performanceController->conductReview($id, $data);
    logAndRespond($user_id, $user_role, "performance-reviews-conduct", $response, 'PUT', $data);
}

if ($method === 'GET' && $uri === '/performance-kpis') {
    $response = $performanceController->getKpiSettings();
    logAndRespond($user_id, $user_role, '/performance-kpis', $response, 'GET', $_GET);
}

// Router continuation in api.php or index.php
if ($method === 'PUT' && $uri === '/performance-kpis') {
    $data = json_decode(file_get_contents('php://input'), true);
    $response = $performanceController->updateKpiSettings($data, $user_id);
    logAndRespond($user_id, $user_role, '/performance-kpis', $response, 'PUT', $data);
}

if ($method === 'GET' && $uri === '/performance-analytics') {
    $response = $performanceController->getTeamAnalytics($_GET);
    logAndRespond($user_id, $user_role, '/performance-analytics', $response, 'GET', $_GET);
}

