<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { http_response_code(200); exit(); }
header("Content-Type: application/json; charset=UTF-8");

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode('/', $uri);
$apiIndex = array_search('api.php', $uri);
$route    = isset($uri[$apiIndex + 1]) ? $uri[$apiIndex + 1] : '';
$subroute = isset($uri[$apiIndex + 2]) ? $uri[$apiIndex + 2] : '';

include_once './controllers/AuthController.php';
include_once './controllers/BankingController.php';
include_once './controllers/AdminController.php';
include_once './controllers/PaymentController.php';

$authController    = new AuthController();
$bankingController = new BankingController();
$adminController   = new AdminController();
$paymentController = new PaymentController();

$data = json_decode(file_get_contents("php://input"));

switch ($route) {
    case 'login':
        $_SERVER['REQUEST_METHOD'] === 'POST' ? $authController->login($data) : http_response_code(405);
        break;
    case 'register':
        $_SERVER['REQUEST_METHOD'] === 'POST' ? $authController->register($data) : http_response_code(405);
        break;

    case 'user':
        if ($subroute === 'cards') {
            $_SERVER['REQUEST_METHOD'] === 'POST' ? $bankingController->createCard($data) : http_response_code(405);
        } else {
            $_SERVER['REQUEST_METHOD'] === 'GET' ? $bankingController->getUserData() : http_response_code(405);
        }
        break;

    case 'transfer':
        $_SERVER['REQUEST_METHOD'] === 'POST' ? $bankingController->transferMoney($data) : http_response_code(405);
        break;

    case 'loan':
        if ($_SERVER['REQUEST_METHOD'] === 'GET') $bankingController->getLoans();
        else if ($_SERVER['REQUEST_METHOD'] === 'POST') $bankingController->applyLoan($data);
        else http_response_code(405);
        break;

    case 'deposit':
        $_SERVER['REQUEST_METHOD'] === 'POST' ? $bankingController->depositMoney($data) : http_response_code(405);
        break;

    case 'pay':
        if ($_SERVER['REQUEST_METHOD'] === 'POST') $paymentController->pay($data);
        else if ($_SERVER['REQUEST_METHOD'] === 'GET') $paymentController->getPayments();
        else http_response_code(405);
        break;

    case 'notifications':
        if ($_SERVER['REQUEST_METHOD'] === 'GET') $paymentController->getNotifications();
        else if ($_SERVER['REQUEST_METHOD'] === 'PUT') $paymentController->markNotificationsRead();
        else http_response_code(405);
        break;

    case 'cards':
        if ($subroute === 'block') {
            $_SERVER['REQUEST_METHOD'] === 'PUT' ? $paymentController->blockCard($data) : http_response_code(405);
        } else if ($subroute === 'limit') {
            $_SERVER['REQUEST_METHOD'] === 'PUT' ? $paymentController->updateCardLimit($data) : http_response_code(405);
        } else http_response_code(404);
        break;

    case 'admin':
        if ($subroute === 'users') {
            if ($_SERVER['REQUEST_METHOD'] === 'GET') $adminController->getAllUsers();
            else if ($_SERVER['REQUEST_METHOD'] === 'POST') $adminController->createUser($data);
            else if ($_SERVER['REQUEST_METHOD'] === 'PUT') $adminController->updateUser($data);
            else if ($_SERVER['REQUEST_METHOD'] === 'DELETE') $adminController->deleteUser($data);
            else http_response_code(405);
        } else if ($subroute === 'transactions' && $_SERVER['REQUEST_METHOD'] === 'GET') {
            $adminController->getAllTransactions();
        } else if ($subroute === 'accounts') {
            if ($_SERVER['REQUEST_METHOD'] === 'GET') $adminController->getAllAccounts();
            else if ($_SERVER['REQUEST_METHOD'] === 'POST') $adminController->createAccount($data);
            else if ($_SERVER['REQUEST_METHOD'] === 'PUT') $adminController->blockAccount($data);
            else http_response_code(405);
        } else if ($subroute === 'stats' && $_SERVER['REQUEST_METHOD'] === 'GET') {
            $adminController->getStats();
        } else if ($subroute === 'cards' && $_SERVER['REQUEST_METHOD'] === 'GET') {
            $adminController->getAllCards();
        } else if ($subroute === 'loans') {
            if ($_SERVER['REQUEST_METHOD'] === 'GET') $adminController->getAllLoans();
            else if ($_SERVER['REQUEST_METHOD'] === 'PUT') $adminController->updateLoanStatus($data);
            else http_response_code(405);
        } else if ($subroute === 'notes') {
            if ($_SERVER['REQUEST_METHOD'] === 'GET') $adminController->getNotes();
            else if ($_SERVER['REQUEST_METHOD'] === 'POST') $adminController->createNote($data);
            else if ($_SERVER['REQUEST_METHOD'] === 'PUT') $adminController->updateNote($data);
            else if ($_SERVER['REQUEST_METHOD'] === 'DELETE') $adminController->deleteNote($data);
            else http_response_code(405);
        } else if ($subroute === 'settings') {
            if ($_SERVER['REQUEST_METHOD'] === 'GET') $adminController->getSettings();
            else if ($_SERVER['REQUEST_METHOD'] === 'PUT') $adminController->updateSettings($data);
            else http_response_code(405);
        } else if ($subroute === 'logs' && $_SERVER['REQUEST_METHOD'] === 'GET') {
            $adminController->getLogs();
        } else if ($subroute === 'balance' && $_SERVER['REQUEST_METHOD'] === 'PUT') {
            $adminController->adjustBalance($data);
        } else {
            http_response_code(404);
            echo json_encode(["message" => "Admin route not found."]);
        }
        break;

    default:
        http_response_code(404);
        echo json_encode(["message" => "Route not found.", "route" => $route]);
        break;
}
?>
