<?php

declare(strict_types=1);

use App\Http\Controllers\Account\BillingController;
use App\Http\Controllers\Account\CheckoutController;
use App\Http\Controllers\Account\DashboardController;
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\SubscriptionController;
use App\Http\Controllers\Account\TicketController;
use App\Http\Controllers\Account\UpgradeController;
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::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');

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

// 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/{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');
});

// 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::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');
