<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (file_exists(SYSTEMPATH . 'Config/Routes.php')) {
    require SYSTEMPATH . 'Config/Routes.php';
}

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');
$routes->get('dashboard', 'Dashboard::index');
$routes->get('shopkeeper', 'Frontend\Shopkeeper\Dashboard::index');
$routes->get('user', 'Frontend\User\Dashboard::index');
$routes->get('whatsapp-test', 'Frontend\WhatsApp::index');
$routes->post('whatsapp-test/send', 'Frontend\WhatsApp::send');
$routes->get('whatsapp-test/result', 'Frontend\WhatsApp::result');
$routes->get('shop/(:num)', 'Frontend\Shop::view/$1');
$routes->get('shop/(:num)/bill', 'Frontend\Shop::bill/$1');

$routes->get('payments/success', 'Payments::success');
$routes->post('payments/offline/request', 'OfflineBills::create');
$routes->get('payments/offline/qr/(:segment)', 'OfflineBills::qr/$1');
$routes->get('rewarderPayments', 'RewarderPayments::index');

$routes->get('dashboard/notifications', 'Notifications::index');
$routes->post('dashboard/notifications/mark-all-read', 'Notifications::markAllRead');
$routes->get('dashboard/notifications/unread-count', 'Notifications::unreadCount');
$routes->get('dashboard/notifications/latest', 'Notifications::latest');
$routes->get('dashboard/notifications/go/(:num)', 'Notifications::go/$1');
$routes->get('dashboard/offline-bills/approve/(:segment)', 'OfflineBills::approve/$1');
$routes->post('dashboard/offline-bills/decide/(:segment)', 'OfflineBills::decide/$1');

$routes->group('shopkeeper', ['namespace' => 'App\Controllers\Frontend\Shopkeeper'], static function ($routes) {
    $routes->get('products', 'Products::index');
    $routes->get('products/add', 'Products::add');
    $routes->post('products/save', 'Products::save');
    $routes->get('products/edit/(:num)', 'Products::edit/$1');
    $routes->post('products/update/(:num)', 'Products::update/$1');
    $routes->get('products/delete/(:num)', 'Products::delete/$1');
    $routes->post('products/saveCategory', 'Products::saveCategory');
    $routes->get('products/deleteCategory/(:num)', 'Products::deleteCategory/$1');

    $routes->get('services', 'Services::index');
    $routes->get('services/add', 'Services::add');
    $routes->post('services/save', 'Services::save');
    $routes->get('services/edit/(:num)', 'Services::edit/$1');
    $routes->post('services/update/(:num)', 'Services::update/$1');
    $routes->get('services/delete/(:num)', 'Services::delete/$1');
    $routes->post('services/saveCategory', 'Services::saveCategory');
    $routes->get('services/deleteCategory/(:num)', 'Services::deleteCategory/$1');

    $routes->get('discounts', 'Discounts::index');
    $routes->get('discounts/add', 'Discounts::add');
    $routes->post('discounts/save', 'Discounts::save');
    $routes->get('discounts/edit/(:num)', 'Discounts::edit/$1');
    $routes->post('discounts/update/(:num)', 'Discounts::update/$1');
    $routes->get('discounts/delete/(:num)', 'Discounts::delete/$1');

    $routes->get('rewards', 'Rewards::index');
    $routes->get('rewards/add', 'Rewards::add');
    $routes->post('rewards/save', 'Rewards::save');
    $routes->get('rewards/edit/(:num)', 'Rewards::edit/$1');
    $routes->post('rewards/update/(:num)', 'Rewards::update/$1');
    $routes->get('rewards/delete/(:num)', 'Rewards::delete/$1');

    $routes->get('orders', 'Orders::index');
    $routes->get('orders/add', 'Orders::add');
    $routes->post('orders/save', 'Orders::save');
    $routes->get('orders/view/(:num)', 'Orders::view/$1');
});

$routes->group('user', ['namespace' => 'App\Controllers\Frontend\User'], static function ($routes) {
    $routes->get('/', 'Dashboard::index');
    $routes->get('profile', 'Profile::index');
    $routes->get('discounts', 'Discounts::index');
    $routes->get('wallet', 'Wallet::index');
    $routes->get('orders', 'Orders::index');
    $routes->get('orders/view/(:num)', 'Orders::view/$1');
    $routes->get('orders/receipt/(:num)', 'Orders::receipt/$1');
    $routes->post('preferred-city', 'Preferences::saveCity');
});

// Spin routes (frontend)
$routes->group('spin', ['namespace' => 'App\Controllers\Frontend'], function($routes) {
    $routes->get('/', 'Spin::index');
    $routes->get('list', 'Spin::list');
    $routes->get('view/(:num)', 'Spin::view/$1');
});

$routes->group('api/v1', ['namespace' => 'App\Controllers\Api'], static function ($routes) {
    $routes->options('(:any)', 'Cors::options');
    $routes->get('branding/logo', 'Branding::logo');
    $routes->get('home', 'Home::index');
    $routes->get('lookups', 'Lookups::index');
    $routes->get('auth/me', 'Otp::me');
    $routes->post('auth/otp/send', 'Otp::send');
    $routes->post('auth/otp/check-phone', 'Otp::checkPhone');
    $routes->post('auth/otp/verify', 'Otp::verify');
    $routes->get('profile', 'Profile::show');
    $routes->put('profile', 'Profile::update');
    $routes->patch('profile', 'Profile::update');
    $routes->post('profile/picture', 'Profile::picture');
    $routes->get('shops', 'Shops::index');
    $routes->get('shops/(:num)', 'Shops::show/$1');
    $routes->get('shops/(:num)/bill', 'Billing::show/$1');
    $routes->post('billing/preview', 'Billing::preview');
    $routes->post('billing/razorpay/order', 'Billing::razorpayOrder');
    $routes->post('billing/razorpay/verify', 'Billing::razorpayVerify');
    $routes->post('billing/phonepe/order', 'Billing::phonepeOrder');
    $routes->post('billing/phonepe/status', 'Billing::phonepeStatus');
    $routes->get('spinwheels', 'Spinwheels::index');
    $routes->get('spinwheels/(:num)', 'Spinwheels::show/$1');
    $routes->post('spinwheels/(:num)/spin', 'Spinwheels::spin/$1');
});

$routes->group('api/v1/admin', ['namespace' => 'App\Controllers\Api\Admin'], static function ($routes) {
    $routes->options('(:any)', 'Cors::options');
    $routes->post('auth/login', 'Auth::login');
    $routes->post('auth/otp/send', 'Auth::otpSend');
    $routes->post('auth/otp/verify', 'Auth::otpVerify');

    $routes->group('', ['filter' => 'admin_api_auth'], static function ($routes) {
        $routes->get('auth/me', 'Auth::me');
        $routes->post('auth/logout', 'Auth::logout');
        $routes->get('profile', 'Profile::show');
        $routes->put('profile', 'Profile::update');
        $routes->patch('profile', 'Profile::update');
        $routes->post('profile/change-password', 'Profile::changePassword');
        $routes->post('profile/picture', 'Profile::picture');

        $routes->get('product-categories', 'ProductCategories::index');
        $routes->post('product-categories', 'ProductCategories::create');
        $routes->get('product-categories/(:num)', 'ProductCategories::show/$1');
        $routes->put('product-categories/(:num)', 'ProductCategories::update/$1');
        $routes->patch('product-categories/(:num)', 'ProductCategories::update/$1');
        $routes->delete('product-categories/(:num)', 'ProductCategories::delete/$1');

        $routes->get('products', 'Products::index');
        $routes->post('products', 'Products::create');
        $routes->get('products/(:num)', 'Products::show/$1');
        $routes->put('products/(:num)', 'Products::update/$1');
        $routes->patch('products/(:num)', 'Products::update/$1');
        $routes->delete('products/(:num)', 'Products::delete/$1');

        $routes->get('service-categories', 'ServiceCategories::index');
        $routes->post('service-categories', 'ServiceCategories::create');
        $routes->get('service-categories/(:num)', 'ServiceCategories::show/$1');
        $routes->put('service-categories/(:num)', 'ServiceCategories::update/$1');
        $routes->patch('service-categories/(:num)', 'ServiceCategories::update/$1');
        $routes->delete('service-categories/(:num)', 'ServiceCategories::delete/$1');

        $routes->get('services', 'Services::index');
        $routes->post('services', 'Services::create');
        $routes->get('services/(:num)', 'Services::show/$1');
        $routes->put('services/(:num)', 'Services::update/$1');
        $routes->patch('services/(:num)', 'Services::update/$1');
        $routes->delete('services/(:num)', 'Services::delete/$1');

        $routes->get('discounts', 'Discounts::index');
        $routes->post('discounts', 'Discounts::create');
        $routes->get('discounts/(:num)', 'Discounts::show/$1');
        $routes->put('discounts/(:num)', 'Discounts::update/$1');
        $routes->patch('discounts/(:num)', 'Discounts::update/$1');
        $routes->delete('discounts/(:num)', 'Discounts::delete/$1');

        $routes->get('shops', 'Shops::index');
        $routes->post('shops', 'Shops::create');
        $routes->get('shops/(:num)', 'Shops::show/$1');
        $routes->put('shops/(:num)', 'Shops::update/$1');
        $routes->patch('shops/(:num)', 'Shops::update/$1');
        $routes->delete('shops/(:num)', 'Shops::delete/$1');

        $routes->get('orders', 'Orders::index');
        $routes->post('orders', 'Orders::create');
        $routes->get('orders/(:num)', 'Orders::show/$1');
        $routes->delete('orders/(:num)', 'Orders::delete/$1');

        $routes->get('payments', 'Payments::index');
        $routes->get('payments/(:num)', 'Payments::show/$1');
        $routes->patch('payments/(:num)', 'Payments::update/$1');

        $routes->get('notifications', 'Notifications::index');
        $routes->get('notifications/unread-count', 'Notifications::unreadCount');
        $routes->post('notifications/mark-all-read', 'Notifications::markAllRead');
        $routes->post('notifications/(:num)/read', 'Notifications::markRead/$1');

        $routes->get('offline-bills/approve/(:segment)', 'OfflineBills::approve/$1');
        $routes->post('offline-bills/decide/(:segment)', 'OfflineBills::decide/$1');
    });
});

$routes->group('api/v1/shopkeeper', ['namespace' => 'App\Controllers\Api\Shopkeeper', 'filter' => 'admin_api_auth'], static function ($routes) {
    $routes->options('(:any)', 'Cors::options');
    $routes->get('payments/summary', 'Payments::summary');
    $routes->get('payments', 'Payments::index');
    $routes->get('payments/(:num)', 'Payments::show/$1');
});

$routes->group('auth', ['namespace' => 'App\Controllers\Auth'], static function ($routes) {
    $routes->get('login', 'Login::index');
    $routes->get('login/(:segment)', 'Login::role/$1');
    $routes->post('login/check', 'Login::check');
    $routes->get('otp', 'Otp::index');
    $routes->get('otp/verify', 'Otp::verifyForm');
    $routes->post('otp/send', 'Otp::send');
    $routes->post('otp/verify', 'Otp::verify');
});

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (file_exists(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}
