<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\LandingPageController;
use App\Http\Controllers\SitemapController;

// ============================================================================
// ROOT / HOMEPAGE — Landing Page
// ============================================================================

Route::get('/', [LandingPageController::class, 'index'])->name('landing.home');
Route::get('/home', [LandingPageController::class, 'index'])->name('landing.home.alt');

// API routes for dynamic content (AJAX)
Route::get('/api/organization', [LandingPageController::class, 'getOrganization'])->name('api.organization');
Route::get('/api/sliders', [LandingPageController::class, 'getSliders'])->name('api.sliders');

// ============================================================================
// SITEMAP — publicly accessible, no auth middleware
// Accessible at: APP_URL/sitemap.xml
// Submit this URL to Google Search Console.
// ============================================================================

Route::get('/sitemap.xml', [SitemapController::class, 'index'])->name('sitemap');

// ============================================================================
// ROBOTS.TXT — served dynamically so APP_URL is always correct.
//
// IMPORTANT: Apache/Nginx serve public/robots.txt as a static file BEFORE
// Laravel handles the request (because the .htaccess only passes requests
// to index.php when the file does NOT exist on disk).
//
// Therefore:
//   - On Apache/Nginx (production): public/robots.txt is served directly.
//     Keep public/robots.txt up-to-date with the correct absolute Sitemap URL.
//   - On php artisan serve (local dev): this route is used instead.
//
// Both files are kept in sync — same content, same Sitemap directive.
// ============================================================================

Route::get('/robots.txt', [SitemapController::class, 'robots'])->name('robots');

// ============================================================================
// DASHBOARD ROUTES (role-specific)
// ============================================================================

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/dashboard/super-manager', [DashboardController::class, 'superManager'])->name('dashboard.super-manager');
    Route::get('/dashboard/manager', [DashboardController::class, 'manager'])->name('dashboard.manager');
    Route::get('/dashboard/members', [DashboardController::class, 'members'])->name('dashboard.members');
    Route::get('/dashboard/trainers', [DashboardController::class, 'trainers'])->name('dashboard.trainers');
    Route::get('/dashboard/trainees', [DashboardController::class, 'trainees'])->name('dashboard.trainees');
    Route::get('/dashboard/mentor', [DashboardController::class, 'mentor'])->name('dashboard.mentor');
    Route::get('/dashboard/mentee', [DashboardController::class, 'mentee'])->name('dashboard.mentee');
    Route::get('/dashboard/partners', [DashboardController::class, 'partners'])->name('dashboard.partners');
    Route::get('/dashboard/contributor', [DashboardController::class, 'contributor'])->name('dashboard.contributor');
    Route::get('/dashboard/health-marketor', [DashboardController::class, 'healthMarketor'])->name('dashboard.health-marketor');
});

// ============================================================================
// PROFILE ROUTES
// ============================================================================

use App\Http\Controllers\ProfileController;

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

// ============================================================================
// SETTINGS ROUTES (sessions, security)
// ============================================================================

Route::middleware('auth')->prefix('settings')->name('settings.')->group(function () {
    Route::get('/sessions', [ProfileController::class, 'sessions'])->name('sessions');
    Route::post('/sessions/logout-other', [ProfileController::class, 'logoutOtherDevices'])->name('sessions.logout-other');
    Route::get('/security', [ProfileController::class, 'security'])->name('security');
});

// ============================================================================
// USER MANAGEMENT ROUTES
// ============================================================================

use App\Http\Controllers\UserController;

Route::middleware(['auth'])->prefix('users')->name('users.')->group(function () {
    Route::get('/', [UserController::class, 'index'])->name('index');
    Route::get('/create', [UserController::class, 'create'])->name('create');
    Route::post('/', [UserController::class, 'store'])->name('store');
    Route::get('/{user}', [UserController::class, 'show'])->name('show');
    Route::get('/{user}/edit', [UserController::class, 'edit'])->name('edit');
    Route::put('/{user}', [UserController::class, 'update'])->name('update');
    Route::patch('/{user}/toggle-active', [UserController::class, 'toggleActive'])->name('toggleActive');
    Route::delete('/{user}', [UserController::class, 'destroy'])->name('destroy');
});

// ============================================================================
// ROLE ASSIGNMENT ROUTES (Super Manager only)
// ============================================================================

use App\Http\Controllers\RoleAssignmentController;

Route::middleware(['auth', 'role:Super Manager'])->prefix('roles')->name('roles.')->group(function () {
    Route::get('/', [RoleAssignmentController::class, 'indexRoles'])->name('index');
    Route::get('/assign', [RoleAssignmentController::class, 'index'])->name('assign');
    Route::put('/{role}/update', [RoleAssignmentController::class, 'update'])->name('update');
    Route::post('/{role}/permissions/assign', [RoleAssignmentController::class, 'assignPermission'])->name('permissions.assign');
    Route::delete('/{role}/permissions/remove', [RoleAssignmentController::class, 'removePermission'])->name('permissions.remove');
});

// ============================================================================
// THEME MANAGEMENT ROUTES (Super Manager only)
// ============================================================================

use App\Http\Controllers\Admin\ThemeController;
use App\Http\Controllers\Admin\SliderController;
use App\Http\Controllers\Admin\HeroCardController;

Route::middleware(['auth', 'role:Super Manager'])->prefix('admin/themes')->name('admin.themes.')->group(function () {
    Route::get('/', [ThemeController::class, 'index'])->name('index');
    Route::post('/apply', [ThemeController::class, 'apply'])->name('apply');
    Route::get('/preview/{theme}', [ThemeController::class, 'preview'])->name('preview');
});

// ============================================================================
// HERO SLIDER MANAGEMENT ROUTES (Manager & Super Manager) — Requirement 13.1, 13.3
// ============================================================================

Route::middleware(['auth', 'role:Super Manager|Manager'])->prefix('admin/sliders')->name('admin.sliders.')->group(function () {
    Route::get('/', [SliderController::class, 'index'])->name('index');
    Route::get('/create', [SliderController::class, 'create'])->name('create');
    Route::post('/', [SliderController::class, 'store'])->name('store');
    Route::get('/{slider}/edit', [SliderController::class, 'edit'])->name('edit');
    Route::put('/{slider}', [SliderController::class, 'update'])->name('update');
    Route::delete('/{slider}', [SliderController::class, 'destroy'])->name('destroy');
});

// ============================================================================
// HERO CARDS MANAGEMENT ROUTES (Manager & Super Manager) — Requirement 13.2, 13.3
// ============================================================================

Route::middleware(['auth', 'role:Super Manager|Manager'])->prefix('admin/hero-cards')->name('admin.hero-cards.')->group(function () {
    Route::get('/', [HeroCardController::class, 'index'])->name('index');
    Route::get('/create', [HeroCardController::class, 'create'])->name('create');
    Route::post('/', [HeroCardController::class, 'store'])->name('store');
    Route::get('/{heroCard}/edit', [HeroCardController::class, 'edit'])->name('edit');
    Route::put('/{heroCard}', [HeroCardController::class, 'update'])->name('update');
    Route::delete('/{heroCard}', [HeroCardController::class, 'destroy'])->name('destroy');
});

// ============================================================================
// WHAT WE DO IMAGES MANAGEMENT ROUTES (Super Manager & Manager)
// Requirements: 10.1, 10.2, 10.3
// ============================================================================

use App\Http\Controllers\Admin\WhatWeDoImageController;

Route::middleware(['auth', 'permission:manage what we do images'])->prefix('admin/what-we-do-images')->name('admin.what-we-do-images.')->group(function () {
    Route::get('/', [WhatWeDoImageController::class, 'index'])->name('index');
    Route::post('/{slot}', [WhatWeDoImageController::class, 'update'])->name('update');
});

// ============================================================================
// OTP SETTINGS ROUTES (Super Manager only)
// ============================================================================

use App\Http\Controllers\OtpSettingsController;

Route::middleware(['auth', 'role:Super Manager'])->prefix('admin/otp')->name('otp.')->group(function () {
    Route::get('/settings', [OtpSettingsController::class, 'index'])->name('settings');
    Route::post('/toggle', [OtpSettingsController::class, 'toggle'])->name('toggle');
    Route::get('/status', [OtpSettingsController::class, 'status'])->name('status');
});

// ============================================================================
// SAMPLE / EXAMPLE ROUTES
// ============================================================================

use App\Http\Controllers\SampleFormController;

Route::middleware(['auth'])->prefix('samples')->name('samples.')->group(function () {
    Route::get('/form', [SampleFormController::class, 'create'])->name('form');
    Route::post('/form', [SampleFormController::class, 'store'])->name('form.store');
});

Route::get('/examples/landing', [SampleFormController::class, 'landing'])->name('examples.landing');

// ============================================================================
// PUBLISHING PAGES — Admin CMS (Super Manager & Manager)
// ============================================================================

use App\Http\Controllers\PublishingController;

Route::middleware(['auth'])->prefix('admin/pages')->name('admin.pages.')->group(function () {
    Route::get('/', [PublishingController::class, 'index'])
        ->name('index')
        ->middleware('permission:view pages');

    Route::get('/create', [PublishingController::class, 'create'])
        ->name('create')
        ->middleware('permission:create pages');

    Route::post('/', [PublishingController::class, 'store'])
        ->name('store')
        ->middleware('permission:create pages');

    Route::get('/{page}/edit', [PublishingController::class, 'edit'])
        ->name('edit')
        ->middleware('permission:edit pages');

    Route::put('/{page}', [PublishingController::class, 'update'])
        ->name('update')
        ->middleware('permission:edit pages');

    Route::delete('/{page}', [PublishingController::class, 'destroy'])
        ->name('destroy')
        ->middleware('permission:delete pages');

    Route::post('/{page}/toggle-publish', [PublishingController::class, 'togglePublish'])
        ->name('toggle-publish')
        ->middleware('permission:edit pages');

    Route::get('/{page}/preview', [PublishingController::class, 'preview'])
        ->name('preview')
        ->middleware('permission:view pages');

    Route::post('/block-template', [PublishingController::class, 'getBlockTemplate'])
        ->name('block-template')
        ->middleware('permission:create pages');

    Route::post('/upload-image', [PublishingController::class, 'uploadImage'])
        ->name('upload-image')
        ->middleware('permission:create pages');

    Route::post('/{page}/reorder-blocks', [PublishingController::class, 'reorderBlocks'])
        ->name('reorder-blocks')
        ->middleware('permission:edit pages');
});

// ============================================================================
// PUBLISHING PAGES — Footer legal links (Requirements 8.1, 8.2, 8.3)
// ============================================================================

Route::get('/pages/privacy-policy', [PublishingController::class, 'show'])
    ->defaults('slug', 'privacy-policy')
    ->name('pages.privacy');

Route::get('/pages/terms-of-service', [PublishingController::class, 'show'])
    ->defaults('slug', 'terms-of-service')
    ->name('pages.terms');

Route::get('/pages/support', [PublishingController::class, 'show'])
    ->defaults('slug', 'support')
    ->name('pages.support');

// Wildcard catch-all for any publishing page created via dashboard or seeder
Route::get('/pages/{slug}', [PublishingController::class, 'show'])
    ->name('pages.show');

// ============================================================================
// GOOGLE MEET CONFIGURATION ROUTES (Super Manager only)
// ============================================================================

use App\Http\Controllers\GoogleMeetController;

Route::middleware(['auth', 'role:Super Manager'])
    ->prefix('config/google-meet')
    ->name('config.google-meet.')
    ->group(function () {
        Route::get('/', [GoogleMeetController::class, 'index'])->name('index');
        Route::get('/create', [GoogleMeetController::class, 'create'])->name('create');
        Route::post('/', [GoogleMeetController::class, 'store'])->name('store');
        Route::get('/edit', [GoogleMeetController::class, 'edit'])->name('edit');
        Route::put('/', [GoogleMeetController::class, 'update'])->name('update');
        Route::delete('/', [GoogleMeetController::class, 'destroy'])->name('destroy');
        Route::get('/authenticate', [GoogleMeetController::class, 'authenticate'])->name('authenticate');
        Route::get('/callback', [GoogleMeetController::class, 'callback'])
            ->name('callback')
            ->withoutMiddleware(['role:Super Manager']);
        Route::match(['get', 'post'], '/test', [GoogleMeetController::class, 'testConnection'])->name('test');
        Route::post('/test-meeting', [GoogleMeetController::class, 'testMeeting'])->name('test-meeting');
        Route::get('/logs', [GoogleMeetController::class, 'logs'])->name('logs');
        Route::get('/documentation', [GoogleMeetController::class, 'documentation'])->name('documentation');
        Route::post('/revoke', [GoogleMeetController::class, 'revokeAccess'])->name('revoke');
    });

// ============================================================================
// TOPBAR NOTIFICATIONS (auth + Super Manager|Manager role)
// Requirements: 4.8, 5.1, 5.2, 5.3
// ============================================================================

use App\Http\Controllers\TopbarNotificationController;

Route::middleware(['auth', 'role:Super Manager|Manager'])
    ->prefix('dashboard/topbar-notifications')
    ->name('topbar-notifications.')
    ->group(function () {
        Route::get('/', [TopbarNotificationController::class, 'index'])->name('index');
        Route::get('/create', [TopbarNotificationController::class, 'create'])->name('create');
        Route::post('/', [TopbarNotificationController::class, 'store'])->name('store');
        Route::get('/{topbarNotification}/edit', [TopbarNotificationController::class, 'edit'])->name('edit');
        Route::put('/{topbarNotification}', [TopbarNotificationController::class, 'update'])->name('update');
        Route::delete('/{topbarNotification}', [TopbarNotificationController::class, 'destroy'])->name('destroy');
    });

// ============================================================================
// EMAIL SUBSCRIPTION — Public routes (no auth)
// Requirements: 7.1, 7.5, 7.7, 7.9, 7.10
// ============================================================================

use App\Http\Controllers\EmailSubscriptionController;

// POST /subscribe — rate limited to 5 requests/minute per IP (Req 7.5)
Route::post('/subscribe', [EmailSubscriptionController::class, 'store'])
    ->middleware('throttle:5,1')
    ->name('subscribe');

// GET /subscribe/confirm/{token} — double opt-in confirmation (Req 7.7, 7.8, 7.9)
Route::get('/subscribe/confirm/{token}', [EmailSubscriptionController::class, 'confirm'])
    ->name('subscribe.confirm');

// GET /unsubscribe/{token} — one-click unsubscribe (Req 7.10)
Route::get('/unsubscribe/{token}', [EmailSubscriptionController::class, 'unsubscribe'])
    ->name('unsubscribe');

// ============================================================================
// PUBLIC NOTIFICATION DETAIL PAGE (no auth required)
// Display full content of a topbar notification
// Requirements: Public access to view notification details
// ============================================================================

Route::get('/notifications/{topbarNotification}', [TopbarNotificationController::class, 'show'])
    ->whereNumber('topbarNotification')
    ->name('topbar-notifications.show');

// ============================================================================
// EMAIL SUBSCRIBERS DASHBOARD (auth + Super Manager|Manager role)
// Requirements: 9.1, 9.2, 9.3, 9.7
// ============================================================================

Route::middleware(['auth', 'role:Super Manager|Manager'])
    ->prefix('dashboard/email-subscribers')
    ->name('email-subscribers.')
    ->group(function () {
        Route::get('/', [EmailSubscriptionController::class, 'index'])->name('index');
        Route::get('/compose', [EmailSubscriptionController::class, 'compose'])->name('compose');
        Route::post('/send', [EmailSubscriptionController::class, 'send'])->name('send');
        Route::get('/export', [EmailSubscriptionController::class, 'export'])->name('export');
        Route::get('/{emailSubscriber}', [EmailSubscriptionController::class, 'show'])->name('show');
    });

// ============================================================================
// MODULE ROUTE FILES
// ============================================================================

require __DIR__.'/auth.php';
require __DIR__.'/registration.php';
require __DIR__.'/health-directory.php';
require __DIR__.'/communication.php';
require __DIR__.'/email.php';
require __DIR__.'/sms.php';
require __DIR__.'/payment.php';
require __DIR__.'/organization.php';
require __DIR__.'/membership.php';
require __DIR__.'/mentorship.php';
require __DIR__.'/training.php';
require __DIR__.'/knowledge-sharing.php';
require __DIR__.'/self-paced.php';
require __DIR__.'/modules.php';
require __DIR__.'/media.php';
