<?php
require_once 'config/headers.php';
header('Content-Type: application/json');
$request_method = $_SERVER['REQUEST_METHOD'];
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path_parts = explode('/', $path);

if ($path_parts[1] == 'api') {
    switch ($path_parts[2]) {
        case 'geocode':
            if ($request_method == 'GET') {
                include 'calls/geocode.php';
            }
            break;
        case 'get_items':
            if ($request_method == 'GET') {
                include 'calls/get_items.php';
            }
            break;
        case 'get_item':
            if ($request_method == 'GET') {
                include 'calls/get_item.php';
            }
            break;
        case 'create_item':
            if ($request_method == 'POST') {
                include 'calls/create_item.php';
            }
            break;
        case 'update_item':
            if ($request_method == 'PUT') {
                include 'calls/update_item.php';
            }
            break;
        case 'delete_item':
            if ($request_method == 'DELETE') {
                include 'calls/delete_item.php';
            }
            break;
        case 'check':
            if ($request_method == 'GET') {
                include 'auth/check_session.php';
            }
            break;
        case 'login':
            if ($request_method == 'POST') {
                include 'auth/login.php';
            }
            break;
        case 'logout':
            if ($request_method == 'POST') {
                include 'auth/logout.php';
            }
            break;
        case 'register':
            if ($request_method == 'POST') {
                include 'auth/register.php';
            }
            break;            
        default:
            http_response_code(404);
            echo json_encode(['message' => 'Endpoint not found']);
    }
} else {
    http_response_code(400);
    echo json_encode(['message' => 'Invalid request']);
}
