<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\Portal\AddFundsController;
use App\Http\Controllers\Portal\CheckoutController;
use App\Http\Controllers\Portal\GoogleAuthController;
use App\Http\Controllers\Portal\InvoicePaymentController;
use App\Http\Controllers\Portal\PortalController;
use App\Http\Controllers\Portal\StoreController;
use App\Http\Controllers\Portal\TicketController;
use App\Http\Controllers\Portal\ManualPaymentController;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

Route::get('/', function () {
    return auth()->check()
        ? redirect('/client/dashboard')
        : redirect('/login');
});

Route::view('/contact-us', 'client.support')->name('contact');

Route::get('/store', [StoreController::class, 'index'])->name('store.index');
Route::get('/store/{group}', [StoreController::class, 'group'])->name('store.group');
Route::get('/store/web-hosting', [StoreController::class, 'group'])->defaults('group', 'web-hosting')->name('store.hosting');
Route::get('/store/professional-email', [StoreController::class, 'group'])->defaults('group', 'business-email')->name('store.email');

Route::get('/cart', [CheckoutController::class, 'index'])->name('cart.index');

Route::match(['get', 'post'], '/domain-search', function (Request $request) {
    $params = ['operation' => $request->input('operation', 'register')];

    if ($request->filled('domain_name')) {
        $params['domain_check'] = 1;
        $params['domain_name'] = $request->input('domain_name');
    }

    return redirect(route('checkout.index', $params) . '#domain-search');
})->name('domains.search');

Route::get('/domain', function () {
    return redirect(route('domains.search'));
})->name('domains.simple');

Route::get('/domains/register', function () {
    return redirect(route('checkout.index', ['operation' => 'register']) . '#domain-search');
})->name('domains.register');

Route::get('/domain/register', function () {
    return redirect(route('domains.register'));
})->name('domains.register.simple');

Route::get('/domains/transfer', function () {
    return redirect(route('checkout.index', ['operation' => 'transfer']) . '#domain-search');
})->name('domains.transfer');

Route::get('/domain/transfer', function () {
    return redirect(route('domains.transfer'));
})->name('domains.transfer.simple');

Route::post('/currency/browser-country', [CheckoutController::class, 'browserCountry'])->middleware('throttle:zt-domain-search')->name('currency.browser-country');
Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout.index');
Route::get('/cart/add', [CheckoutController::class, 'addToCartLink'])->middleware('throttle:zt-checkout')->name('cart.add-link');
Route::post('/checkout/domain-search', [CheckoutController::class, 'domainSearchAjax'])->middleware('throttle:zt-domain-search')->name('checkout.domain-search');
Route::post('/checkout/domain-suggestion', [CheckoutController::class, 'domainSuggestionAjax'])->middleware('throttle:zt-domain-search')->name('checkout.domain-suggestion');
Route::get('/checkout/cart-summary', [CheckoutController::class, 'cartSummary'])->name('checkout.cart-summary');
Route::get('/product/cart/add', [CheckoutController::class, 'addProductLink'])->middleware('throttle:zt-checkout')->name('cart.add-product-link');
Route::get('/domain/cart/add', [CheckoutController::class, 'addDomainLink'])->middleware('throttle:zt-domain-search')->name('cart.add-domain-link');
Route::post('/cart/add-product', [CheckoutController::class, 'addProduct'])->middleware('throttle:zt-checkout')->name('cart.add-product');
Route::post('/cart/add-domain', [CheckoutController::class, 'addDomain'])->middleware('throttle:zt-domain-search')->name('cart.add-domain');
Route::patch('/cart/items/{cartItem}', [CheckoutController::class, 'updateItem'])->middleware('throttle:zt-checkout')->name('cart.update-item');
Route::delete('/cart/items/{cartItem}', [CheckoutController::class, 'removeItem'])->middleware('throttle:zt-checkout')->name('cart.remove-item');
Route::post('/cart/promotion', [CheckoutController::class, 'applyPromotion'])->middleware('throttle:zt-checkout')->name('cart.promotion.apply');
Route::delete('/cart/promotion', [CheckoutController::class, 'removePromotion'])->middleware('throttle:zt-checkout')->name('cart.promotion.remove');
Route::post('/checkout/account', [CheckoutController::class, 'saveCheckoutDetails'])->middleware('throttle:zt-checkout')->name('checkout.account');
Route::post('/checkout/complete', [CheckoutController::class, 'complete'])->middleware('throttle:zt-checkout')->name('checkout.complete');

Route::get('/auth/google/redirect', [GoogleAuthController::class, 'redirect'])->name('google.redirect');
Route::get('/auth/google/callback', [GoogleAuthController::class, 'callback'])->name('google.callback');

Route::match(['get', 'post'], '/client/payfast/success', [InvoicePaymentController::class, 'payfastSuccess'])
    ->middleware('throttle:zt-checkout')
    ->name('client.invoices.payfast.success');
Route::match(['get', 'post'], '/client/payfast/failure', [InvoicePaymentController::class, 'payfastFailure'])
    ->middleware('throttle:zt-checkout')
    ->name('client.invoices.payfast.failure');

Route::match(['get', 'post'], '/client/add-funds/payfast/success', [AddFundsController::class, 'payfastSuccess'])
    ->middleware('throttle:zt-checkout')
    ->name('client.add-funds.payfast.success');
Route::match(['get', 'post'], '/client/add-funds/payfast/failure', [AddFundsController::class, 'payfastFailure'])
    ->middleware('throttle:zt-checkout')
    ->name('client.add-funds.payfast.failure');

Route::middleware(['auth'])->prefix('client')->group(function () {
    Route::get('/dashboard', [PortalController::class, 'dashboard'])->name('client.dashboard');
    Route::get('/services', [PortalController::class, 'services'])->name('client.services');
    Route::post('/services/refresh', [PortalController::class, 'refreshServices'])->name('client.services.refresh');
    Route::get('/services/{service}', [PortalController::class, 'showService'])->whereNumber('service')->name('client.services.show');
    Route::get('/services/{service}/cpanel-sso', [PortalController::class, 'openServiceCpanel'])->whereNumber('service')->name('client.services.cpanel-sso');
    Route::post('/services/{service}/change-password', [PortalController::class, 'changeServicePassword'])->whereNumber('service')->name('client.services.change-password');
    Route::get('/services/{service}/upgrade', [PortalController::class, 'upgradeService'])->whereNumber('service')->name('client.services.upgrade');
    Route::post('/services/{service}/upgrade', [PortalController::class, 'submitUpgradeService'])->whereNumber('service')->middleware('throttle:zt-checkout')->name('client.services.upgrade.submit');
    Route::get('/services/{service}/open-whmcs', [PortalController::class, 'openServiceWhmcs'])->whereNumber('service')->name('client.services.open-whmcs');
    Route::get('/services/addons', fn () => view('client.static-page', [
        'heading' => 'Available Addons',
        'subtitle' => 'Optional features for your hosting services.',
        'message' => 'No addons are available for self-service purchase right now. Contact support for available options.',
        'primaryUrl' => route('client.services'),
        'primaryLabel' => 'Back to My Services',
    ]))->name('client.addons');
    Route::get('/domains', [PortalController::class, 'domains'])->name('client.domains');
    Route::post('/domains/refresh', [PortalController::class, 'refreshDomains'])->name('client.domains.refresh');
    Route::get('/domains/renew', [PortalController::class, 'domains'])->name('client.domains.renew');
    Route::get('/domains/{domain}', [PortalController::class, 'showDomain'])->whereNumber('domain')->name('client.domains.show');
    Route::post('/domains/{domain}/renew', [PortalController::class, 'renewDomain'])->whereNumber('domain')->middleware('throttle:zt-checkout')->name('client.domains.renew.submit');
    Route::post('/domains/{domain}/nameservers', [PortalController::class, 'updateDomainNameservers'])->whereNumber('domain')->middleware('throttle:zt-checkout')->name('client.domains.nameservers.update');
    Route::get('/invoices', [PortalController::class, 'invoices'])->name('client.invoices');
    Route::post('/invoices/refresh', [PortalController::class, 'refreshInvoices'])->name('client.invoices.refresh');
    Route::get('/invoices/{invoice}', [InvoicePaymentController::class, 'show'])->whereNumber('invoice')->name('client.invoices.show');
    Route::post('/invoices/{invoice}/pay', [InvoicePaymentController::class, 'pay'])->whereNumber('invoice')->middleware('throttle:zt-checkout')->name('client.invoices.pay');
    Route::post('/invoices/{invoice}/manual-payment', [ManualPaymentController::class, 'store'])->whereNumber('invoice')->middleware('throttle:zt-checkout')->name('client.invoices.manual-payment');
    Route::match(['get', 'post'], '/invoices/{invoice}/payfast', [InvoicePaymentController::class, 'payfast'])->whereNumber('invoice')->middleware('throttle:zt-checkout')->name('client.invoices.payfast');
    Route::get('/quotes', fn () => view('client.static-page', [
        'heading' => 'My Quotes',
        'subtitle' => 'Review quotes issued for your account.',
        'message' => 'No quotes have been issued for your account yet.',
        'primaryUrl' => route('client.invoices'),
        'primaryLabel' => 'View My Invoices',
    ]))->name('client.quotes');
    Route::get('/mass-payment', fn () => view('client.static-page', [
        'heading' => 'Mass Payment',
        'subtitle' => 'Pay multiple invoices together.',
        'message' => 'Combined invoice payment is not available right now. You can review and pay each invoice from My Invoices.',
        'primaryUrl' => route('client.invoices'),
        'primaryLabel' => 'View My Invoices',
    ]))->name('client.mass-payment');
    Route::get('/add-funds', [AddFundsController::class, 'index'])->name('client.add-funds');
    Route::post('/add-funds', [AddFundsController::class, 'store'])
        ->middleware('throttle:zt-checkout')
        ->name('client.add-funds.store');
    Route::get('/support', [TicketController::class, 'index'])->name('client.support');
    Route::post('/support', [TicketController::class, 'store'])->middleware('throttle:zt-tickets')->name('client.support.store');
    Route::get('/announcements', fn () => view('client.static-page', [
        'heading' => 'Announcements',
        'subtitle' => 'Latest hosting and account notices.',
        'message' => 'There are no announcements at this time.',
        'primaryUrl' => route('client.support'),
        'primaryLabel' => 'Go to Tickets',
    ]))->name('client.announcements');
    Route::get('/knowledgebase', fn () => view('client.static-page', [
        'heading' => 'Knowledgebase',
        'subtitle' => 'Help articles and client guides.',
        'message' => 'Help articles are being prepared. Contact support for assistance.',
        'primaryUrl' => route('client.support'),
        'primaryLabel' => 'Go to Tickets',
    ]))->name('client.knowledgebase');
    Route::get('/downloads', fn () => view('client.static-page', [
        'heading' => 'Downloads',
        'subtitle' => 'Client downloads and useful resources.',
        'message' => 'There are no downloads available for your account.',
        'primaryUrl' => route('client.support'),
        'primaryLabel' => 'Go to Tickets',
    ]))->name('client.downloads');
    Route::get('/network-status', fn () => view('client.static-page', [
        'heading' => 'Network Status',
        'subtitle' => 'Service availability and network notices.',
        'message' => 'No network notices are currently available. Contact support if you are experiencing an issue.',
        'primaryUrl' => route('client.support'),
        'primaryLabel' => 'Go to Tickets',
    ]))->name('client.network-status');
    Route::get('/affiliates', fn () => view('client.static-page', [
        'heading' => 'Affiliates',
        'subtitle' => 'Affiliate program section.',
        'message' => 'The affiliate program is not currently available from this portal.',
        'primaryUrl' => route('client.dashboard'),
        'primaryLabel' => 'Back to Dashboard',
    ]))->name('client.affiliates');
    Route::get('/support/{ticket}', [TicketController::class, 'show'])->name('client.support.show');
    Route::post('/support/{ticket}/reply', [TicketController::class, 'reply'])->middleware('throttle:zt-tickets')->name('client.support.reply');
});

Route::get('/dashboard', [PortalController::class, 'dashboard'])->middleware(['auth'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';
