<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->get('/', 'Home::index');
$routes->post('deposit', 'Deposit::index');
$routes->post('easy/deposit', 'Easy::index');
$routes->get('ip', 'Deposit::getIp');
$routes->post('callback', 'Deposit::callback');
$routes->get('transaction-status/(:segment)', 'Deposit::getTransactionStatus/$1');
$routes->post('deposit/custom', 'Deposit::custom');

// Auth routes
$routes->get('auth/login', 'Auth::login');
$routes->post('auth/login', 'Auth::login');
$routes->get('auth/logout', 'Auth::logout');

// Transactions routes
$routes->get('transactions', 'Transactions::index');
$routes->get('transactions/filter', 'Transactions::filter');
$routes->post('transactions/update-status/(:num)', 'Transactions::updateStatus/$1');

$routes->get('payment', 'PaymentController::index');

// PesaPal IPN and callback (public, no auth)
$routes->post('pesapal-ipn', 'PesaPal::ipn');
$routes->get('pesapal-callback', 'PesaPal::callback');
$routes->get('pesapal-result', 'PesaPal::result');

// PesaPal API Routes
$routes->group('api/pesapal', function ($routes) {
    $routes->post('order', 'PesaPal::order');
    $routes->get('status/(:segment)', 'PesaPal::status/$1');
    $routes->post('refund', 'PesaPal::refund');
    $routes->post('cancel', 'PesaPal::cancel');
    $routes->post('register-ipn', 'PesaPal::registerIpn');
    $routes->get('ipn-list', 'PesaPal::ipnList');
    $routes->get('transaction/(:segment)', 'PesaPal::transaction/$1');
});

       // Mobile Money API Routes - Simplified endpoints for in-house apps
       $routes->group('api/mobile-money', function($routes) {
           // Core payment endpoints
           $routes->post('pay', 'MobileMoney::pay');
           $routes->post('send', 'MobileMoney::send');
           $routes->post('validate', 'MobileMoney::validateMobile');
           $routes->post('payment-session', 'MobileMoney::paymentSession');
           $routes->get('balance', 'MobileMoney::balance');
           $routes->get('history', 'MobileMoney::history');
           $routes->get('status/(:segment)', 'MobileMoney::getStatus/$1');
           $routes->get('check-status/(:segment)', 'MobileMoney::checkRequestStatus/$1');
           $routes->post('callback', 'MobileMoney::callback');
           $routes->post('webhook/relworx', 'MobileMoney::relworxWebhook');
           $routes->get('health', 'MobileMoney::health');
           $routes->get('debug/transactions', 'MobileMoney::debugTransactions');
           
           // Service catalog endpoints
           $routes->get('services', 'MobileMoney::services');
           $routes->get('services/categorized', 'MobileMoney::categorizedServices');
           $routes->get('services/price-list/(:segment)', 'MobileMoney::servicePriceList/$1');
            $routes->post('validate-product', 'MobileMoney::validateProduct');
            $routes->post('validate-bank', 'MobileMoney::validateBank');
            $routes->post('purchase-product', 'MobileMoney::purchaseProduct');
    
    // Transaction management endpoints
    $routes->get('transaction/(:segment)', 'MobileMoney::getTransactionDetails/$1');
    $routes->post('chargeback', 'MobileMoney::initiateChargeback');
    $routes->put('chargeback/(:segment)', 'MobileMoney::updateChargebackStatus/$1');
    $routes->post('refund', 'MobileMoney::processRefund');
    $routes->put('refund/(:segment)', 'MobileMoney::updateRefundStatus/$1');
    $routes->get('chargebacks', 'MobileMoney::getChargebacks');
    $routes->get('refunds', 'MobileMoney::getRefunds');
    $routes->get('report', 'MobileMoney::generateReport');
});

// Cardyfie API Routes - Alias for 'live' environment (maps api/live/* to api/cardyfie/*)
// This must come BEFORE api/cardyfie group for proper route matching when environment is "live"
$routes->group('api/live', function($routes) {
    // Card endpoints (v1) - Alias routes that map to Cardyfie controller
    $routes->get('v1/card/currencies', 'Cardyfie::getCardCurrencies');
    $routes->post('v1/card/issue', 'Cardyfie::issueCard');
    $routes->get('v1/card/get-all', 'Cardyfie::getCards');
    $routes->get('v1/card/details/(:segment)', 'Cardyfie::getCardDetails/$1');
    $routes->get('v1/card/details', 'Cardyfie::getCardDetails');
    $routes->post('v1/card/deposit/(:segment)', 'Cardyfie::depositToCard/$1');
    $routes->post('v1/card/deposit', 'Cardyfie::depositToCard');
    $routes->post('v1/card/withdraw/(:segment)', 'Cardyfie::withdrawFromCard/$1');
    $routes->post('v1/card/withdraw', 'Cardyfie::withdrawFromCard');
    $routes->get('v1/card/transactions', 'Cardyfie::getCardTransactions');
    $routes->post('v1/card/freeze/(:segment)', 'Cardyfie::freezeCard/$1');
    $routes->post('v1/card/freeze', 'Cardyfie::freezeCard');
    $routes->post('v1/card/unfreeze/(:segment)', 'Cardyfie::unfreezeCard/$1');
    $routes->post('v1/card/unfreeze', 'Cardyfie::unfreezeCard');
    $routes->post('v1/card/close/(:segment)', 'Cardyfie::closeCard/$1');
    $routes->post('v1/card/close', 'Cardyfie::closeCard');
});

// Cardyfie API Routes
$routes->group('api/cardyfie', function($routes) {
    // Customer endpoints
    $routes->post('customer/create', 'Cardyfie::createCustomer');
    $routes->get('customer/get/(:segment)', 'Cardyfie::getCustomer/$1'); // ULID as path parameter
    $routes->get('customer/get', 'Cardyfie::getCustomer'); // Also support query parameter for flexibility
    $routes->put('customer/update/(:segment)', 'Cardyfie::updateCustomer/$1'); // ULID as path parameter
    $routes->put('customer/update', 'Cardyfie::updateCustomer'); // Also support ULID in body for flexibility
    
    // Card endpoints (v1)
    $routes->get('v1/card/currencies', 'Cardyfie::getCardCurrencies');
    $routes->post('v1/card/issue', 'Cardyfie::issueCard');
    $routes->get('v1/card/get-all', 'Cardyfie::getCards');
    $routes->get('v1/card/details/(:segment)', 'Cardyfie::getCardDetails/$1'); // ULID as path parameter
    $routes->get('v1/card/details', 'Cardyfie::getCardDetails'); // Also support query parameter for flexibility
    $routes->post('v1/card/deposit/(:segment)', 'Cardyfie::depositToCard/$1'); // ULID as path parameter
    $routes->post('v1/card/deposit', 'Cardyfie::depositToCard'); // Also support ULID in body for flexibility
    $routes->post('v1/card/withdraw/(:segment)', 'Cardyfie::withdrawFromCard/$1'); // ULID as path parameter
    $routes->post('v1/card/withdraw', 'Cardyfie::withdrawFromCard'); // Also support ULID in body for flexibility
    $routes->get('v1/card/transactions', 'Cardyfie::getCardTransactions'); // Optional query param: ?card_ulid={ulid}
    $routes->post('v1/card/freeze/(:segment)', 'Cardyfie::freezeCard/$1'); // ULID as path parameter
    $routes->post('v1/card/freeze', 'Cardyfie::freezeCard'); // Also support ULID in body for flexibility
    $routes->post('v1/card/unfreeze/(:segment)', 'Cardyfie::unfreezeCard/$1'); // ULID as path parameter
    $routes->post('v1/card/unfreeze', 'Cardyfie::unfreezeCard'); // Also support ULID in body for flexibility
    $routes->post('v1/card/close/(:segment)', 'Cardyfie::closeCard/$1'); // ULID as path parameter
    $routes->post('v1/card/close', 'Cardyfie::closeCard'); // Also support ULID in body for flexibility
    
    // Webhook endpoint
    $routes->post('webhook', 'Cardyfie::webhook'); // Receives webhooks from Cardyfie
    
    // Health check
    $routes->get('health', 'Cardyfie::health');
});

// YEMScan / YEMChain API Routes - Simplified wrapper for yemscan.com API
$routes->group('api/yemscan', function ($routes) {
    $routes->post('transactions', 'Yemscan::createTransactions');
    $routes->post('transaction', 'Yemscan::createTransactions'); // alias (singular) for POST only
    $routes->get('uid', 'Yemscan::getUid');
    $routes->get('balances', 'Yemscan::getBalances');
    $routes->get('globals', 'Yemscan::getGlobals');
    $routes->get('volume', 'Yemscan::getVolume');
    $routes->get('transaction-statuses', 'Yemscan::getTransactionStatuses');
    $routes->get('asset-stats', 'Yemscan::getAssetStats');
    $routes->get('getAssetStats', 'Yemscan::getAssetStats'); // alias for upstream API naming
    $routes->get('public-key/(:num)', 'Yemscan::getPublicKey/$1');
    $routes->get('public-key', 'Yemscan::getPublicKey');
    $routes->get('transaction/verify', 'Yemscan::verifyTransaction');
    $routes->get('transaction/(:segment)/verify', 'Yemscan::verifyTransaction/$1');
    $routes->get('transaction/(:segment)', 'Yemscan::getTransaction/$1');
    $routes->get('transaction', 'Yemscan::getTransaction');
    $routes->get('validate-access', 'Yemscan::validateAccess');
    $routes->get('third-party-access', 'Yemscan::validateAccess'); // alias
    $routes->get('health', 'Yemscan::health');
    $routes->get('cron/refresh-statuses', 'Yemscan::cronRefreshStatuses');
});

// Paytota API Routes - Simplified wrapper for gate.paytota.com
$routes->group('api/paytota', function ($routes) {
    $routes->post('checkout', 'Paytota::checkout');
    $routes->get('balance', 'Paytota::balance');
    $routes->get('purchase/status/(:segment)', 'Paytota::purchaseStatus/$1');
    $routes->post('payout', 'Paytota::payout');
    $routes->post('payout/execute', 'Paytota::payoutExecute');
    $routes->get('payout/status/(:segment)', 'Paytota::payoutStatus/$1');
    $routes->post('webhook', 'Paytota::webhook');
    $routes->post('stk', 'Paytota::stk');
    $routes->get('health', 'Paytota::health');
});

// NOWPayments API Routes - proxy for api.nowpayments.io/v1
$routes->group('api/nowpayments', function ($routes) {
    $routes->get('health', 'NowPayments::health');
    $routes->post('ipn', 'NowPayments::ipn');
    $routes->post('payment', 'NowPayments::createPayment');
    $routes->get('payment/(:segment)', 'NowPayments::paymentStatus/$1');
    $routes->get('balance', 'NowPayments::balance');
    $routes->post('payout/validate-address', 'NowPayments::payoutValidateAddress');
    $routes->post('payout', 'NowPayments::payoutCreate');
    $routes->post('payout/(:segment)/verify', 'NowPayments::payoutVerify/$1');
    $routes->get('payout/(:segment)', 'NowPayments::payoutStatus/$1');
    $routes->get('fiat-payouts/providers', 'NowPayments::fiatProviders');
    $routes->get('fiat-payouts/fiat-currencies', 'NowPayments::fiatCurrencies');
    $routes->get('fiat-payouts/crypto-currencies', 'NowPayments::fiatCryptoCurrencies');
    $routes->get('fiat-payouts/payment-methods', 'NowPayments::fiatPaymentMethods');
    $routes->post('fiat-payouts/account', 'NowPayments::fiatCreateAccount');
    $routes->post('fiat-payouts', 'NowPayments::fiatRequestPayout');
    $routes->get('fiat-payouts', 'NowPayments::fiatList');
});

// API Documentation
$routes->get('api/docs', function() {
    return view('api_documentation');
});

// Admin API (JSON) - app login uses admin/auth (same as web). Other API routes below.
$routes->post('api/admin/logout', 'Api\AdminApiController::logout', ['filter' => 'auth']);
$routes->get('api/admin/dashboard', 'Api\AdminApiController::dashboard', ['filter' => 'auth']);
$routes->get('api/admin/cardyfie-cards', 'Api\AdminApiController::cardyfieCards', ['filter' => 'auth']);
$routes->get('api/admin/cardyfie-card', 'Api\AdminApiController::cardyfieCard', ['filter' => 'auth']);
$routes->get('api/admin/cardyfie-transactions', 'Api\AdminApiController::cardyfieTransactions', ['filter' => 'auth']);
$routes->get('api/admin/yem', 'Api\AdminApiController::yem', ['filter' => 'auth']);
$routes->post('api/admin/yem/refresh-status', 'Api\AdminApiController::yemRefreshStatus', ['filter' => 'auth']);

//Admin Dashoard
$routes->get('admin/login', 'AdminController::login');
$routes->post('admin/auth', 'AdminController::auth');
$routes->get('admin/dashboard', 'AdminController::dashboard', ['filter' => 'auth']);
$routes->get('admin/transactions', 'AdminController::transactions', ['filter' => 'auth']);
$routes->get('admin/relworx-transactions', 'AdminController::relworxTransactions', ['filter' => 'auth']);
$routes->get('admin/disbursements', 'AdminController::disbursements', ['filter' => 'auth']);
$routes->get('admin/nowpayments', 'AdminController::nowpayments', ['filter' => 'auth']);
$routes->get('admin/transactions/filter', 'AdminController::filterTransactions', ['filter' => 'auth']);
$routes->get('admin/relworx-transactions/filter', 'AdminController::filterRelworxTransactions', ['filter' => 'auth']);
$routes->get('admin/transactions/export', 'AdminController::exportTransactions', ['filter' => 'auth']);
$routes->get('admin/relworx-transactions/export', 'AdminController::exportRelworxTransactions', ['filter' => 'auth']);
$routes->post('admin/transactions/update-status/(:segment)', 'Transactions::updateStatus/$1', ['filter' => 'auth']);
$routes->post('admin/transactions/delete-failed', 'AdminController::deleteFailedTransactions', ['filter' => 'auth']);
$routes->post('admin/transactions/mark-pending-failed', 'AdminController::markPendingAsFailed', ['filter' => 'auth']);
$routes->post('admin/relworx-transactions/update-status/(:segment)', 'AdminController::updateRelworxStatus/$1', ['filter' => 'auth']);
$routes->post('admin/relworx-transactions/delete-failed', 'AdminController::deleteFailedRelworxTransactions', ['filter' => 'auth']);
$routes->post('admin/relworx-transactions/mark-pending-failed', 'AdminController::markPendingRelworxAsFailed', ['filter' => 'auth']);
$routes->get('admin/logout', 'AdminController::logout');

// Cardyfie Admin Routes
$routes->get('admin/cardyfie-customers', 'AdminController::cardyfieCustomers', ['filter' => 'auth']);
$routes->get('admin/yem', 'AdminController::yem', ['filter' => 'auth']);
$routes->post('admin/yem/refresh-status', 'AdminController::yemRefreshStatus', ['filter' => 'auth']);
$routes->get('admin/cardyfie-cards', 'AdminController::cardyfieCards', ['filter' => 'auth']);
$routes->get('admin/cardyfie-transactions', 'AdminController::cardyfieTransactions', ['filter' => 'auth']);
$routes->get('admin/cardyfie-card/(:segment)', 'AdminController::cardyfieCardDetails/$1', ['filter' => 'auth']);
$routes->get('admin/cardyfie-card', 'AdminController::cardyfieCardDetails', ['filter' => 'auth']);
$routes->post('admin/cardyfie-card-action', 'AdminController::cardyfieCardAction', ['filter' => 'auth']);
