<?php

use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\OrderController;
use App\Http\Controllers\Admin\ProductController;
use App\Http\Controllers\Admin\FranchiseController;
use App\Http\Controllers\Admin\GatewayController;
use App\Http\Controllers\Admin\HomepageAchieverController;
use App\Http\Controllers\Admin\HomepageBusinessCardController;
use App\Http\Controllers\Admin\HomepageFeatureController;
use App\Http\Controllers\Admin\HomepageGalleryController;
use App\Http\Controllers\Admin\HomepageSettingsController;
use App\Http\Controllers\Admin\HomepageStepController;
use App\Http\Controllers\Admin\HomepageTestimonialController;
use App\Http\Controllers\Admin\NotificationController;
use App\Http\Controllers\Admin\KycController;
use App\Http\Controllers\Admin\LegalPageController;
use App\Http\Controllers\Admin\MemberController;
use App\Http\Controllers\Admin\RankController;
use App\Http\Controllers\Admin\ApiController;
use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\PaymentController;
use App\Http\Controllers\Admin\PlanController;
use App\Http\Controllers\Admin\RepairToolController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\SecurityController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\TreeController;
use App\Http\Controllers\Admin\UpiController;
use App\Http\Controllers\Admin\WalletController;
use App\Http\Controllers\Admin\WithdrawalController;
use App\Http\Controllers\Auth\AuthController;
use Illuminate\Support\Facades\Route;

Route::prefix('{adminSlug}')->middleware('admin.slug')->group(function () {
    Route::middleware('guest')->group(function () {
        Route::get('/', [AuthController::class, 'showLogin'])->name('admin.login');
        Route::post('/login', [AuthController::class, 'login'])
            ->defaults('expected_role', 'admin')
            ->middleware('throttle:login')
            ->name('admin.login.submit');
    });

    Route::middleware(['auth', 'role:admin'])->name('admin.')->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
        Route::get('/members', [MemberController::class, 'index'])->name('members');
        Route::get('/members/create', [MemberController::class, 'create'])->name('members.create');
        Route::post('/members', [MemberController::class, 'store'])->name('members.store');
        Route::get('/members/{user}/edit', [MemberController::class, 'edit'])->name('members.edit');
        Route::post('/members/{user}/update', [MemberController::class, 'update'])->name('members.update');
        Route::post('/members/{user}/status', [MemberController::class, 'updateStatus'])->name('members.status');
        Route::post('/members/{user}/sponsor', [MemberController::class, 'updateSponsor'])->name('members.sponsor');
        Route::post('/members/{user}/placement', [MemberController::class, 'updatePlacement'])->name('members.placement');
        Route::post('/members/{user}/impersonate', [MemberController::class, 'impersonate'])->name('members.impersonate');
        Route::post('/members/{user}/close', [MemberController::class, 'close'])->name('members.close');
        Route::post('/members/merge', [MemberController::class, 'merge'])->name('members.merge');
        Route::post('/members/{user}/reset-password', [MemberController::class, 'resetPassword'])->name('members.reset-password');
        Route::post('/members/{user}/reset-otp', [MemberController::class, 'resetOtp'])->name('members.reset-otp');
        Route::post('/members/{user}/reset-pin', [MemberController::class, 'resetTransactionPin'])->name('members.reset-pin');
        Route::post('/members/{user}/toggle-login-otp', [MemberController::class, 'toggleLoginOtp'])->name('members.toggle-login-otp');
        Route::post('/members/{user}/rank-override', [MemberController::class, 'overrideRank'])->name('members.rank-override');
        Route::post('/members/{user}/force-plan', [MemberController::class, 'forcePlan'])->name('members.force-plan');
        Route::post('/members/{user}/bv-adjust', [MemberController::class, 'adjustBv'])->name('members.bv-adjust');
        Route::post('/members/bulk-import', [MemberController::class, 'bulkImport'])->name('members.bulk-import');
        Route::get('/members/imports', [MemberController::class, 'imports'])->name('members.imports');
        Route::get('/members/imports/{batch}/errors', [MemberController::class, 'downloadImportErrors'])->name('members.imports.errors');
        Route::get('/wallet', [WalletController::class, 'index'])->name('wallet');
        Route::get('/wallet/{user}/ledger', [WalletController::class, 'ledger'])->name('wallet.ledger');
        Route::get('/wallet/{user}/ledger/export', [WalletController::class, 'exportLedger'])->name('wallet.ledger.export');
        Route::post('/wallet/credit', [WalletController::class, 'credit'])->name('wallet.credit');
        Route::post('/wallet/debit', [WalletController::class, 'debit'])->name('wallet.debit');
        Route::post('/wallet/{user}/freeze', [WalletController::class, 'freeze'])->name('wallet.freeze');
        Route::post('/wallet/transfer', [WalletController::class, 'transferBetweenWallets'])->name('wallet.transfer');
        Route::post('/wallet/reverse', [WalletController::class, 'reverse'])->name('wallet.reverse');
        Route::post('/wallet/bulk-adjust', [WalletController::class, 'bulkAdjust'])->name('wallet.bulk-adjust');
        Route::get('/wallet/imports/{batch}/errors', [WalletController::class, 'downloadAdjustErrors'])->name('wallet.imports.errors');
        Route::get('/withdrawals', [WithdrawalController::class, 'index'])->name('withdrawals');
        Route::post('/withdrawals/{withdrawal}/approve', [WithdrawalController::class, 'approve'])->name('withdrawals.approve');
        Route::post('/withdrawals/{withdrawal}/reject', [WithdrawalController::class, 'reject'])->name('withdrawals.reject');
        Route::post('/withdrawals/bulk-approve', [WithdrawalController::class, 'bulkApprove'])->name('withdrawals.bulk-approve');
        Route::post('/withdrawals/{withdrawal}/proof', [WithdrawalController::class, 'uploadProof'])->name('withdrawals.proof');
        Route::post('/withdrawals/{withdrawal}/retry', [WithdrawalController::class, 'retryPayout'])->name('withdrawals.retry');
        Route::get('/products', [ProductController::class, 'index'])->name('products');
        Route::post('/products', [ProductController::class, 'store'])->name('products.store');
        Route::post('/products/{product}/update', [ProductController::class, 'update'])->name('products.update');
        Route::post('/products/{product}/delete', [ProductController::class, 'destroy'])->name('products.delete');
        Route::post('/products/categories', [ProductController::class, 'storeCategory'])->name('products.categories.store');
        Route::post('/products/categories/{category}/update', [ProductController::class, 'updateCategory'])->name('products.categories.update');
        Route::post('/products/categories/{category}/delete', [ProductController::class, 'deleteCategory'])->name('products.categories.delete');
        Route::get('/orders', [OrderController::class, 'index'])->name('orders');
        Route::post('/orders/{order}/approve', [OrderController::class, 'approve'])->name('orders.approve');
        Route::post('/orders/{order}/cancel', [OrderController::class, 'cancel'])->name('orders.cancel');
        Route::post('/orders/{order}/refund', [OrderController::class, 'refund'])->name('orders.refund');
        Route::post('/orders/{order}/status', [OrderController::class, 'updateStatus'])->name('orders.status');
        Route::get('/orders/{order}/invoice', [OrderController::class, 'invoice'])->name('orders.invoice');
        Route::get('/reports', [ReportController::class, 'index'])->name('reports');
        Route::get('/reports/joining', [ReportController::class, 'joining'])->name('reports.joining');
        Route::get('/reports/bv', [ReportController::class, 'bv'])->name('reports.bv');
        Route::get('/reports/income', [ReportController::class, 'income'])->name('reports.income');
        Route::get('/reports/payout', [ReportController::class, 'payout'])->name('reports.payout');
        Route::get('/reports/product-sales', [ReportController::class, 'productSales'])->name('reports.product-sales');
        Route::get('/reports/franchise-sales', [ReportController::class, 'franchiseSales'])->name('reports.franchise-sales');
        Route::get('/reports/wallet', [ReportController::class, 'wallet'])->name('reports.wallet');
        Route::get('/reports/tax', [ReportController::class, 'tax'])->name('reports.tax');
        Route::get('/reports/rank', [ReportController::class, 'rank'])->name('reports.rank');
        Route::get('/reports/team-growth', [ReportController::class, 'teamGrowth'])->name('reports.team-growth');
        Route::get('/reports/withdrawal-trends', [ReportController::class, 'withdrawalTrends'])->name('reports.withdrawal-trends');
        Route::get('/settings', [SettingController::class, 'index'])->name('settings');
        Route::post('/settings', [SettingController::class, 'update'])->name('settings.update');
        Route::get('/tree', [TreeController::class, 'index'])->name('tree');
        Route::get('/tree/export', [TreeController::class, 'export'])->name('tree.export');
        Route::get('/security', [SecurityController::class, 'index'])->name('security');
        Route::get('/security/roles', [RoleController::class, 'index'])->name('security.roles');
        Route::post('/security/roles', [RoleController::class, 'store'])->name('security.roles.store');
        Route::post('/security/roles/{role}/permissions', [RoleController::class, 'updatePermissions'])->name('security.roles.permissions');
        Route::get('/security/logs', [ActivityLogController::class, 'index'])->name('security.logs');
        Route::get('/payments', [PaymentController::class, 'index'])->name('payments');
        Route::get('/payments/gateways', [GatewayController::class, 'index'])->name('payments.gateways');
        Route::post('/payments/gateways/{gateway}', [GatewayController::class, 'update'])->name('payments.gateways.update');
        Route::post('/payments/gateways/{gateway}/toggle', [GatewayController::class, 'toggle'])->name('payments.gateways.toggle');
        Route::get('/api', [ApiController::class, 'index'])->name('api');
        Route::post('/api/keys', [ApiController::class, 'store'])->name('api.keys.store');
        Route::post('/api/keys/{key}/revoke', [ApiController::class, 'revoke'])->name('api.keys.revoke');
        Route::post('/api/keys/{key}/ips', [ApiController::class, 'addIp'])->name('api.keys.ips');
        Route::post('/api/keys/{key}/ips/{ip}/delete', [ApiController::class, 'removeIp'])->name('api.keys.ips.delete');
        Route::get('/franchises', [FranchiseController::class, 'index'])->name('franchises');
        Route::get('/plan', [PlanController::class, 'index'])->name('plan');
        Route::post('/plan', [PlanController::class, 'store'])->name('plan.store');
        Route::post('/plan/{plan}', [PlanController::class, 'update'])->name('plan.update');
        Route::post('/plan/{plan}/delete', [PlanController::class, 'destroy'])->name('plan.delete');
        Route::get('/cms/homepage-settings', [HomepageSettingsController::class, 'index'])->name('cms.homepage-settings');
        Route::post('/cms/homepage-settings', [HomepageSettingsController::class, 'update'])->name('cms.homepage-settings.update');
        Route::get('/cms/features', [HomepageFeatureController::class, 'index'])->name('cms.features');
        Route::post('/cms/features', [HomepageFeatureController::class, 'store'])->name('cms.features.store');
        Route::post('/cms/features/{feature}', [HomepageFeatureController::class, 'update'])->name('cms.features.update');
        Route::post('/cms/features/{feature}/delete', [HomepageFeatureController::class, 'destroy'])->name('cms.features.delete');
        Route::get('/cms/steps', [HomepageStepController::class, 'index'])->name('cms.steps');
        Route::post('/cms/steps', [HomepageStepController::class, 'store'])->name('cms.steps.store');
        Route::post('/cms/steps/{step}', [HomepageStepController::class, 'update'])->name('cms.steps.update');
        Route::post('/cms/steps/{step}/delete', [HomepageStepController::class, 'destroy'])->name('cms.steps.delete');
        Route::get('/cms/business-plan', [HomepageBusinessCardController::class, 'index'])->name('cms.business-plan');
        Route::post('/cms/business-plan', [HomepageBusinessCardController::class, 'store'])->name('cms.business-plan.store');
        Route::post('/cms/business-plan/{card}', [HomepageBusinessCardController::class, 'update'])->name('cms.business-plan.update');
        Route::post('/cms/business-plan/{card}/delete', [HomepageBusinessCardController::class, 'destroy'])->name('cms.business-plan.delete');
        Route::get('/cms/testimonials', [HomepageTestimonialController::class, 'index'])->name('cms.testimonials');
        Route::post('/cms/testimonials', [HomepageTestimonialController::class, 'store'])->name('cms.testimonials.store');
        Route::post('/cms/testimonials/{testimonial}', [HomepageTestimonialController::class, 'update'])->name('cms.testimonials.update');
        Route::post('/cms/testimonials/{testimonial}/delete', [HomepageTestimonialController::class, 'destroy'])->name('cms.testimonials.delete');
        Route::get('/cms/achievers', [HomepageAchieverController::class, 'index'])->name('cms.achievers');
        Route::post('/cms/achievers', [HomepageAchieverController::class, 'store'])->name('cms.achievers.store');
        Route::post('/cms/achievers/{achiever}', [HomepageAchieverController::class, 'update'])->name('cms.achievers.update');
        Route::post('/cms/achievers/{achiever}/delete', [HomepageAchieverController::class, 'destroy'])->name('cms.achievers.delete');
        Route::get('/cms/gallery', [HomepageGalleryController::class, 'index'])->name('cms.gallery');
        Route::post('/cms/gallery', [HomepageGalleryController::class, 'store'])->name('cms.gallery.store');
        Route::post('/cms/gallery/{item}', [HomepageGalleryController::class, 'update'])->name('cms.gallery.update');
        Route::post('/cms/gallery/{item}/delete', [HomepageGalleryController::class, 'destroy'])->name('cms.gallery.delete');
        Route::get('/cms/legal', [LegalPageController::class, 'index'])->name('cms.legal');
        Route::post('/cms/legal', [LegalPageController::class, 'store'])->name('cms.legal.store');
        Route::post('/cms/legal/{page}', [LegalPageController::class, 'update'])->name('cms.legal.update');
        Route::post('/cms/legal/{page}/delete', [LegalPageController::class, 'destroy'])->name('cms.legal.delete');
        Route::get('/repair-tools', [RepairToolController::class, 'index'])->name('repair-tools');
        Route::post('/repair-tools/recalculate-wallet', [RepairToolController::class, 'recalculateWallet'])->name('repair-tools.recalculate-wallet');
        Route::post('/repair-tools/rebuild-carry', [RepairToolController::class, 'rebuildCarry'])->name('repair-tools.rebuild-carry');
        Route::post('/repair-tools/fix-tree', [RepairToolController::class, 'fixTree'])->name('repair-tools.fix-tree');
        Route::post('/repair-tools/retry-payouts', [RepairToolController::class, 'retryPayouts'])->name('repair-tools.retry-payouts');
        Route::post('/repair-tools/retry-webhooks', [RepairToolController::class, 'retryWebhooks'])->name('repair-tools.retry-webhooks');
        Route::post('/repair-tools/maintenance/on', [RepairToolController::class, 'enableMaintenance'])->name('repair-tools.maintenance.on');
        Route::post('/repair-tools/maintenance/off', [RepairToolController::class, 'disableMaintenance'])->name('repair-tools.maintenance.off');
        Route::get('/kyc', [KycController::class, 'index'])->name('kyc');
        Route::post('/kyc/{kyc}/approve', [KycController::class, 'approve'])->name('kyc.approve');
        Route::post('/kyc/{kyc}/reject', [KycController::class, 'reject'])->name('kyc.reject');
        Route::post('/kyc/bulk-approve', [KycController::class, 'bulkApprove'])->name('kyc.bulk-approve');
        Route::post('/kyc/{kyc}/reupload', [KycController::class, 'forceReupload'])->name('kyc.reupload');
        Route::get('/upi', [UpiController::class, 'index'])->name('upi');
        Route::post('/upi/{upi}/approve', [UpiController::class, 'approve'])->name('upi.approve');
        Route::post('/upi/{upi}/reject', [UpiController::class, 'reject'])->name('upi.reject');
        Route::get('/ranks', [RankController::class, 'index'])->name('ranks');
        Route::post('/ranks', [RankController::class, 'store'])->name('ranks.store');
        Route::post('/ranks/{rank}/update', [RankController::class, 'update'])->name('ranks.update');
        Route::post('/ranks/{rank}/delete', [RankController::class, 'destroy'])->name('ranks.delete');
        Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications');
        Route::post('/notifications', [NotificationController::class, 'store'])->name('notifications.store');
    });
});
