<?php
/**
 * BillBox OTP API - Main Entry Point
 * Handles routing and API documentation
 */

require_once 'config/config.php';

// Simple routing
$request_uri = $_SERVER['REQUEST_URI'];
$path = parse_url($request_uri, PHP_URL_PATH);
$path = str_replace('/api', '', $path);

// Remove trailing slash
$path = rtrim($path, '/');

// Route to appropriate endpoint
switch ($path) {
    case '':
    case '/':
        showApiDocumentation();
        break;
        
    case '/auth/google':
        require_once 'auth/google_auth.php';
        break;
        
    case '/auth/email':
    case '/auth/email_auth.php':
        require_once 'auth/email_auth.php';
        break;
        
    case '/user/profile':
    case '/user/profile.php':
        require_once 'user/profile.php';
        break;
        
    case '/products':
        require_once 'products/products.php';
        break;
        
    case '/categories':
        require_once 'categories/categories.php';
        break;
        
    case '/orders':
        require_once 'orders/orders.php';
        break;
        
    case '/settings':
        require_once 'settings/settings.php';
        break;
        
    default:
        sendError('Endpoint not found', 404);
}

function showApiDocumentation() {
    $documentation = [
        'api_name' => APP_NAME,
        'version' => '1.0.0',
        'base_url' => BASE_URL,
        'endpoints' => [
            'authentication' => [
                'POST /auth/google' => 'Authenticate with Google OAuth',
                'POST /auth/email' => 'Authenticate with email/password (action: login/register)'
            ],
            'user' => [
                'GET /user/profile' => 'Get user profile',
                'PUT /user/profile' => 'Update user profile',
                'DELETE /user/profile' => 'Delete user profile'
            ],
            'products' => [
                'GET /products' => 'Get all products',
                'POST /products' => 'Create new product',
                'GET /products/{id}' => 'Get product by ID',
                'PUT /products/{id}' => 'Update product',
                'DELETE /products/{id}' => 'Delete product'
            ],
            'categories' => [
                'GET /categories' => 'Get all categories',
                'POST /categories' => 'Create new category',
                'GET /categories/{id}' => 'Get category by ID',
                'PUT /categories/{id}' => 'Update category',
                'DELETE /categories/{id}' => 'Delete category'
            ],
            'orders' => [
                'GET /orders' => 'Get all orders',
                'POST /orders' => 'Create new order',
                'GET /orders/{id}' => 'Get order by ID',
                'PUT /orders/{id}' => 'Update order',
                'DELETE /orders/{id}' => 'Delete order'
            ],
            'settings' => [
                'GET /settings' => 'Get user settings',
                'PUT /settings' => 'Update user settings'
            ]
        ],
        'authentication' => [
            'type' => 'Bearer Token',
            'description' => 'Include Authorization header with Bearer token for authenticated endpoints'
        ],
        'response_format' => [
            'success' => 'boolean',
            'message' => 'string',
            'data' => 'object|array',
            'timestamp' => 'string (ISO 8601)'
        ]
    ];
    
    sendResponse($documentation, 200, 'API Documentation');
}
?>
