<?php
class Router {
    public function run() {

        try {
            $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
            $queryParams = $_GET;
            $method = $_SERVER['REQUEST_METHOD'];
            $path = rtrim(str_replace("/ApiV1", "", $uri), "/");
            header("Content-Type: application/json");

            
            $response = array();
            $contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
            
            // Handle different content types
            if (strpos($contentType, 'application/json') !== false) {
                $input = json_decode(file_get_contents('php://input'), true);
            } elseif (strpos($contentType, 'application/x-www-form-urlencoded') !== false) {
                $input = $_POST;
            } else {
                $rawInput = file_get_contents('php://input');
                $input = json_decode($rawInput, true);
                if ($input === null) {
                    $input = $_POST;
                }
            }

            $input = $input ?? array();

            switch ("$method $path") {
                case 'POST /auth/login':
                    require_once 'controllers/AuthController.php';
                    $controller = new AuthController();
                    echo json_encode($controller->login($input));
                    return;
                    break;

                case 'POST /accounts':
                    require_once 'controllers/AccountController.php';
                    $controller = new AccountController();
                    $response = $controller->create($input);
                    break;

                case 'POST /products':
                    require_once 'controllers/ProductController.php';
                    $controller = new ProductController();
                    $response = $controller->create($input);
                    break;

                case 'GET /products/stock':
                    require_once 'controllers/ProductController.php';
                    $controller = new ProductController();
                    $response = $controller->getStock();
                    break;

                case 'PUT /products':
                    require_once 'controllers/ProductController.php';
                    $controller = new ProductController();
                    $response = $controller->update($input);
                    break;

                case 'POST /orders':
                    require_once 'controllers/OrderController.php';
                    $controller = new OrderController();
                    $response = $controller->createOutgoing($input);
                    break;

                case 'POST /inbound-orders':
                    require_once 'controllers/OrderController.php';
                    $controller = new OrderController();
                    $response = $controller->createIncoming($input);
                    break;

                case 'GET /consult':
                    require_once 'controllers/ConsultController.php';
                    $controller = new ConsultController();
                    $response = $controller->search($queryParams);
                    break;
                case 'PUT /delete_OE':
                    require_once 'controllers/OrderController.php';
                    $controller = new OrderController();
                    $response = $controller->deleteOE($input);
                    break;
                case 'PUT /delete_OT':
                    require_once 'controllers/OrderController.php';
                    $controller = new OrderController();
                    $response = $controller->deleteOT($input);
                    break;    

                case 'PUT /accounts':
                case 'GET /orders':
                case 'GET /inbound-orders':
                case 'GET /boxes/status':
                    throw new Exception("Endpoint en construcción",404);
                    break;

                default:
                    throw new Exception("Ruta no encontrada",404);
            }

            echo json_encode(["status" => "success", "message" => "operacion exitosa", "data" => $response]);

            
        } catch (Exception $e) {
            if ($e->getCode() <> 0) {
                http_response_code($e->getCode());
            } else {
                http_response_code(500);
            }
            
            echo json_encode(["status" => "error", "message" => $e->getMessage()]);
        }

    }
}
