<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/config/helpers.php';

setCorsHeaders();

$uri    = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$base   = '/Triangle_Tree_Life_Number/api';
if (strpos($uri, $base) === 0) {
    $uri = substr($uri, strlen($base));
}
// Strip index.php if present
$uri = preg_replace('#^/index\.php#', '', $uri);
$uri = '/' . ltrim($uri ?: '/', '/');
$method = $_SERVER['REQUEST_METHOD'];

// Load all route handlers
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/subscription.php';
require_once __DIR__ . '/data.php';
require_once __DIR__ . '/admin.php';

// Route matching
switch (true) {

    // Health
    case $method === 'GET' && $uri === '/health':
    case $method === 'GET' && $uri === '/':
        respond(['status' => 'ok', 'db' => 'mysql', 'timestamp' => date('c')]);
        break;

    // Auth
    case $method === 'POST' && $uri === '/auth/register':        authRegister(); break;
    case $method === 'POST' && $uri === '/auth/login':           authLogin(); break;
    case $method === 'POST' && $uri === '/auth/verify-email':    authVerifyEmail(); break;
    case $method === 'POST' && $uri === '/auth/verify-phone':    authVerifyPhone(); break;
    case $method === 'POST' && $uri === '/auth/resend-otp':      authResendOTP(); break;
    case $method === 'GET'  && $uri === '/auth/me':              authMe(); break;
    case $method === 'POST' && $uri === '/auth/refresh':         authRefresh(); break;
    case $method === 'POST' && $uri === '/auth/forgot-password': authForgot(); break;
    case $method === 'POST' && $uri === '/auth/reset-password':  authReset(); break;

    // Subscription
    case $method === 'GET'  && $uri === '/subscription/plans':   subPlans(); break;
    case $method === 'GET'  && $uri === '/subscription/status':  subStatus(); break;
    case $method === 'POST' && $uri === '/subscription/checkout':subCheckout(); break;
    case $method === 'GET'  && preg_match('#^/subscription/verify/(.+)$#', $uri, $m):
        subVerifySession($m[1]); break;

    // Data
    case $method === 'GET'  && $uri === '/data/descriptions':    dataGetDescriptions(); break;
    case $method === 'POST' && $uri === '/data/descriptions':    dataSaveDescriptions(); break;
    case $method === 'GET'  && $uri === '/data/languages':       dataGetLanguages(); break;
    case $method === 'POST' && $uri === '/data/languages':       dataSaveLanguages(); break;
    case $method === 'DELETE' && preg_match('#^/data/descriptions/([^/]+)/(\d+)$#', $uri, $m):
        dataDeleteDescription([$m[1], $m[2]]); break;

    // Admin
    case $method === 'GET'   && $uri === '/admin/stats':         adminStats(); break;
    case $method === 'GET'   && $uri === '/admin/users':         adminUsers(); break;
    case $method === 'GET'   && $uri === '/admin/settings':      adminSettings(); break;
    case $method === 'PATCH' && $uri === '/admin/settings':      adminSaveSettings(); break;
    case $method === 'GET'   && $uri === '/admin/payments':      adminPayments(); break;
    case $method === 'GET'   && preg_match('#^/admin/users/(\d+)$#', $uri, $m):
        adminGetUser($m[1]); break;
    case $method === 'PATCH' && preg_match('#^/admin/users/(\d+)$#', $uri, $m):
        adminUpdateUser($m[1]); break;
    case $method === 'POST'  && preg_match('#^/admin/users/(\d+)/extend$#', $uri, $m):
        adminExtendSub($m[1]); break;
    case $method === 'POST'  && preg_match('#^/admin/users/(\d+)/revoke$#', $uri, $m):
        adminRevokeSub($m[1]); break;

    default:
        respond(['error' => 'Not found', 'uri' => $uri, 'method' => $method], 404);
}
