<?php

declare(strict_types=1);

require_once dirname(__DIR__) . '/core/bootstrap.php';

use Core\Router;
use App\Controllers\AuthController;
use App\Controllers\DashboardController;
use App\Controllers\CategoryController;
use App\Controllers\MenuItemController;
use App\Controllers\OrderController;
use App\Controllers\VoucherController;
use App\Controllers\DealController;
use App\Controllers\DiscountController;
use App\Controllers\DiscountPolicyController;
use App\Controllers\ReportController;
use App\Controllers\UserController;
use App\Controllers\SettingsController;
use App\Controllers\ActivityLogController;
use App\Controllers\ShopController;
use App\Controllers\RiderController;
use App\Controllers\ImportController;
use App\Controllers\RiderPortalController;
use App\Controllers\CustomerController;
use App\Controllers\BiController;
use App\Controllers\CashierController;
use App\Controllers\RoleController;
use App\Controllers\Api\AuthController    as ApiAuth;
use App\Controllers\Api\ShopApiController as ApiShop;
use App\Controllers\Api\AddressController as ApiAddress;

$router = new Router();
// Use env var if set; otherwise derive from APP_URL path so it works on both XAMPP and live hosting
$router->setBasePath(getenv('ROUTER_BASE') ?: rtrim(parse_url(APP_URL, PHP_URL_PATH), '/'));

// ── Auth ──────────────────────────────────────────────────────────────────────
$router->get( '/auth/login',   [AuthController::class, 'loginForm']);
$router->post('/auth/login',   [AuthController::class, 'login']);
$router->get( '/auth/logout',  [AuthController::class, 'logout']);

// ── Dashboard ─────────────────────────────────────────────────────────────────
$router->get('/dashboard',     [DashboardController::class, 'index']);
$router->get('/',              [DashboardController::class, 'index']);

// ── Categories ────────────────────────────────────────────────────────────────
$router->get( '/categories',            [CategoryController::class, 'index']);
$router->get( '/categories/create',     [CategoryController::class, 'create']);
$router->post('/categories/store',      [CategoryController::class, 'store']);
$router->get( '/categories/{id}/edit',  [CategoryController::class, 'edit']);
$router->post('/categories/{id}/update',[CategoryController::class, 'update']);
$router->post('/categories/{id}/delete',[CategoryController::class, 'delete']);
$router->post('/categories/{id}/toggle',[CategoryController::class, 'toggle']);
$router->post('/categories/{id}/sort',  [CategoryController::class, 'updateSort']);

// ── Menu Items ────────────────────────────────────────────────────────────────
$router->get( '/menu',                 [MenuItemController::class, 'index']);
$router->get( '/menu/create',          [MenuItemController::class, 'create']);
$router->post('/menu/store',           [MenuItemController::class, 'store']);
$router->get( '/menu/{id}/edit',       [MenuItemController::class, 'edit']);
$router->post('/menu/{id}/update',     [MenuItemController::class, 'update']);
$router->post('/menu/{id}/delete',     [MenuItemController::class, 'delete']);
$router->post('/menu/{id}/toggle',     [MenuItemController::class, 'toggle']);
// Choices (variants) — inline AJAX
$router->post('/menu/{id}/choices/store',         [MenuItemController::class, 'storeChoice']);
$router->post('/menu/choices/{choiceId}/delete',  [MenuItemController::class, 'deleteChoice']);

// ── Orders ────────────────────────────────────────────────────────────────────
$router->get( '/orders',                      [OrderController::class, 'index']);
$router->get( '/orders/live',                 [OrderController::class, 'live']);
// AJAX — static paths must come before the {id} wildcard
$router->get( '/orders/pending-count',        [OrderController::class, 'pendingCount']);
$router->get( '/orders/live-data',            [OrderController::class, 'liveData']);
$router->get( '/orders/{id}',                 [OrderController::class, 'show']);
$router->post('/orders/{id}/status',          [OrderController::class, 'updateStatus']);
$router->post('/orders/{id}/cancel',          [OrderController::class, 'cancel']);
$router->get( '/orders/{id}/invoice',         [OrderController::class, 'invoice']);
$router->get( '/orders/{id}/kot',             [OrderController::class, 'kot']);

// ── Vouchers ──────────────────────────────────────────────────────────────────
$router->get( '/vouchers',            [VoucherController::class, 'index']);
$router->get( '/vouchers/create',     [VoucherController::class, 'create']);
$router->post('/vouchers/store',      [VoucherController::class, 'store']);
$router->get( '/vouchers/{id}/edit',  [VoucherController::class, 'edit']);
$router->post('/vouchers/{id}/update',[VoucherController::class, 'update']);
$router->post('/vouchers/{id}/delete',[VoucherController::class, 'delete']);
$router->post('/vouchers/{id}/toggle',[VoucherController::class, 'toggle']);

// ── Deals ─────────────────────────────────────────────────────────────────────
$router->get( '/deals',            [DealController::class, 'index']);
$router->get( '/deals/create',     [DealController::class, 'create']);
$router->post('/deals/store',      [DealController::class, 'store']);
$router->get( '/deals/{id}/edit',  [DealController::class, 'edit']);
$router->post('/deals/{id}/update',[DealController::class, 'update']);
$router->post('/deals/{id}/delete',[DealController::class, 'delete']);

// ── Discounts ─────────────────────────────────────────────────────────────────
$router->get( '/discounts',            [DiscountController::class, 'index']);
$router->get( '/discounts/create',     [DiscountController::class, 'create']);
$router->post('/discounts/store',      [DiscountController::class, 'store']);
$router->get( '/discounts/{id}/edit',  [DiscountController::class, 'edit']);
$router->post('/discounts/{id}/update',[DiscountController::class, 'update']);
$router->post('/discounts/{id}/delete',[DiscountController::class, 'delete']);

// ── Reports ───────────────────────────────────────────────────────────────────
$router->get('/reports/sales',             [ReportController::class, 'sales']);
$router->get('/reports/invoices',          [ReportController::class, 'invoices']);
$router->get('/reports/customers',         [ReportController::class, 'customers']);
$router->get('/reports/customers/{id}',    [ReportController::class, 'customerDetail']);

// ── Roles & Permissions ───────────────────────────────────────────────────────
$router->get( '/roles',                [RoleController::class, 'index']);
$router->post('/roles/store',          [RoleController::class, 'store']);
$router->get( '/roles/{id}/edit',      [RoleController::class, 'edit']);
$router->post('/roles/{id}/update',    [RoleController::class, 'update']);
$router->post('/roles/{id}/delete',    [RoleController::class, 'delete']);

// ── Users ─────────────────────────────────────────────────────────────────────
$router->get( '/users',                   [UserController::class, 'index']);
$router->get( '/users/create',            [UserController::class, 'create']);
$router->post('/users/store',             [UserController::class, 'store']);
$router->get( '/users/{id}/edit',         [UserController::class, 'edit']);
$router->post('/users/{id}/update',       [UserController::class, 'update']);
$router->post('/users/{id}/delete',       [UserController::class, 'delete']);
$router->post('/users/{id}/toggle',       [UserController::class, 'toggle']);
$router->post('/users/{id}/reset-lock',   [UserController::class, 'resetLock']);

// ── Settings ──────────────────────────────────────────────────────────────────
$router->get( '/settings',        [SettingsController::class, 'index']);
$router->post('/settings/update', [SettingsController::class, 'update']);

$router->get( '/discount-policies',        [DiscountPolicyController::class, 'index']);
$router->post('/discount-policies/update', [DiscountPolicyController::class, 'update']);

// ── Import ────────────────────────────────────────────────────────────────────
$router->get( '/import',                    [ImportController::class, 'index']);
$router->post('/import/categories',         [ImportController::class, 'importCategories']);
$router->post('/import/menu',               [ImportController::class, 'importMenu']);
$router->get( '/import/sample/categories',  [ImportController::class, 'sampleCategories']);
$router->get( '/import/sample/menu',        [ImportController::class, 'sampleMenu']);

// ── Customers ─────────────────────────────────────────────────────────────────
$router->get( '/customers',                       [CustomerController::class, 'index']);
$router->get( '/customers/export',                [CustomerController::class, 'export']);
$router->get( '/customers/{id}',                  [CustomerController::class, 'show']);
$router->get( '/customers/{id}/edit',             [CustomerController::class, 'edit']);
$router->post('/customers/{id}/update',           [CustomerController::class, 'update']);
$router->post('/customers/{id}/toggle',           [CustomerController::class, 'toggle']);
$router->post('/customers/{id}/blacklist',        [CustomerController::class, 'blacklist']);
$router->post('/customers/{id}/unblacklist',      [CustomerController::class, 'unblacklist']);
$router->post('/customers/{id}/delete',           [CustomerController::class, 'delete']);

// ── Riders ────────────────────────────────────────────────────────────────────
$router->get( '/riders',                [RiderController::class, 'index']);
$router->get( '/riders/create',         [RiderController::class, 'create']);
$router->post('/riders/store',          [RiderController::class, 'store']);
$router->get( '/riders/{id}',           [RiderController::class, 'show']);
$router->get( '/riders/{id}/edit',      [RiderController::class, 'edit']);
$router->post('/riders/{id}/update',    [RiderController::class, 'update']);
$router->post('/riders/{id}/delete',    [RiderController::class, 'delete']);
$router->post('/riders/{id}/toggle',    [RiderController::class, 'toggle']);

// ── Rider Portal ──────────────────────────────────────────────────────────────
$router->get( '/rider/login',                    [RiderPortalController::class, 'loginForm']);
$router->post('/rider/login',                    [RiderPortalController::class, 'login']);
$router->get( '/rider/logout',                   [RiderPortalController::class, 'logout']);
$router->get( '/rider/dashboard',                [RiderPortalController::class, 'dashboard']);
$router->get( '/rider/orders/{id}',              [RiderPortalController::class, 'showOrder']);
$router->post('/rider/orders/{id}/status',       [RiderPortalController::class, 'updateStatus']);
$router->post('/rider/location',                 [RiderPortalController::class, 'updateLocation']);
// Customer polls rider GPS for live tracking
$router->get( '/shop/orders/{id}/rider-location',[RiderPortalController::class, 'riderLocation']);

// ── Business Intelligence ─────────────────────────────────────────────────────
$router->get('/reports/bi',                [BiController::class, 'overview']);
$router->get('/reports/bi/revenue',        [BiController::class, 'revenue']);
$router->get('/reports/bi/category',       [BiController::class, 'category']);
$router->get('/reports/bi/peak-hours',     [BiController::class, 'peakHours']);
$router->get('/reports/bi/cancellations',  [BiController::class, 'cancellations']);
$router->get('/reports/bi/riders',         [BiController::class, 'riders']);
$router->get('/reports/bi/retention',      [BiController::class, 'retention']);

// ── Cashier ───────────────────────────────────────────────────────────────────
$router->get( '/cashier',          [CashierController::class, 'index']);
$router->post('/cashier/{id}/pay', [CashierController::class, 'markPaid']);

// ── Payment Modes (Settings) ──────────────────────────────────────────────────
$router->post('/settings/payment-modes/store',          [SettingsController::class, 'storePaymentMode']);
$router->post('/settings/payment-modes/{id}/toggle',    [SettingsController::class, 'togglePaymentMode']);
$router->post('/settings/payment-modes/{id}/delete',    [SettingsController::class, 'deletePaymentMode']);

// ── Cash Summary Report ───────────────────────────────────────────────────────
$router->get('/reports/cash-summary', [ReportController::class, 'cashSummary']);

// ── Activity Log ──────────────────────────────────────────────────────────────
$router->get('/activity-log', [ActivityLogController::class, 'index']);

// ── Customer Storefront ───────────────────────────────────────────────────────
$router->get( '/shop',                          [ShopController::class, 'menu']);
$router->get( '/shop/menu',                     [ShopController::class, 'menu']);
$router->get( '/shop/item/{id}',                [ShopController::class, 'item']);
$router->get( '/shop/item/{id}/json',           [ShopController::class, 'itemJson']);
$router->post('/shop/cart/add',                 [ShopController::class, 'cartAdd']);
$router->post('/shop/cart/update',              [ShopController::class, 'cartUpdate']);
$router->get( '/shop/cart',                     [ShopController::class, 'cart']);
$router->post('/shop/voucher/validate',         [ShopController::class, 'validateVoucher']);
$router->post('/shop/delivery-fee',             [ShopController::class, 'calcDeliveryFee']);
$router->get( '/shop/checkout',                 [ShopController::class, 'checkout']);
$router->post('/shop/place-order',              [ShopController::class, 'placeOrder']);
$router->get( '/shop/orders',                   [ShopController::class, 'myOrders']);
$router->get( '/shop/orders/{id}',              [ShopController::class, 'orderDetail']);
$router->get( '/shop/orders/{id}/status',       [ShopController::class, 'orderStatus']);
$router->get( '/shop/account',                  [ShopController::class, 'account']);
$router->get( '/shop/login',                    [ShopController::class, 'loginForm']);
$router->post('/shop/login',                    [ShopController::class, 'login']);
$router->get( '/shop/register',                 [ShopController::class, 'registerForm']);
$router->post('/shop/register',                 [ShopController::class, 'register']);
$router->get( '/shop/logout',                   [ShopController::class, 'logout']);

// ── REST API v1 ───────────────────────────────────────────────────────────────
$router->post('/api/v1/auth/register',      [ApiAuth::class,  'register']);
$router->post('/api/v1/auth/login',         [ApiAuth::class,  'login']);
$router->post('/api/v1/auth/logout',        [ApiAuth::class,  'logout']);
$router->get( '/api/v1/auth/me',            [ApiAuth::class,  'me']);
$router->post('/api/v1/auth/profile',       [ApiAuth::class,  'updateProfile']);

$router->get( '/api/v1/settings',           [ApiShop::class,  'settings']);
$router->get( '/api/v1/categories',         [ApiShop::class,  'categories']);
$router->get( '/api/v1/items',              [ApiShop::class,  'items']);
$router->get( '/api/v1/items/{id}',         [ApiShop::class,  'itemDetail']);
$router->get( '/api/v1/deals',              [ApiShop::class,  'deals']);
$router->get( '/api/v1/discount-policies',  [ApiShop::class,  'discountPolicies']);
$router->post('/api/v1/delivery-fee',       [ApiShop::class,  'deliveryFee']);
$router->post('/api/v1/order/quote',        [ApiShop::class,  'quote']);
$router->post('/api/v1/voucher/validate',   [ApiShop::class,  'validateVoucher']);
$router->post('/api/v1/orders',             [ApiShop::class,  'placeOrder']);
$router->get( '/api/v1/orders',             [ApiShop::class,  'myOrders']);
$router->get( '/api/v1/orders/{id}',        [ApiShop::class,  'orderDetail']);
$router->get( '/api/v1/orders/{id}/status', [ApiShop::class,  'orderStatus']);

// Saved delivery addresses (auth required)
$router->get( '/api/v1/addresses',              [ApiAddress::class, 'index']);
$router->post('/api/v1/addresses',              [ApiAddress::class, 'store']);
$router->post('/api/v1/addresses/{id}/location',[ApiAddress::class, 'updateLocation']);
$router->post('/api/v1/addresses/{id}/default', [ApiAddress::class, 'setDefault']);
$router->post('/api/v1/addresses/{id}/delete',  [ApiAddress::class, 'delete']);

$router->dispatch();
