<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\RegisteredBusinessController;
use App\Http\Controllers\Admin\AppointmentController;
use App\Http\Controllers\Admin\AppointmentStatusController;
use App\Http\Controllers\Admin\BusinessWorkspaceController;
use App\Http\Controllers\Admin\CustomerController;
use App\Http\Controllers\Admin\CustomerNoteController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\InvoiceController;
use App\Http\Controllers\Admin\InvoicePaymentController;
use App\Http\Controllers\Admin\LanguageController;
use App\Http\Controllers\Admin\PaymentSettingsController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\ReminderController;
use App\Http\Controllers\Admin\ServiceController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\StaffController;
use App\Http\Controllers\Admin\StaffWorkingHoursController;
use App\Http\Controllers\Admin\SubscriptionController as AdminSubscriptionController;
use App\Http\Controllers\Admin\TranslationController;
use App\Http\Controllers\PublicBookingController;
use App\Http\Controllers\SuperAdmin\ActivityLogController as SuperAdminActivityLogController;
use App\Http\Controllers\SuperAdmin\BusinessController as SuperAdminBusinessController;
use App\Http\Controllers\SuperAdmin\DashboardController as SuperAdminDashboardController;
use App\Http\Controllers\SuperAdmin\PaymentController as SuperAdminPaymentController;
use App\Http\Controllers\SuperAdmin\PlanController as SuperAdminPlanController;
use App\Http\Controllers\SuperAdmin\PlatformSettingsController as SuperAdminPlatformSettingsController;
use App\Http\Controllers\SuperAdmin\SubscriptionController as SuperAdminSubscriptionController;
use App\Http\Controllers\SuperAdmin\SupportTicketController as SuperAdminSupportTicketController;
use App\Http\Controllers\SuperAdmin\UserController as SuperAdminUserController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Public marketing routes
|--------------------------------------------------------------------------
*/

Route::view('/', 'landing')->name('landing');
Route::redirect('/landing', '/');
Route::redirect('/features', '/#features')->name('features');
Route::redirect('/pricing', '/#pricing')->name('pricing');
Route::get('/demo', [DashboardController::class, 'demo'])->name('demo');

/*
|--------------------------------------------------------------------------
| Authentication routes
|--------------------------------------------------------------------------
*/

Route::middleware('guest')->group(function (): void {
    Route::get('/login', [AuthenticatedSessionController::class, 'create'])->name('login');
    Route::post('/login', [AuthenticatedSessionController::class, 'store'])->middleware('throttle:auth')->name('login.store');

    Route::get('/register', [RegisteredBusinessController::class, 'create'])->name('register');
    Route::post('/register', [RegisteredBusinessController::class, 'store'])->middleware('throttle:auth')->name('register.store');

    Route::get('/forgot-password', [PasswordResetLinkController::class, 'create'])->name('password.request');
    Route::post('/forgot-password', [PasswordResetLinkController::class, 'store'])->middleware('throttle:auth')->name('password.email');

    Route::get('/reset-password/{token?}', [NewPasswordController::class, 'create'])->name('password.reset');
    Route::post('/reset-password', [NewPasswordController::class, 'store'])->middleware('throttle:auth')->name('password.update');
});

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

Route::middleware('auth')->group(function (): void {
    Route::view('/email/verify', 'auth.verify-email')->name('verification.notice');
    Route::view('/account-created', 'auth.welcome')->name('auth.welcome');
    Route::view('/choose-plan', 'auth.choose-plan')->name('auth.choose-plan');
    Route::view('/trial-started', 'auth.trial-started')->name('auth.trial-started');
});

Route::middleware(['auth', 'role:business_owner,staff', 'tenant'])->group(function (): void {
    Route::get('/businesses', [BusinessWorkspaceController::class, 'index'])->name('admin.businesses.index');
    Route::get('/businesses/create', [BusinessWorkspaceController::class, 'create'])->name('admin.businesses.create');
    Route::post('/businesses', [BusinessWorkspaceController::class, 'store'])->name('admin.businesses.store');
    Route::post('/businesses/{business}/switch', [BusinessWorkspaceController::class, 'switch'])->name('admin.businesses.switch');
});

/*
|--------------------------------------------------------------------------
| Public booking routes
|--------------------------------------------------------------------------
*/

Route::middleware('throttle:public-booking')->group(function (): void {
    Route::get('/book/{business_slug}', [PublicBookingController::class, 'profile'])->name('public.booking.profile');
    Route::get('/book/{business_slug}/services', [PublicBookingController::class, 'services'])->name('public.booking.services');
    Route::get('/book/{business_slug}/staff', [PublicBookingController::class, 'staff'])->name('public.booking.staff');
    Route::get('/book/{business_slug}/date-time', [PublicBookingController::class, 'dateTime'])->name('public.booking.date-time');
    Route::get('/book/{business_slug}/details', [PublicBookingController::class, 'details'])->name('public.booking.details');
    Route::post('/book/{business_slug}/details', [PublicBookingController::class, 'storeDetails'])->name('public.booking.details.store');
    Route::get('/book/{business_slug}/summary', [PublicBookingController::class, 'summary'])->name('public.booking.summary');
    Route::post('/book/{business_slug}/summary', [PublicBookingController::class, 'confirm'])->name('public.booking.confirm');
    Route::get('/book/{business_slug}/confirmation', [PublicBookingController::class, 'confirmation'])->name('public.booking.confirmation');
    Route::get('/book/{business_slug}/failed', [PublicBookingController::class, 'failed'])->name('public.booking.failed');
    Route::get('/book/{business_slug}/reschedule', [PublicBookingController::class, 'reschedule'])->name('public.booking.reschedule');
    Route::post('/book/{business_slug}/reschedule', [PublicBookingController::class, 'updateReschedule'])->name('public.booking.reschedule.update');
    Route::get('/book/{business_slug}/cancel', [PublicBookingController::class, 'cancel'])->name('public.booking.cancel');
    Route::post('/book/{business_slug}/cancel', [PublicBookingController::class, 'cancelStore'])->name('public.booking.cancel.store');
});

/*
|--------------------------------------------------------------------------
| Authenticated business owner/staff routes
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'role:business_owner,staff', 'tenant', 'tenant.model'])->group(function (): void {
    Route::redirect('/admin', '/dashboard')->name('admin.index');
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('admin.dashboard');

    Route::get('/appointments', [AppointmentController::class, 'index'])->name('admin.appointments.index');
    Route::post('/appointments', [AppointmentController::class, 'store'])->name('admin.appointments.store');
    Route::get('/calendar', [AppointmentController::class, 'calendar'])->name('admin.appointments.calendar');
    Route::put('/appointments/{appointment}', [AppointmentController::class, 'update'])->name('admin.appointments.update');
    Route::delete('/appointments/{appointment}', [AppointmentController::class, 'destroy'])->name('admin.appointments.destroy');
    Route::patch('/appointments/{appointment}/status', [AppointmentStatusController::class, 'update'])->name('admin.appointments.status.update');
    Route::get('/appointments/{appointment}', [AppointmentController::class, 'show'])->name('admin.appointments.show');

    Route::get('/services', [ServiceController::class, 'index'])->name('admin.services.index');
    Route::get('/services/create', [ServiceController::class, 'create'])->name('admin.services.create');
    Route::post('/services', [ServiceController::class, 'store'])->name('admin.services.store');
    Route::get('/services/{service}/edit', [ServiceController::class, 'edit'])->name('admin.services.edit');
    Route::put('/services/{service}', [ServiceController::class, 'update'])->name('admin.services.update');
    Route::delete('/services/{service}', [ServiceController::class, 'destroy'])->name('admin.services.destroy');
    Route::get('/services/{service}', [ServiceController::class, 'show'])->name('admin.services.show');

    Route::get('/staff', [StaffController::class, 'index'])->name('admin.staff.index');
    Route::get('/staff/create', [StaffController::class, 'create'])->name('admin.staff.create');
    Route::post('/staff', [StaffController::class, 'store'])->name('admin.staff.store');
    Route::get('/staff/{staff}/edit', [StaffController::class, 'edit'])->name('admin.staff.edit');
    Route::put('/staff/{staff}', [StaffController::class, 'update'])->name('admin.staff.update');
    Route::delete('/staff/{staff}', [StaffController::class, 'destroy'])->name('admin.staff.destroy');
    Route::get('/staff/{staff}/working-hours', [StaffWorkingHoursController::class, 'edit'])->name('admin.staff.working-hours');
    Route::put('/staff/{staff}/working-hours', [StaffWorkingHoursController::class, 'update'])->name('admin.staff.working-hours.update');
    Route::get('/staff/{staff}', [StaffController::class, 'show'])->name('admin.staff.show');

    Route::get('/customers', [CustomerController::class, 'index'])->name('admin.customers.index');
    Route::post('/customers', [CustomerController::class, 'store'])->name('admin.customers.store');
    Route::put('/customers/{customer}', [CustomerController::class, 'update'])->name('admin.customers.update');
    Route::delete('/customers/{customer}', [CustomerController::class, 'destroy'])->name('admin.customers.destroy');
    Route::post('/customers/{customer}/notes', [CustomerNoteController::class, 'store'])->name('admin.customers.notes.store');
    Route::get('/customers/{customer}', [CustomerController::class, 'show'])->name('admin.customers.show');

    Route::get('/invoices', [InvoiceController::class, 'index'])->name('admin.invoices.index');
    Route::get('/invoices/create', [InvoiceController::class, 'create'])->name('admin.invoices.create');
    Route::post('/invoices', [InvoiceController::class, 'store'])->name('admin.invoices.store');
    Route::get('/invoices/{invoice}/edit', [InvoiceController::class, 'edit'])->name('admin.invoices.edit');
    Route::put('/invoices/{invoice}', [InvoiceController::class, 'update'])->name('admin.invoices.update');
    Route::delete('/invoices/{invoice}', [InvoiceController::class, 'destroy'])->name('admin.invoices.destroy');
    Route::post('/invoices/{invoice}/payments', [InvoicePaymentController::class, 'store'])->name('admin.invoices.payments.store');
    Route::get('/invoices/{invoice}/pdf', [InvoiceController::class, 'pdf'])->name('admin.invoices.pdf');
    Route::get('/invoices/{invoice}', [InvoiceController::class, 'show'])->name('admin.invoices.show');

    Route::get('/reminders', [ReminderController::class, 'index'])->name('admin.reminders.index');
    Route::view('/reminders/templates', 'admin.reminders.templates')->name('admin.reminders.templates');
    Route::view('/reminders/templates/create', 'admin.reminders.editor')->name('admin.reminders.templates.create');
    Route::view('/reminders/templates/{template}/edit', 'admin.reminders.editor')->name('admin.reminders.templates.edit');
    Route::get('/reminders/scheduled', [ReminderController::class, 'scheduled'])->name('admin.reminders.scheduled');
    Route::get('/reminders/logs', [ReminderController::class, 'logs'])->name('admin.reminders.logs');
    Route::view('/reminders/whatsapp', 'admin.reminders.whatsapp')->name('admin.reminders.whatsapp');
    Route::view('/reminders/email', 'admin.reminders.email')->name('admin.reminders.email');

    Route::get('/reports', [ReportController::class, 'overview'])->name('admin.reports.index');
    Route::get('/reports/bookings', [ReportController::class, 'bookings'])->name('admin.reports.bookings');
    Route::get('/reports/revenue', [ReportController::class, 'revenue'])->name('admin.reports.revenue');
    Route::get('/reports/staff', [ReportController::class, 'staff'])->name('admin.reports.staff');
    Route::get('/reports/services', [ReportController::class, 'services'])->name('admin.reports.services');
    Route::get('/reports/customers', [ReportController::class, 'customers'])->name('admin.reports.customers');
    Route::get('/reports/invoices', [ReportController::class, 'invoices'])->name('admin.reports.invoices');
    Route::get('/reports/reminders', [ReportController::class, 'reminders'])->name('admin.reports.reminders');

    Route::get('/settings', [SettingsController::class, 'edit'])->defaults('page', 'business')->name('admin.settings.business');
    Route::post('/settings', [SettingsController::class, 'update'])->defaults('page', 'business')->name('admin.settings.business.update');
    Route::get('/settings/booking', [SettingsController::class, 'edit'])->defaults('page', 'booking')->name('admin.settings.booking');
    Route::post('/settings/booking', [SettingsController::class, 'update'])->defaults('page', 'booking')->name('admin.settings.booking.update');
    Route::get('/settings/staff-services', [SettingsController::class, 'edit'])->defaults('page', 'staff-services')->name('admin.settings.staff-services');
    Route::post('/settings/staff-services', [SettingsController::class, 'update'])->defaults('page', 'staff-services')->name('admin.settings.staff-services.update');
    Route::get('/settings/invoices', [SettingsController::class, 'edit'])->defaults('page', 'invoices')->name('admin.settings.invoices');
    Route::post('/settings/invoices', [SettingsController::class, 'update'])->defaults('page', 'invoices')->name('admin.settings.invoices.update');
    Route::get('/settings/payments', [PaymentSettingsController::class, 'edit'])->name('admin.settings.payments');
    Route::post('/settings/payments', [PaymentSettingsController::class, 'update'])->name('admin.settings.payments.update');
    Route::post('/settings/payments/{gateway}/test', [PaymentSettingsController::class, 'test'])->name('admin.settings.payments.test');
    Route::get('/settings/notifications', [SettingsController::class, 'edit'])->defaults('page', 'notifications')->name('admin.settings.notifications');
    Route::post('/settings/notifications', [SettingsController::class, 'update'])->defaults('page', 'notifications')->name('admin.settings.notifications.update');
    Route::get('/settings/whatsapp', [SettingsController::class, 'edit'])->defaults('page', 'whatsapp')->name('admin.settings.whatsapp');
    Route::post('/settings/whatsapp', [SettingsController::class, 'update'])->defaults('page', 'whatsapp')->name('admin.settings.whatsapp.update');
    Route::get('/settings/email-smtp', [SettingsController::class, 'edit'])->defaults('page', 'email-smtp')->name('admin.settings.email-smtp');
    Route::post('/settings/email-smtp', [SettingsController::class, 'update'])->defaults('page', 'email-smtp')->name('admin.settings.email-smtp.update');
    Route::get('/settings/branding', [SettingsController::class, 'edit'])->defaults('page', 'branding')->name('admin.settings.branding');
    Route::post('/settings/branding', [SettingsController::class, 'update'])->defaults('page', 'branding')->name('admin.settings.branding.update');
    Route::get('/settings/localization', [SettingsController::class, 'edit'])->defaults('page', 'localization')->name('admin.settings.localization');
    Route::post('/settings/localization', [SettingsController::class, 'update'])->defaults('page', 'localization')->name('admin.settings.localization.update');
    Route::get('/settings/languages', [LanguageController::class, 'index'])->name('admin.settings.languages.index');
    Route::get('/settings/languages/create', [LanguageController::class, 'create'])->name('admin.settings.languages.create');
    Route::post('/settings/languages', [LanguageController::class, 'store'])->name('admin.settings.languages.store');
    Route::get('/settings/languages/{language}/edit', [LanguageController::class, 'edit'])->name('admin.settings.languages.edit');
    Route::put('/settings/languages/{language}', [LanguageController::class, 'update'])->name('admin.settings.languages.update');
    Route::patch('/settings/languages/{language}/default', [LanguageController::class, 'makeDefault'])->name('admin.settings.languages.default');
    Route::patch('/settings/languages/{language}/toggle', [LanguageController::class, 'toggle'])->name('admin.settings.languages.toggle');
    Route::delete('/settings/languages/{language}', [LanguageController::class, 'destroy'])->name('admin.settings.languages.destroy');
    Route::get('/settings/translations', [TranslationController::class, 'index'])->name('admin.settings.translations');
    Route::post('/settings/translations', [TranslationController::class, 'update'])->name('admin.settings.translations.update');
    Route::get('/settings/translations/export', [TranslationController::class, 'export'])->name('admin.settings.translations.export');
    Route::get('/settings/translations/sample', [TranslationController::class, 'sample'])->name('admin.settings.translations.sample');
    Route::get('/settings/translations/import-export', [TranslationController::class, 'importExport'])->name('admin.settings.translations.import-export');
    Route::post('/settings/translations/import', [TranslationController::class, 'import'])->name('admin.settings.translations.import');
    Route::get('/settings/security', [SettingsController::class, 'edit'])->defaults('page', 'security')->name('admin.settings.security');
    Route::post('/settings/security', [SettingsController::class, 'update'])->defaults('page', 'security')->name('admin.settings.security.update');
    Route::get('/settings/billing', [SettingsController::class, 'edit'])->defaults('page', 'billing')->name('admin.settings.billing');
    Route::post('/settings/billing', [SettingsController::class, 'update'])->defaults('page', 'billing')->name('admin.settings.billing.update');

    Route::get('/subscription', [AdminSubscriptionController::class, 'current'])->name('admin.subscription.current');
    Route::get('/subscription/upgrade', [AdminSubscriptionController::class, 'upgrade'])->name('admin.subscription.upgrade');
    Route::post('/subscription/upgrade', [AdminSubscriptionController::class, 'storeUpgrade'])->name('admin.subscription.upgrade.store');
    Route::get('/subscription/billing-history', [AdminSubscriptionController::class, 'billingHistory'])->name('admin.subscription.billing-history');
    Route::get('/subscription/payment-method', [AdminSubscriptionController::class, 'paymentMethod'])->name('admin.subscription.payment-method');
    Route::get('/subscription/usage-limits', [AdminSubscriptionController::class, 'usageLimits'])->name('admin.subscription.usage-limits');
    Route::get('/subscription/expired', [AdminSubscriptionController::class, 'expired'])->name('admin.subscription.expired');
    Route::get('/subscription/trial-ending', [AdminSubscriptionController::class, 'trialEnding'])->name('admin.subscription.trial-ending');
    Route::get('/subscription/upgrade-success', [AdminSubscriptionController::class, 'upgradeSuccess'])->name('admin.subscription.upgrade-success');
});

/*
|--------------------------------------------------------------------------
| Super admin platform routes
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'role:super_admin'])->group(function (): void {
    Route::redirect('/super-admin', '/super-admin/dashboard');
    Route::get('/super-admin/dashboard', SuperAdminDashboardController::class)->name('super-admin.dashboard');
    Route::get('/super-admin/businesses', [SuperAdminBusinessController::class, 'index'])->name('super-admin.businesses.index');
    Route::get('/super-admin/businesses/{business}', [SuperAdminBusinessController::class, 'show'])->name('super-admin.businesses.show');
    Route::patch('/super-admin/businesses/{business}/suspend', [SuperAdminBusinessController::class, 'suspend'])->name('super-admin.businesses.suspend');
    Route::patch('/super-admin/businesses/{business}/activate', [SuperAdminBusinessController::class, 'activate'])->name('super-admin.businesses.activate');
    Route::post('/super-admin/businesses/{business}/login-as-owner', [SuperAdminBusinessController::class, 'loginAsOwner'])->name('super-admin.businesses.login-as-owner');
    Route::get('/super-admin/plans', [SuperAdminPlanController::class, 'index'])->name('super-admin.plans.index');
    Route::get('/super-admin/plans/create', [SuperAdminPlanController::class, 'create'])->name('super-admin.plans.create');
    Route::post('/super-admin/plans', [SuperAdminPlanController::class, 'store'])->name('super-admin.plans.store');
    Route::get('/super-admin/plans/{plan}/edit', [SuperAdminPlanController::class, 'edit'])->name('super-admin.plans.edit');
    Route::put('/super-admin/plans/{plan}', [SuperAdminPlanController::class, 'update'])->name('super-admin.plans.update');
    Route::get('/super-admin/subscriptions', [SuperAdminSubscriptionController::class, 'index'])->name('super-admin.subscriptions.index');
    Route::get('/super-admin/subscriptions/{subscription}', [SuperAdminSubscriptionController::class, 'show'])->name('super-admin.subscriptions.show');
    Route::get('/super-admin/payments', [SuperAdminPaymentController::class, 'index'])->name('super-admin.payments.index');
    Route::get('/super-admin/users', [SuperAdminUserController::class, 'index'])->name('super-admin.users.index');
    Route::get('/super-admin/support-tickets', [SuperAdminSupportTicketController::class, 'index'])->name('super-admin.support.index');
    Route::get('/super-admin/settings', [SuperAdminPlatformSettingsController::class, 'system'])->name('super-admin.settings.system');
    Route::post('/super-admin/settings/{group}', [SuperAdminPlatformSettingsController::class, 'update'])->name('super-admin.settings.update');
    Route::get('/super-admin/settings/landing-page', [SuperAdminPlatformSettingsController::class, 'landing'])->name('super-admin.settings.landing');
    Route::get('/super-admin/settings/email', [SuperAdminPlatformSettingsController::class, 'email'])->name('super-admin.settings.email');
    Route::get('/super-admin/settings/whatsapp', [SuperAdminPlatformSettingsController::class, 'whatsapp'])->name('super-admin.settings.whatsapp');
    Route::get('/super-admin/activity-logs', [SuperAdminActivityLogController::class, 'index'])->name('super-admin.activity.index');
});
