<?php

declare(strict_types=1);

use App\Http\Controllers\Account\AffiliateController;
use App\Http\Controllers\Account\BillingController;
use App\Http\Controllers\Account\CartController;
use App\Http\Controllers\Account\CheckoutController;
use App\Http\Controllers\Account\DashboardController;
use App\Http\Controllers\Account\Dedicated\DedicatedPanelController;
use App\Http\Controllers\Account\Game\GamePanelController;
use App\Http\Controllers\Account\Hosting\HostingPanelController;
use App\Http\Controllers\Account\LoginHistoryController;
use App\Http\Controllers\Account\NotificationController;
use App\Http\Controllers\Account\PlanController;
use App\Http\Controllers\Account\ProfileController;
use App\Http\Controllers\Account\ServiceController;
use App\Http\Controllers\Account\SessionController;
use App\Http\Controllers\Account\SubscriptionController;
use App\Http\Controllers\Account\TicketController;
use App\Http\Controllers\Account\TrustedDeviceController;
use App\Http\Controllers\Account\UpgradeController;
use App\Http\Controllers\Account\Vps\VpsPanelController;
use App\Http\Controllers\Account\VpsController;
use Illuminate\Support\Facades\Route;

// Impersonation stop (must be on account subdomain since impersonated user lacks admin role)
Route::post('/impersonate/stop', [\App\Http\Controllers\Admin\ImpersonationController::class, 'stop'])->name('impersonation.stop');

Route::get('/dashboard', [DashboardController::class, 'index'])->name('account.dashboard');

Route::get('/profile', [ProfileController::class, 'show'])->name('account.profile');
Route::get('/login-history', [LoginHistoryController::class, 'index'])->name('login-history.index');
Route::put('/profile', [ProfileController::class, 'update'])->name('account.profile.update');
Route::put('/profile/password', [ProfileController::class, 'updatePassword'])->name('account.profile.password');
Route::put('/profile/billing', [ProfileController::class, 'updateBilling'])->name('account.profile.billing');

// Trusted Devices
Route::delete('/profile/trusted-devices/{device}', [TrustedDeviceController::class, 'destroy'])->name('trusted-devices.destroy');
Route::delete('/profile/trusted-devices', [TrustedDeviceController::class, 'destroyAll'])->name('trusted-devices.destroy-all');

// Sessions
Route::delete('/profile/sessions', [SessionController::class, 'destroy'])->name('sessions.destroy');

// Plans
Route::get('/plans', [PlanController::class, 'index'])->name('account.plans.index');
Route::get('/plans/{plan}', [PlanController::class, 'show'])->name('account.plans.show');

// Cart
Route::get('/cart', [CartController::class, 'index'])->name('account.cart.index');
Route::post('/cart', [CartController::class, 'add'])->name('account.cart.add');
Route::put('/cart/{cartItem}', [CartController::class, 'update'])->name('account.cart.update');
Route::delete('/cart/{cartItem}', [CartController::class, 'remove'])->name('account.cart.remove');

// Checkout (static routes first to avoid parameter matching)
Route::post('/checkout/apply-coupon', [CheckoutController::class, 'applyCoupon'])->name('account.checkout.apply-coupon');
Route::get('/checkout/paypal/callback', [CheckoutController::class, 'paypalCallback'])->name('account.checkout.paypal-callback');
Route::get('/checkout/paypal/cancel', [CheckoutController::class, 'paypalCancel'])->name('account.checkout.paypal-cancel');
Route::get('/checkout/custom/{serviceType}', [CheckoutController::class, 'showCustom'])
    ->where('serviceType', 'vps|mysql|game')
    ->name('checkout.custom');
Route::get('/checkout/{plan}', [CheckoutController::class, 'show'])->name('account.checkout.show');
Route::post('/checkout/{plan}', [CheckoutController::class, 'store'])->name('account.checkout.store');

// Services
Route::resource('services', ServiceController::class)->only(['index', 'show'])->names('account.services');
Route::get('/services/{service}/upgrade', [UpgradeController::class, 'show'])->name('account.services.upgrade');
Route::post('/services/{service}/upgrade', [UpgradeController::class, 'store'])->name('account.services.upgrade.store');

// VPS Control
Route::prefix('/services/{service}/vps')->name('account.services.vps.')->group(function () {
    Route::post('/boot', [VpsController::class, 'boot'])->name('boot');
    Route::post('/shutdown', [VpsController::class, 'shutdown'])->name('shutdown');
    Route::post('/restart', [VpsController::class, 'restart'])->name('restart');
    Route::post('/poweroff', [VpsController::class, 'poweroff'])->name('poweroff');
    Route::post('/reset-password', [VpsController::class, 'resetPassword'])->name('reset-password');
    Route::get('/vnc', [VpsController::class, 'vnc'])->name('vnc');
    Route::get('/templates', [VpsController::class, 'templates'])->name('templates');
    Route::post('/rebuild', [VpsController::class, 'rebuild'])->name('rebuild');
});

// VPS Panel
Route::prefix('/services/{service}/vps-panel')->name('account.services.vps-panel.')->group(function () {
    Route::get('/', [VpsPanelController::class, 'dashboard'])->name('dashboard');
    Route::get('/console', [VpsPanelController::class, 'console'])->name('console');
    Route::get('/graphs', [VpsPanelController::class, 'graphs'])->name('graphs');
    Route::get('/firewall', [VpsPanelController::class, 'firewall'])->name('firewall');
    Route::post('/firewall', [VpsPanelController::class, 'storeFirewallRule'])->name('firewall.store');
    Route::delete('/firewall/{ruleId}', [VpsPanelController::class, 'destroyFirewallRule'])->name('firewall.destroy');
    Route::get('/snapshots', [VpsPanelController::class, 'snapshots'])->name('snapshots');
    Route::post('/snapshots', [VpsPanelController::class, 'createSnapshot'])->name('snapshots.store');
    Route::post('/snapshots/{snapshotId}/restore', [VpsPanelController::class, 'restoreSnapshot'])->name('snapshots.restore');
    Route::delete('/snapshots/{snapshotId}', [VpsPanelController::class, 'deleteSnapshot'])->name('snapshots.destroy');
    Route::get('/backups', [VpsPanelController::class, 'backups'])->name('backups');
    Route::get('/network', [VpsPanelController::class, 'network'])->name('network');
    Route::put('/network/rdns', [VpsPanelController::class, 'updateRdns'])->name('network.rdns');
    Route::get('/rebuild', [VpsPanelController::class, 'rebuild'])->name('rebuild');
    Route::get('/settings', [VpsPanelController::class, 'settings'])->name('settings');
    Route::put('/settings', [VpsPanelController::class, 'updateSettings'])->name('settings.update');
    Route::get('/activity', [VpsPanelController::class, 'activity'])->name('activity');
});

// Game Panel
Route::prefix('/services/{service}/game-panel')->name('account.services.game-panel.')->group(function () {
    Route::get('/', [GamePanelController::class, 'dashboard'])->name('dashboard');
    Route::get('/console', [GamePanelController::class, 'console'])->name('console');
    Route::get('/files', [GamePanelController::class, 'files'])->name('files');
    Route::get('/databases', [GamePanelController::class, 'databases'])->name('databases');
    Route::get('/backups', [GamePanelController::class, 'backups'])->name('backups');
    Route::get('/schedules', [GamePanelController::class, 'schedules'])->name('schedules');
    Route::get('/startup', [GamePanelController::class, 'startup'])->name('startup');
    Route::get('/settings', [GamePanelController::class, 'settings'])->name('settings');
    Route::get('/sub-users', [GamePanelController::class, 'subUsers'])->name('sub-users');
});

// Dedicated Panel
Route::prefix('/services/{service}/dedicated-panel')->name('account.services.dedicated-panel.')->group(function () {
    Route::get('/', [DedicatedPanelController::class, 'dashboard'])->name('dashboard');
    Route::get('/console', [DedicatedPanelController::class, 'console'])->name('console');
    Route::get('/bandwidth', [DedicatedPanelController::class, 'bandwidth'])->name('bandwidth');
    Route::get('/network', [DedicatedPanelController::class, 'network'])->name('network');
    Route::get('/reinstall', [DedicatedPanelController::class, 'reinstall'])->name('reinstall');
    Route::get('/settings', [DedicatedPanelController::class, 'settings'])->name('settings');
});

// Hosting Panel
Route::prefix('/services/{service}/hosting-panel')->name('account.services.hosting-panel.')->group(function () {
    Route::get('/', [HostingPanelController::class, 'dashboard'])->name('dashboard');
    Route::get('/files', [HostingPanelController::class, 'files'])->name('files');
    Route::get('/databases', [HostingPanelController::class, 'databases'])->name('databases');
    Route::get('/email', [HostingPanelController::class, 'email'])->name('email');
    Route::get('/domains', [HostingPanelController::class, 'domains'])->name('domains');
    Route::get('/dns', [HostingPanelController::class, 'dns'])->name('dns');
    Route::get('/ssl', [HostingPanelController::class, 'ssl'])->name('ssl');
    Route::get('/php', [HostingPanelController::class, 'php'])->name('php');
    Route::get('/cron', [HostingPanelController::class, 'cron'])->name('cron');
    Route::get('/settings', [HostingPanelController::class, 'settings'])->name('settings');
});

// Subscriptions
Route::get('/subscriptions', [SubscriptionController::class, 'index'])->name('account.subscriptions.index');
Route::get('/subscriptions/{subscription}', [SubscriptionController::class, 'show'])->name('account.subscriptions.show');
Route::post('/subscriptions/{subscription}/cancel', [SubscriptionController::class, 'cancel'])->name('account.subscriptions.cancel');
Route::post('/subscriptions/{subscription}/resume', [SubscriptionController::class, 'resume'])->name('account.subscriptions.resume');
Route::post('/subscriptions/{subscription}/swap', [SubscriptionController::class, 'swap'])->name('account.subscriptions.swap');

// Billing
Route::get('/billing', [BillingController::class, 'index'])->name('account.billing.index');
Route::post('/billing/payment-methods', [BillingController::class, 'addPaymentMethod'])->name('account.billing.add-payment-method');
Route::delete('/billing/payment-methods/{paymentMethod}', [BillingController::class, 'removePaymentMethod'])->name('account.billing.remove-payment-method');
Route::post('/billing/payment-methods/default', [BillingController::class, 'setDefaultPaymentMethod'])->name('account.billing.set-default-payment-method');
Route::get('/billing/payment-methods', [BillingController::class, 'paymentMethods'])->name('account.billing.payment-methods');
Route::get('/billing/invoices', [BillingController::class, 'invoices'])->name('account.billing.invoices');
Route::get('/billing/invoices/{invoice}/download', [BillingController::class, 'downloadInvoice'])->name('account.billing.invoices.download');
Route::get('/billing/transactions', [BillingController::class, 'transactions'])->name('account.billing.transactions');
Route::get('/billing/renewals', [BillingController::class, 'upcomingRenewals'])->name('account.billing.renewals');
Route::get('/billing/credits', [BillingController::class, 'credits'])->name('account.billing.credits');
Route::post('/billing/setup-intent', [BillingController::class, 'setupIntent'])->name('account.billing.setup-intent');

// Support Tickets
Route::resource('tickets', TicketController::class)->only(['index', 'create', 'store', 'show'])->names('account.tickets');
Route::post('/tickets/{ticket}/reply', [TicketController::class, 'reply'])->name('account.tickets.reply');
Route::post('/tickets/{ticket}/close', [TicketController::class, 'close'])->name('account.tickets.close');

// Notifications
Route::get('/notifications', [NotificationController::class, 'index'])->name('account.notifications.index');
Route::post('/notifications/{id}/read', [NotificationController::class, 'markAsRead'])->name('account.notifications.read');
Route::post('/notifications/read-all', [NotificationController::class, 'markAllAsRead'])->name('account.notifications.read-all');

// Affiliate
Route::prefix('affiliate')->name('account.affiliate.')->group(function (): void {
    Route::get('/', [AffiliateController::class, 'dashboard'])->name('dashboard');
    Route::get('/referrals', [AffiliateController::class, 'referrals'])->name('referrals');
    Route::get('/commissions', [AffiliateController::class, 'commissions'])->name('commissions');
    Route::get('/payouts', [AffiliateController::class, 'payouts'])->name('payouts');
    Route::post('/payouts', [AffiliateController::class, 'requestPayout'])->name('payouts.request');
    Route::post('/apply', [AffiliateController::class, 'apply'])->name('apply');
});
