<?php
require_once __DIR__ . '/../controllers/ProjectController.php';

function handleProjectRoutes($path, $method, $db) {
    $controller = new ProjectController($db);
    
    // Remover 'projects/' del path
    $path = preg_replace('#^projects/?#', '', $path);
    
    // Parsear path y parámetros
    $pathParts = explode('/', trim($path, '/'));
    $id = null;
    $action = null;
    $subAction = null;
    
    if (!empty($pathParts[0]) && is_numeric($pathParts[0])) {
        $id = (int)$pathParts[0];
        $action = $pathParts[1] ?? null;
        $subAction = $pathParts[2] ?? null;
    } elseif (!empty($pathParts[0])) {
        $action = $pathParts[0];
    }
    
    try {
        switch ($method) {
            case 'GET':
                if (empty($path) || $path === '/') {
                    // GET /api/projects
                    return $controller->index();
                } elseif ($action === 'departments') {
                    // GET /api/projects/departments
                    return $controller->getDepartments();
                } elseif ($id && !$action) {
                    // GET /api/projects/{id}
                    return $controller->show($id);
                } elseif ($id && $action === 'documents' && $subAction) {
                    // GET /api/projects/{id}/documents/{type}
                    return $controller->downloadDocument($id, $subAction);
                } else {
                    http_response_code(404);
                    return json_encode([
                        'success' => false,
                        'message' => 'Endpoint no encontrado'
                    ]);
                }
                break;
                
            case 'POST':
                if (empty($path) || $path === '/') {
                    // POST /api/projects
                    return $controller->store();
                } elseif ($id && $action === 'documents') {
                    // POST /api/projects/{id}/documents
                    return $controller->uploadDocument($id);
                } else {
                    http_response_code(404);
                    return json_encode([
                        'success' => false,
                        'message' => 'Endpoint no encontrado'
                    ]);
                }
                break;
                
            case 'PUT':
                if ($id && !$action) {
                    // PUT /api/projects/{id}
                    return $controller->update($id);
                } else {
                    http_response_code(404);
                    return json_encode([
                        'success' => false,
                        'message' => 'Endpoint no encontrado'
                    ]);
                }
                break;
                
            case 'DELETE':
                if ($id && !$action) {
                    // DELETE /api/projects/{id}
                    return $controller->destroy($id);
                } else {
                    http_response_code(404);
                    return json_encode([
                        'success' => false,
                        'message' => 'Endpoint no encontrado'
                    ]);
                }
                break;
                
            default:
                http_response_code(405);
                return json_encode([
                    'success' => false,
                    'message' => 'Método no permitido'
                ]);
        }
        
    } catch (Exception $e) {
        error_log("Project Route Error: " . $e->getMessage());
        http_response_code(500);
        return json_encode([
            'success' => false,
            'message' => 'Error interno del servidor'
        ]);
    }
}
?>