<?php

use App\Http\Controllers\AccountController;
use App\Http\Controllers\AdminAnalyticsController;
use App\Http\Controllers\AdminAuditLogController;
use App\Http\Controllers\AdminBannerController;
use App\Http\Controllers\AdminBulkActionController;
use App\Http\Controllers\AdminCategoryController;
use App\Http\Controllers\AdminCouponController;
use App\Http\Controllers\AdminCustomerController;
use App\Http\Controllers\AdminDashboardController;
use App\Http\Controllers\AdminExportController;
use App\Http\Controllers\AdminFinanceController;
use App\Http\Controllers\AdminFulfillmentController;
use App\Http\Controllers\AdminGameController;
use App\Http\Controllers\AdminManualPaymentGatewayController;
use App\Http\Controllers\AdminManualPaymentSubmissionController;
use App\Http\Controllers\AdminMarketingController;
use App\Http\Controllers\AdminOperationsController;
use App\Http\Controllers\AdminOrderController;
use App\Http\Controllers\AdminProductController;
use App\Http\Controllers\AdminProviderController;
use App\Http\Controllers\AdminReportController;
use App\Http\Controllers\AdminSiteSettingController;
use App\Http\Controllers\AdminStockCodeController;
use App\Http\Controllers\AdminSupportTicketController;
use App\Http\Controllers\AdminSystemController;
use App\Http\Controllers\AdminVoucherController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\CartController;
use App\Http\Controllers\DevInspectorController;
use App\Http\Controllers\HealthController;
use App\Http\Controllers\ManualPaymentSubmissionController;
use App\Http\Controllers\MockPaymentController;
use App\Http\Controllers\OnlinePaymentController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\PaymentWebhookController;
use App\Http\Controllers\StorefrontController;
use App\Http\Controllers\StorePreferenceController;
use App\Http\Controllers\SupportTicketController;
use App\Http\Controllers\VoucherRedeemController;
use App\Http\Controllers\WalletTransferController;
use Illuminate\Support\Facades\Route;

Route::get('/', [StorefrontController::class, 'home'])->name('home');
Route::get('/health', HealthController::class)->name('health');
Route::post('/_dev/inspect-selection', [DevInspectorController::class, 'store'])->name('dev.inspect-selection');
Route::post('/preferences/currency', [StorePreferenceController::class, 'currency'])->name('preferences.currency');
Route::post('/preferences/locale', [StorePreferenceController::class, 'locale'])->name('preferences.locale');
Route::get('/product/{game:slug}', [StorefrontController::class, 'service'])->name('games.show');
Route::get('/products/{productVariant}', [StorefrontController::class, 'product'])->name('products.show');
Route::get('/checkout/{productVariant}', [StorefrontController::class, 'checkout'])->name('checkout.show');
Route::post('/checkout/{productVariant}', [OrderController::class, 'store'])->middleware('throttle:checkout')->name('checkout.store');
Route::get('/orders/{order}', [OrderController::class, 'show'])->name('orders.show');
Route::get('/payments/mock/{order}', [MockPaymentController::class, 'show'])->name('payments.mock.show');
Route::post('/payments/mock/{paymentAttempt}/confirm', [MockPaymentController::class, 'confirm'])->middleware('throttle:checkout')->name('payments.mock.confirm');
Route::post('/payments/mock/{paymentAttempt}/fail', [MockPaymentController::class, 'fail'])->middleware('throttle:checkout')->name('payments.mock.fail');
Route::get('/payments/online/{order}', [OnlinePaymentController::class, 'show'])->name('payments.online.show');
Route::post('/payments/webhooks/generic', PaymentWebhookController::class)->middleware('throttle:webhook')->name('payments.generic.webhook');
Route::get('/cart', [CartController::class, 'index'])->name('cart.index');
Route::get('/cart/checkout', [CartController::class, 'checkout'])->name('cart.checkout');
Route::post('/cart/checkout', [CartController::class, 'placeOrder'])->middleware('throttle:checkout')->name('cart.place-order');
Route::post('/cart/coupon', [CartController::class, 'applyCoupon'])->middleware('throttle:checkout')->name('cart.coupon.apply');
Route::delete('/cart/coupon', [CartController::class, 'removeCoupon'])->name('cart.coupon.remove');
Route::post('/cart/{productVariant}', [CartController::class, 'add'])->name('cart.add');
Route::patch('/cart/{productVariant}', [CartController::class, 'update'])->name('cart.update');
Route::delete('/cart/{productVariant}', [CartController::class, 'remove'])->name('cart.remove');

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->middleware('throttle:auth')->name('login.store');
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::get('/signup', [AuthController::class, 'showRegister'])->name('signup');
    Route::post('/register', [AuthController::class, 'register'])->middleware('throttle:register')->name('register.store');
});

Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');

Route::middleware('auth')->group(function () {
    Route::get('/account', [AccountController::class, 'dashboard'])->name('account.dashboard');
    Route::get('/dashboard', [AccountController::class, 'dashboard'])->name('dashboard');
    Route::get('/dashboard/orders', [AccountController::class, 'orders'])->name('account.orders');
    Route::get('/dashboard/tickets', [AccountController::class, 'tickets'])->name('account.tickets');
    Route::get('/dashboard/wallet', [AccountController::class, 'wallet'])->name('account.wallet');
    Route::get('/dashboard/notifications', [AccountController::class, 'notifications'])->name('account.notifications');
    Route::post('/dashboard/notifications/read', [AccountController::class, 'markNotificationsRead'])->name('account.notifications.read');
    Route::get('/dashboard/favorites', [AccountController::class, 'favorites'])->name('account.favorites');
    Route::post('/dashboard/favorites/{game}', [AccountController::class, 'toggleFavorite'])->name('account.favorites.toggle');
    Route::get('/dashboard/profile', [AccountController::class, 'profile'])->name('account.profile');
    Route::patch('/dashboard/profile', [AccountController::class, 'updateProfile'])->name('account.profile.update');
    Route::patch('/dashboard/profile/password', [AccountController::class, 'updatePassword'])->name('account.profile.password');
    Route::post('/dashboard/wallet/manual-payments', [ManualPaymentSubmissionController::class, 'store'])->middleware('throttle:checkout')->name('account.wallet.manual-payments.store');
    Route::post('/account/wallet/transfer', [WalletTransferController::class, 'store'])->middleware('throttle:checkout')->name('account.wallet.transfer');
    Route::post('/account/vouchers/redeem', [VoucherRedeemController::class, 'store'])->middleware('throttle:checkout')->name('account.vouchers.redeem');
    Route::post('/account/support', [SupportTicketController::class, 'store'])->middleware('throttle:checkout')->name('account.support.store');
    Route::get('/account/support/{supportTicket}', [SupportTicketController::class, 'show'])->name('account.support.show');
    Route::post('/account/support/{supportTicket}/reply', [SupportTicketController::class, 'reply'])->middleware('throttle:checkout')->name('account.support.reply');
});

Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/', AdminDashboardController::class)->name('dashboard');
    Route::get('/analytics', [AdminAnalyticsController::class, 'index'])->name('analytics.index');
    Route::get('/customers', [AdminCustomerController::class, 'index'])->name('customers.index');
    Route::get('/customers/{customer}', [AdminCustomerController::class, 'show'])->name('customers.show');
    Route::patch('/customers/{customer}', [AdminCustomerController::class, 'update'])->middleware('throttle:admin-write')->name('customers.update');
    Route::post('/customers/{customer}/wallet-adjustment', [AdminCustomerController::class, 'walletAdjustment'])->middleware('throttle:admin-write')->name('customers.wallet-adjustment');
    Route::post('/customers/{customer}/notes', [AdminCustomerController::class, 'storeNote'])->middleware('throttle:admin-write')->name('customers.notes.store');
    Route::get('/finance', [AdminFinanceController::class, 'index'])->name('finance.index');
    Route::get('/marketing', [AdminMarketingController::class, 'index'])->name('marketing.index');
    Route::get('/operations', [AdminOperationsController::class, 'index'])->name('operations.index');
    Route::get('/system', [AdminSystemController::class, 'index'])->name('system.index');
    Route::get('/exports/orders', [AdminExportController::class, 'orders'])->name('exports.orders');
    Route::get('/exports/customers', [AdminExportController::class, 'customers'])->name('exports.customers');
    Route::get('/exports/products', [AdminExportController::class, 'products'])->name('exports.products');
    Route::get('/exports/stock-codes', [AdminExportController::class, 'stockCodes'])->name('exports.stock-codes');
    Route::get('/exports/manual-payments', [AdminExportController::class, 'manualPayments'])->name('exports.manual-payments');
    Route::get('/exports/provider-logs', [AdminExportController::class, 'providerLogs'])->name('exports.provider-logs');
    Route::post('/bulk/orders', [AdminBulkActionController::class, 'orders'])->middleware('throttle:admin-write')->name('bulk.orders');
    Route::post('/bulk/products', [AdminBulkActionController::class, 'products'])->middleware('throttle:admin-write')->name('bulk.products');
    Route::post('/bulk/customers', [AdminBulkActionController::class, 'customers'])->middleware('throttle:admin-write')->name('bulk.customers');
    Route::get('/audit-logs', [AdminAuditLogController::class, 'index'])->name('audit-logs.index');
    Route::get('/reports', [AdminReportController::class, 'index'])->name('reports.index');
    Route::get('/settings', [AdminSiteSettingController::class, 'edit'])->name('settings.edit');
    Route::patch('/settings', [AdminSiteSettingController::class, 'update'])->middleware('throttle:admin-write')->name('settings.update');
    Route::resource('banners', AdminBannerController::class)->except(['show'])->middlewareFor(['store', 'update', 'destroy'], 'throttle:admin-write');
    Route::resource('categories', AdminCategoryController::class)->except(['show'])->middlewareFor(['store', 'update', 'destroy'], 'throttle:admin-write');
    Route::resource('coupons', AdminCouponController::class)->except(['show'])->middlewareFor(['store', 'update', 'destroy'], 'throttle:admin-write');
    Route::resource('vouchers', AdminVoucherController::class)->except(['show'])->middlewareFor(['store', 'update', 'destroy'], 'throttle:admin-write');
    Route::get('/manual-payments/gateways', [AdminManualPaymentGatewayController::class, 'index'])->name('manual-payments.gateways');
    Route::post('/manual-payments/gateways', [AdminManualPaymentGatewayController::class, 'store'])->middleware('throttle:admin-write')->name('manual-payments.gateways.store');
    Route::patch('/manual-payments/gateways/{manualPaymentGateway}', [AdminManualPaymentGatewayController::class, 'update'])->middleware('throttle:admin-write')->name('manual-payments.gateways.update');
    Route::delete('/manual-payments/gateways/{manualPaymentGateway}', [AdminManualPaymentGatewayController::class, 'destroy'])->middleware('throttle:admin-write')->name('manual-payments.gateways.destroy');
    Route::get('/manual-payments/submissions', [AdminManualPaymentSubmissionController::class, 'index'])->name('manual-payments.submissions');
    Route::post('/manual-payments/submissions/{manualPaymentSubmission}/approve', [AdminManualPaymentSubmissionController::class, 'approve'])->middleware('throttle:admin-write')->name('manual-payments.submissions.approve');
    Route::post('/manual-payments/submissions/{manualPaymentSubmission}/reject', [AdminManualPaymentSubmissionController::class, 'reject'])->middleware('throttle:admin-write')->name('manual-payments.submissions.reject');
    Route::post('/manual-payments/submissions/{manualPaymentSubmission}/request-info', [AdminManualPaymentSubmissionController::class, 'requestInfo'])->middleware('throttle:admin-write')->name('manual-payments.submissions.request-info');
    Route::resource('games', AdminGameController::class)->except(['show'])->middlewareFor(['store', 'update', 'destroy'], 'throttle:admin-write');
    Route::get('/products/{productVariant}/overview', [AdminProductController::class, 'show'])->name('products.show');
    Route::patch('/products/{productVariant}/quick-update', [AdminProductController::class, 'quickUpdate'])->middleware('throttle:admin-write')->name('products.quick-update');
    Route::resource('products', AdminProductController::class)->parameters(['products' => 'productVariant'])->except(['show'])->middlewareFor(['store', 'update', 'destroy'], 'throttle:admin-write');
    Route::resource('stock-codes', AdminStockCodeController::class)->only(['index', 'create', 'store', 'show', 'destroy'])->middlewareFor(['store', 'destroy'], 'throttle:admin-write');
    Route::get('/fulfillment', [AdminFulfillmentController::class, 'index'])->name('fulfillment.index');
    Route::get('/fulfillment/{order}', [AdminFulfillmentController::class, 'show'])->name('fulfillment.show');
    Route::post('/fulfillment/{order}/claim', [AdminFulfillmentController::class, 'claim'])->middleware('throttle:admin-write')->name('fulfillment.claim');
    Route::post('/fulfillment/{order}/complete', [AdminFulfillmentController::class, 'complete'])->middleware('throttle:admin-write')->name('fulfillment.complete');
    Route::get('/providers', [AdminProviderController::class, 'index'])->name('providers.index');
    Route::post('/providers', [AdminProviderController::class, 'store'])->middleware('throttle:admin-write')->name('providers.store');
    Route::patch('/providers/{provider}', [AdminProviderController::class, 'update'])->middleware('throttle:admin-write')->name('providers.update');
    Route::post('/providers/{provider}/health', [AdminProviderController::class, 'health'])->middleware('throttle:admin-write')->name('providers.health');
    Route::post('/providers/mappings', [AdminProviderController::class, 'storeMapping'])->middleware('throttle:admin-write')->name('providers.mappings.store');
    Route::patch('/providers/mappings/{mapping}', [AdminProviderController::class, 'updateMapping'])->middleware('throttle:admin-write')->name('providers.mappings.update');
    Route::delete('/providers/mappings/{mapping}', [AdminProviderController::class, 'destroyMapping'])->middleware('throttle:admin-write')->name('providers.mappings.destroy');
    Route::post('/fulfillment/{order}/provider-mock', [AdminProviderController::class, 'fulfill'])->middleware('throttle:admin-write')->name('fulfillment.provider-mock');
    Route::post('/fulfillment/{order}/fail', [AdminFulfillmentController::class, 'fail'])->middleware('throttle:admin-write')->name('fulfillment.fail');
    Route::post('/fulfillment/{order}/request-info', [AdminFulfillmentController::class, 'requestInfo'])->middleware('throttle:admin-write')->name('fulfillment.request-info');
    Route::get('/orders', [AdminOrderController::class, 'index'])->name('orders.index');
    Route::get('/orders/{order}', [AdminOrderController::class, 'show'])->name('orders.show');
    Route::post('/orders/{order}/paid', [AdminOrderController::class, 'markPaid'])->middleware('throttle:admin-write')->name('orders.paid');
    Route::post('/orders/{order}/complete', [AdminOrderController::class, 'complete'])->middleware('throttle:admin-write')->name('orders.complete');
    Route::get('/support-tickets', [AdminSupportTicketController::class, 'index'])->name('support-tickets.index');
    Route::get('/support-tickets/{supportTicket}', [AdminSupportTicketController::class, 'show'])->name('support-tickets.show');
    Route::patch('/support-tickets/{supportTicket}', [AdminSupportTicketController::class, 'update'])->middleware('throttle:admin-write')->name('support-tickets.update');
});
