<?php

use App\Http\Controllers\MeetupAdminController;
use App\Http\Controllers\MeetupController;
use App\Http\Controllers\MeetupSuperAdminController;
use App\Http\Controllers\MexcoCheckoutController;
use App\Http\Controllers\MexcoPortalAuthController;
use App\Http\Controllers\MexcoPortalController;
use App\Http\Controllers\MexcoPublicController;
use App\Http\Controllers\MexcoSuperAdminAddonController;
use App\Http\Controllers\MexcoSuperAdminFeatureController;
use App\Http\Controllers\MexcoSuperAdminAdminUserController;
use App\Http\Controllers\MexcoSuperAdminAnalyticsController;
use App\Http\Controllers\MexcoSuperAdminAuditController;
use App\Http\Controllers\MexcoSuperAdminBillingController;
use App\Http\Controllers\MexcoSuperAdminCampusController;
use App\Http\Controllers\MexcoSuperAdminHealthController;
use App\Http\Controllers\MexcoSuperAdminInvoiceController;
use App\Http\Controllers\MexcoSuperAdminNotificationController;
use App\Http\Controllers\MexcoSuperAdminOrganisationController;
use App\Http\Controllers\MexcoSuperAdminOrganiserController;
use App\Http\Controllers\MexcoSuperAdminPlanController;
use App\Http\Controllers\MexcoSuperAdminPromoController;
use App\Http\Controllers\MexcoSuperAdminReportsController;
use App\Http\Controllers\MexcoSuperAdminSettingsController;
use App\Http\Controllers\MexcoSuperAdminSupportController;
use Illuminate\Support\Facades\Route;

/*
| Public website (mymexco.com)
*/
Route::get('/', [MexcoPublicController::class, 'home'])->name('mexco.home');
Route::get('/features', [MexcoPublicController::class, 'features'])->name('mexco.features');
Route::get('/plans', [MexcoPublicController::class, 'plans'])->name('mexco.plans');
Route::get('/plans/choose/{planCode}', [MexcoPublicController::class, 'choosePlan'])->name('mexco.plans.choose');
Route::get('/demo', [MexcoPublicController::class, 'demo'])->name('mexco.demo');
Route::get('/campus', [MexcoPublicController::class, 'campus'])->name('mexco.campus');
Route::get('/campus/pending', [MexcoPublicController::class, 'campusPending'])->name('mexco.campus.pending');
Route::get('/how-it-works', [MexcoPublicController::class, 'howItWorks'])->name('mexco.how');
Route::get('/enterprise', [MexcoPublicController::class, 'enterprise'])->name('mexco.enterprise');
Route::get('/contact', [MexcoPublicController::class, 'contact'])->name('mexco.contact');

/*
| My MeXCo — customer auth
*/
Route::get('/login', [MexcoPortalAuthController::class, 'showLogin'])->name('mexco.login');
Route::post('/login', [MexcoPortalAuthController::class, 'login'])->name('mexco.login.submit');
Route::get('/register', [MexcoPortalAuthController::class, 'showRegister'])->name('mexco.register');
Route::post('/register', [MexcoPortalAuthController::class, 'register'])->name('mexco.register.submit');
Route::post('/logout', [MexcoPortalAuthController::class, 'logout'])->name('mexco.logout');

/*
| My MeXCo — portal + checkout
*/
Route::middleware('mexco.portal')->group(function () {
    Route::get('/portal', [MexcoPortalController::class, 'dashboard'])->name('mexco.portal.dashboard');
    Route::get('/portal/events', [MexcoPortalController::class, 'events'])->name('mexco.portal.events');
    Route::match(['get', 'post'], '/portal/demo/start', [MexcoPortalController::class, 'startDemo'])->name('mexco.portal.demo.start');
    Route::get('/portal/campus/apply', [MexcoPortalController::class, 'showCampusApply'])->name('mexco.portal.campus.apply');
    Route::post('/portal/campus/apply', [MexcoPortalController::class, 'submitCampusApply'])->name('mexco.portal.campus.apply.submit');

    Route::get('/checkout/start/{plan}', [MexcoCheckoutController::class, 'start'])->name('mexco.checkout.start');
    Route::get('/checkout/event', [MexcoCheckoutController::class, 'showEvent'])->name('mexco.checkout.event');
    Route::post('/checkout/event', [MexcoCheckoutController::class, 'storeEvent'])->name('mexco.checkout.event.store');
    Route::get('/checkout/addons', [MexcoCheckoutController::class, 'showAddons'])->name('mexco.checkout.addons');
    Route::post('/checkout/addons', [MexcoCheckoutController::class, 'storeAddons'])->name('mexco.checkout.addons.store');
    Route::get('/checkout/review', [MexcoCheckoutController::class, 'showReview'])->name('mexco.checkout.review');
    Route::post('/checkout/review', [MexcoCheckoutController::class, 'submitReview'])->name('mexco.checkout.review.submit');
    Route::get('/checkout/payment', [MexcoCheckoutController::class, 'showPayment'])->name('mexco.checkout.payment');
    Route::get('/checkout/resume/{order}', [MexcoCheckoutController::class, 'resume'])->name('mexco.checkout.resume');
});

Route::prefix('mexco')->group(function () {
    Route::get('/', [MeetupController::class, 'index']);
    Route::get('/committee', [MeetupController::class, 'committee'])->name('mexco.committee');

    Route::prefix('admin')->name('mexco.admin.')->group(function () {
        Route::get('/login', [MeetupAdminController::class, 'showLogin'])->name('login');
        Route::post('/login', [MeetupAdminController::class, 'login'])->name('login.submit');
        Route::get('/select-event', [MeetupAdminController::class, 'showSelectEvent'])->name('select-event');
        Route::post('/select-event', [MeetupAdminController::class, 'selectEvent'])->name('select-event.submit');
        Route::post('/logout', [MeetupAdminController::class, 'logout'])->name('logout');

        Route::middleware('mexco.admin')->group(function () {
            Route::get('/', [MeetupAdminController::class, 'dashboard'])->name('dashboard');

            Route::get('/participants', [MeetupAdminController::class, 'participants'])->name('participants');
            Route::get('/participants/create', [MeetupAdminController::class, 'createParticipant'])->name('participants.create');
            Route::post('/participants/lookup', [MeetupAdminController::class, 'lookupParticipant'])->name('participants.lookup');
            Route::post('/participants', [MeetupAdminController::class, 'storeParticipant'])->name('participants.store');
            Route::get('/participants/export', [MeetupAdminController::class, 'exportParticipants'])->name('participants.export');
            Route::get('/participants/template', [MeetupAdminController::class, 'downloadParticipantTemplate'])->name('participants.template');
            Route::post('/participants/import', [MeetupAdminController::class, 'importParticipants'])->name('participants.import');
            Route::get('/participants/{slug}/edit', [MeetupAdminController::class, 'editParticipant'])->name('participants.edit');
            Route::put('/participants/{slug}', [MeetupAdminController::class, 'updateParticipant'])->name('participants.update');
            Route::delete('/participants/{slug}', [MeetupAdminController::class, 'destroyParticipant'])->name('participants.destroy');
            Route::post('/participants/{slug}/verify', [MeetupAdminController::class, 'verifyParticipant'])->name('participants.verify');

            Route::get('/committee', [MeetupAdminController::class, 'committeeAccounts'])->name('committee');
            Route::get('/committee/create', [MeetupAdminController::class, 'createCommitteeAccount'])->name('committee.create');
            Route::post('/committee/lookup', [MeetupAdminController::class, 'lookupCommitteeAccount'])->name('committee.lookup');
            Route::post('/committee', [MeetupAdminController::class, 'storeCommitteeAccount'])->name('committee.store');
            Route::get('/committee/{slug}/edit', [MeetupAdminController::class, 'editCommitteeAccount'])->name('committee.edit');
            Route::put('/committee/{slug}', [MeetupAdminController::class, 'updateCommitteeAccount'])->name('committee.update');
            Route::delete('/committee/{slug}', [MeetupAdminController::class, 'destroyCommitteeAccount'])->name('committee.destroy');

            Route::get('/roles', [MeetupAdminController::class, 'roles'])->name('roles');
            Route::put('/roles', [MeetupAdminController::class, 'updateRoles'])->name('roles.update');

            Route::get('/sessions', [MeetupAdminController::class, 'sessions'])->name('sessions');
            Route::get('/sessions/create', [MeetupAdminController::class, 'createSession'])->name('sessions.create');
            Route::post('/sessions', [MeetupAdminController::class, 'storeSession'])->name('sessions.store');
            Route::get('/sessions/tentative', [MeetupAdminController::class, 'sessionsTentative'])->name('sessions.tentative');
            Route::get('/sessions/{session}/edit', [MeetupAdminController::class, 'editSession'])->name('sessions.edit');
            Route::put('/sessions/{session}', [MeetupAdminController::class, 'updateSession'])->name('sessions.update');
            Route::post('/sessions/{session}/toggle-publish', [MeetupAdminController::class, 'toggleSessionPublish'])->name('sessions.toggle-publish');
            Route::delete('/sessions/{session}', [MeetupAdminController::class, 'destroySession'])->name('sessions.destroy');

            Route::get('/booths', [MeetupAdminController::class, 'booths'])->name('booths');
            Route::get('/booths/export', [MeetupAdminController::class, 'exportBooths'])->name('booths.export');
            Route::get('/booths/print-all', [MeetupAdminController::class, 'printAllBooths'])->name('booths.print-all');
            Route::get('/booths/create', [MeetupAdminController::class, 'createBooth'])->name('booths.create');
            Route::post('/booths', [MeetupAdminController::class, 'storeBooth'])->name('booths.store');
            Route::post('/booths/toggle-all-voting', [MeetupAdminController::class, 'toggleAllBoothVoting'])->name('booths.toggle-all-voting');
            Route::get('/booths/{booth}/print', [MeetupAdminController::class, 'printBooth'])->name('booths.print');
            Route::get('/booths/{booth}', [MeetupAdminController::class, 'showBooth'])->name('booths.show');
            Route::post('/booths/{booth}/toggle-status', [MeetupAdminController::class, 'toggleBoothStatus'])->name('booths.toggle-status');
            Route::post('/booths/{booth}/toggle-voting', [MeetupAdminController::class, 'toggleBoothVoting'])->name('booths.toggle-voting');
            Route::get('/booths/{booth}/edit', [MeetupAdminController::class, 'editBooth'])->name('booths.edit');
            Route::put('/booths/{booth}', [MeetupAdminController::class, 'updateBoothRecord'])->name('booths.update');
            Route::delete('/booths/{booth}', [MeetupAdminController::class, 'destroyBooth'])->name('booths.destroy');
            Route::put('/booths-bulk', [MeetupAdminController::class, 'updateBooths'])->name('booths.update.bulk');

            Route::get('/certificates', [MeetupAdminController::class, 'certificates'])->name('certificates');
            Route::get('/certificates/{key}/edit', [MeetupAdminController::class, 'editCertificateTemplate'])->name('certificates.edit');
            Route::put('/certificates/{key}', [MeetupAdminController::class, 'updateCertificateTemplate'])->name('certificates.update');

            Route::get('/settings', [MeetupAdminController::class, 'settings'])->name('settings');
            Route::put('/settings', [MeetupAdminController::class, 'updateSettings'])->name('settings.update');

            Route::post('/notifications/{notice}/read', [MeetupAdminController::class, 'markNoticeRead'])->name('notifications.read');
            Route::post('/notifications/read-all', [MeetupAdminController::class, 'markAllNoticesRead'])->name('notifications.read-all');
        });
    });

    Route::prefix('super-admin')->name('mexco.super.')->group(function () {
        Route::get('/login', [MeetupSuperAdminController::class, 'showLogin'])->name('login');
        Route::post('/login', [MeetupSuperAdminController::class, 'login'])->name('login.submit');
        Route::post('/logout', [MeetupSuperAdminController::class, 'logout'])->name('logout');

        Route::middleware('mexco.super')->group(function () {
            Route::get('/', [MeetupSuperAdminController::class, 'dashboard'])->name('dashboard');
            Route::get('/events', [MeetupSuperAdminController::class, 'events'])->name('events');
            Route::get('/events/create', [MeetupSuperAdminController::class, 'createEvent'])->name('events.create');
            Route::post('/events', [MeetupSuperAdminController::class, 'storeEvent'])->name('events.store');
            Route::get('/events/{eventId}/edit', [MeetupSuperAdminController::class, 'editEvent'])->name('events.edit');
            Route::put('/events/{eventId}', [MeetupSuperAdminController::class, 'updateEvent'])->name('events.update');
            Route::delete('/events/{eventId}', [MeetupSuperAdminController::class, 'destroyEvent'])->name('events.destroy');
            Route::post('/events/{eventId}/organiser', [MeetupSuperAdminController::class, 'enterOrganiser'])->name('events.organiser');

            Route::get('/plans', [MexcoSuperAdminPlanController::class, 'index'])->name('plans');
            Route::get('/plans/{plan}/edit', [MexcoSuperAdminPlanController::class, 'edit'])->name('plans.edit');
            Route::put('/plans/{plan}', [MexcoSuperAdminPlanController::class, 'update'])->name('plans.update');
            Route::get('/features', [MexcoSuperAdminFeatureController::class, 'index'])->name('features');
            Route::get('/addons', [MexcoSuperAdminAddonController::class, 'index'])->name('addons');
            Route::get('/addons/{addon}/edit', [MexcoSuperAdminAddonController::class, 'edit'])->name('addons.edit');
            Route::put('/addons/{addon}', [MexcoSuperAdminAddonController::class, 'update'])->name('addons.update');

            Route::get('/organisations', [MexcoSuperAdminOrganisationController::class, 'index'])->name('organisations');
            Route::get('/organisations/create', [MexcoSuperAdminOrganisationController::class, 'create'])->name('organisations.create');
            Route::post('/organisations', [MexcoSuperAdminOrganisationController::class, 'store'])->name('organisations.store');
            Route::get('/organisations/{organisation}', [MexcoSuperAdminOrganisationController::class, 'show'])->name('organisations.show');
            Route::get('/organisations/{organisation}/edit', [MexcoSuperAdminOrganisationController::class, 'edit'])->name('organisations.edit');
            Route::put('/organisations/{organisation}', [MexcoSuperAdminOrganisationController::class, 'update'])->name('organisations.update');
            Route::post('/organisations/{organisation}/suspend', [MexcoSuperAdminOrganisationController::class, 'suspend'])->name('organisations.suspend');
            Route::post('/organisations/{organisation}/activate', [MexcoSuperAdminOrganisationController::class, 'activate'])->name('organisations.activate');
            Route::post('/organisations/{organisation}/credits', [MexcoSuperAdminOrganisationController::class, 'storeCredit'])->name('organisations.credits.store');

            Route::get('/organisers', [MexcoSuperAdminOrganiserController::class, 'index'])->name('organisers');
            Route::get('/organisers/create', [MexcoSuperAdminOrganiserController::class, 'create'])->name('organisers.create');
            Route::post('/organisers', [MexcoSuperAdminOrganiserController::class, 'store'])->name('organisers.store');
            Route::get('/organisers/{organiser}/edit', [MexcoSuperAdminOrganiserController::class, 'edit'])->name('organisers.edit');
            Route::put('/organisers/{organiser}', [MexcoSuperAdminOrganiserController::class, 'update'])->name('organisers.update');

            Route::get('/campus', [MexcoSuperAdminCampusController::class, 'index'])->name('campus');
            Route::get('/campus/create', [MexcoSuperAdminCampusController::class, 'create'])->name('campus.create');
            Route::post('/campus', [MexcoSuperAdminCampusController::class, 'store'])->name('campus.store');
            Route::get('/campus/{application}', [MexcoSuperAdminCampusController::class, 'show'])->name('campus.show');
            Route::get('/campus/{application}/letter', [MexcoSuperAdminCampusController::class, 'downloadLetter'])->name('campus.letter');
            Route::post('/campus/{application}/review', [MexcoSuperAdminCampusController::class, 'review'])->name('campus.review');
            Route::post('/campus/{application}/approve', [MexcoSuperAdminCampusController::class, 'approve'])->name('campus.approve');
            Route::post('/campus/{application}/reject', [MexcoSuperAdminCampusController::class, 'reject'])->name('campus.reject');
            Route::post('/campus/{application}/extend-retention', [MexcoSuperAdminCampusController::class, 'extendRetention'])->name('campus.extend-retention');

            Route::get('/billing', [MexcoSuperAdminBillingController::class, 'index'])->name('billing');
            Route::get('/invoices', [MexcoSuperAdminInvoiceController::class, 'index'])->name('invoices');
            Route::get('/invoices/{invoice}', [MexcoSuperAdminInvoiceController::class, 'show'])->name('invoices.show');
            Route::post('/invoices/{invoice}/issue', [MexcoSuperAdminInvoiceController::class, 'issue'])->name('invoices.issue');
            Route::post('/invoices/{invoice}/cancel', [MexcoSuperAdminInvoiceController::class, 'cancel'])->name('invoices.cancel');
            Route::post('/invoices/{invoice}/promo', [MexcoSuperAdminInvoiceController::class, 'applyPromo'])->name('invoices.promo');
            Route::post('/invoices/{invoice}/credit', [MexcoSuperAdminInvoiceController::class, 'applyCredit'])->name('invoices.credit');
            Route::post('/invoices/{invoice}/payment', [MexcoSuperAdminInvoiceController::class, 'recordPayment'])->name('invoices.payment');
            Route::get('/promos', [MexcoSuperAdminPromoController::class, 'index'])->name('promos');
            Route::get('/promos/create', [MexcoSuperAdminPromoController::class, 'create'])->name('promos.create');
            Route::post('/promos', [MexcoSuperAdminPromoController::class, 'store'])->name('promos.store');
            Route::get('/promos/{promo}/edit', [MexcoSuperAdminPromoController::class, 'edit'])->name('promos.edit');
            Route::put('/promos/{promo}', [MexcoSuperAdminPromoController::class, 'update'])->name('promos.update');

            Route::get('/analytics', [MexcoSuperAdminAnalyticsController::class, 'index'])->name('analytics');
            Route::get('/reports', [MexcoSuperAdminReportsController::class, 'index'])->name('reports');
            Route::get('/reports/export', [MexcoSuperAdminReportsController::class, 'export'])->name('reports.export');
            Route::get('/audit', [MexcoSuperAdminAuditController::class, 'index'])->name('audit');
            Route::get('/notifications', [MexcoSuperAdminNotificationController::class, 'index'])->name('notifications');
            Route::post('/notifications/organiser', [MexcoSuperAdminNotificationController::class, 'storeOrganiserNotice'])->name('notifications.organiser.store');
            Route::get('/notifications/logs', [MexcoSuperAdminNotificationController::class, 'logs'])->name('notifications.logs');
            Route::get('/notifications/{template}/edit', [MexcoSuperAdminNotificationController::class, 'edit'])->name('notifications.edit');
            Route::put('/notifications/{template}', [MexcoSuperAdminNotificationController::class, 'update'])->name('notifications.update');
            Route::get('/support', [MexcoSuperAdminSupportController::class, 'index'])->name('support');
            Route::get('/support/create', [MexcoSuperAdminSupportController::class, 'create'])->name('support.create');
            Route::post('/support', [MexcoSuperAdminSupportController::class, 'store'])->name('support.store');
            Route::get('/support/{ticket}', [MexcoSuperAdminSupportController::class, 'show'])->name('support.show');
            Route::post('/support/{ticket}/status', [MexcoSuperAdminSupportController::class, 'updateStatus'])->name('support.status');
            Route::post('/support/{ticket}/reply', [MexcoSuperAdminSupportController::class, 'reply'])->name('support.reply');

            Route::get('/health', [MexcoSuperAdminHealthController::class, 'index'])->name('health');
            Route::get('/admins', [MexcoSuperAdminAdminUserController::class, 'index'])->name('admins');
            Route::get('/admins/create', [MexcoSuperAdminAdminUserController::class, 'create'])->name('admins.create');
            Route::post('/admins', [MexcoSuperAdminAdminUserController::class, 'store'])->name('admins.store');
            Route::get('/admins/{admin}/edit', [MexcoSuperAdminAdminUserController::class, 'edit'])->name('admins.edit');
            Route::put('/admins/{admin}', [MexcoSuperAdminAdminUserController::class, 'update'])->name('admins.update');
            Route::post('/admins/{admin}/suspend', [MexcoSuperAdminAdminUserController::class, 'suspend'])->name('admins.suspend');
            Route::post('/admins/{admin}/activate', [MexcoSuperAdminAdminUserController::class, 'activate'])->name('admins.activate');
            Route::get('/settings', [MexcoSuperAdminSettingsController::class, 'index'])->name('settings');
            Route::put('/settings/{group}', [MexcoSuperAdminSettingsController::class, 'update'])->name('settings.update');
        });
    });

    Route::prefix('api')->group(function () {
        Route::get('/events', [MeetupController::class, 'events']);
        Route::post('/auth/find', [MeetupController::class, 'findAccount']);
        Route::post('/auth/login', [MeetupController::class, 'login']);
        Route::post('/auth/attend', [MeetupController::class, 'attendLogin']);
        Route::get('/auth/session', [MeetupController::class, 'authSession']);
        Route::post('/auth/signout', [MeetupController::class, 'signOut']);
        Route::get('/auth/last-email', [MeetupController::class, 'lastEmail']);
        Route::post('/events/{eventId}/enter', [MeetupController::class, 'enterEvent']);
        Route::post('/events/{eventId}/org', [MeetupController::class, 'orgAccess']);
        Route::post('/events/{eventId}/attend', [MeetupController::class, 'attendEnter']);
        Route::get('/events/{eventId}/config', [MeetupController::class, 'config']);
        Route::match(['get', 'put'], '/events/{eventId}/participant', [MeetupController::class, 'participant']);
        Route::get('/events/{eventId}/participants/{slug}', [MeetupController::class, 'getParticipantBySlug']);
        Route::post('/events/{eventId}/stamp', [MeetupController::class, 'stamp']);
        Route::post('/events/{eventId}/vote', [MeetupController::class, 'vote']);
        Route::post('/events/{eventId}/sessions/{sessionKey}/join', [MeetupController::class, 'joinSession']);
        Route::post('/events/{eventId}/verify/{slug}', [MeetupController::class, 'verify']);
        Route::get('/events/{eventId}/people', [MeetupController::class, 'people']);
        Route::put('/events/{eventId}/settings', [MeetupController::class, 'saveSettings']);
        Route::put('/events/{eventId}/roles', [MeetupController::class, 'saveRoles']);
        Route::put('/events/{eventId}/booths', [MeetupController::class, 'saveBooths']);
        Route::put('/events/{eventId}/sessions', [MeetupController::class, 'saveSessions']);
        Route::put('/events/{eventId}/registry', [MeetupController::class, 'saveRegistry']);
        Route::post('/events/{eventId}/participants', [MeetupController::class, 'upsertParticipant']);
        Route::delete('/events/{eventId}/participants/{slug}', [MeetupController::class, 'deleteParticipant']);
    });
});

// Legacy MeetUP paths → MeXCo
Route::any('/meetup/{path?}', function (?string $path = null) {
    $target = '/mexco'.($path !== null && $path !== '' ? '/'.$path : '');
    $query = request()->getQueryString();

    return redirect($target.($query ? '?'.$query : ''), 301);
})->where('path', '.*');
