<?php
/**
 * TellerPro — Super Admin Entry Point
 */

require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/core/Security.php';
require_once __DIR__ . '/core/Auth.php';
require_once __DIR__ . '/core/SuperAuth.php';
require_once __DIR__ . '/core/Model.php';
require_once __DIR__ . '/core/Router.php';
require_once __DIR__ . '/core/helpers.php';

spl_autoload_register(function(string $class) {
    $paths = [
        __DIR__ . '/models/' . $class . '.php',
        __DIR__ . '/controllers/superadmin/' . $class . '.php',
    ];
    foreach ($paths as $path) {
        if (file_exists($path)) { require_once $path; return; }
    }
});

SuperAuth::startSession();

$uri    = $_SERVER['REQUEST_URI'];
$base   = '/TellerPro/superadmin';
$uri    = substr($uri, strlen($base)) ?: '/';
$method = $_SERVER['REQUEST_METHOD'];

$router = new Router();

$router->get('/',              [SADashboardController::class, 'index']);
$router->get('/login',         [SAAuthController::class, 'showLogin']);
$router->post('/login',        [SAAuthController::class, 'login']);
$router->get('/logout',        [SAAuthController::class, 'logout']);
$router->get('/reset-password', [SAAuthController::class, 'showPasswordReset']);
$router->post('/reset-password', [SAAuthController::class, 'resetPassword']);

$router->get('/tenants',            [SATenantsController::class, 'index']);
$router->get('/tenants/{id}',       [SATenantsController::class, 'show']);
$router->post('/tenants/{id}/suspend',  [SATenantsController::class, 'suspend']);
$router->post('/tenants/{id}/activate', [SATenantsController::class, 'activate']);
$router->post('/tenants/{id}/login-as-admin', [SATenantsController::class, 'loginAsAdmin']);
$router->post('/tenants/{id}/reset-password', [SATenantsController::class, 'resetPassword']);
$router->post('/tenants/{id}/delete',   [SATenantsController::class, 'destroy']);

$router->get('/plans',          [SAPlansController::class, 'index']);
$router->get('/plans/create',   [SAPlansController::class, 'create']);
$router->post('/plans/create',  [SAPlansController::class, 'store']);
$router->get('/plans/{id}/edit',  [SAPlansController::class, 'edit']);
$router->post('/plans/{id}/edit', [SAPlansController::class, 'update']);

$router->get('/subscriptions',  [SASubscriptionsController::class, 'index']);
$router->post('/subscriptions/{id}/cancel', [SASubscriptionsController::class, 'cancel']);
$router->post('/subscriptions/{id}/approve', [SASubscriptionsController::class, 'approve']);

// Paystack Settings
$router->get('/paystack',           [SAPaystackController::class, 'index']);
$router->post('/paystack/update',   [SAPaystackController::class, 'update']);
$router->post('/paystack/update-global', [SAPaystackController::class, 'updateGlobal']);
$router->post('/paystack/test',     [SAPaystackController::class, 'testConnection']);

$router->dispatch($uri, $method);
