<?php

use App\Http\Controllers\AdminController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\ClientController;
use App\Http\Controllers\DesignerController;
use App\Http\Controllers\GtsAdminController;
use App\Http\Controllers\GtsClientController;
use App\Http\Controllers\PublicController;
use App\Http\Controllers\SettingsController;
use Illuminate\Support\Facades\Route;

Route::middleware('maintenance')->group(function () {
    Route::get('/', [PublicController::class, 'home'])->name('home');
    Route::get('/about-us', [PublicController::class, 'about'])->name('about');
    Route::get('/services', [PublicController::class, 'services'])->name('services');
    Route::get('/order-service/{service?}', [PublicController::class, 'order'])->name('service.order');
    Route::post('/order-service', [PublicController::class, 'storeOrder'])->name('service.order.store');
    Route::get('/portfolio', [PublicController::class, 'portfolio'])->name('portfolio');
    Route::get('/pricing-plans', [PublicController::class, 'pricing'])->name('pricing');
    Route::get('/gts', [PublicController::class, 'gts'])->name('gts');
    Route::get('/contact-us', [PublicController::class, 'contact'])->name('contact');
    Route::post('/contact-us', [PublicController::class, 'sendContact'])->name('contact.send');
});

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.store');
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [AuthController::class, 'register'])->name('register.store');
    Route::post('/demo-login/{role}', [AuthController::class, 'demoLogin'])->name('login.demo');
});

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

Route::middleware(['auth', 'role:admin'])->prefix('gts-admin')->name('gts-admin.')->group(function () {
    Route::get('/dashboard', [GtsAdminController::class, 'dashboard'])->middleware('permission:view_reports')->name('dashboard');
    Route::get('/api-manager', [GtsAdminController::class, 'apiManager'])->middleware(['permission:manage_plans', 'module:gts_api_manager_enabled,1'])->name('api-manager');
    Route::post('/gts-providers/{provider}/sync', [GtsAdminController::class, 'syncProvider'])->middleware('permission:manage_plans')->name('gts-providers.sync');
    Route::post('/gts-providers/{provider}/import-services', [GtsAdminController::class, 'importProviderServices'])->middleware('permission:manage_plans')->name('gts-providers.import-services');
    Route::post('/gts-orders/{order}/sync', [GtsAdminController::class, 'syncOrder'])->middleware('permission:manage_tasks')->name('gts-orders.sync');

    foreach (['gts-clients', 'gts-providers', 'gts-services', 'gts-orders', 'gts-wallets', 'gts-wallet-transactions'] as $module) {
        $permission = match ($module) {
            'gts-clients' => 'manage_clients',
            'gts-providers', 'gts-services' => 'manage_plans',
            'gts-wallets', 'gts-wallet-transactions' => 'manage_payments',
            default => 'manage_tasks',
        };
        Route::get("/{$module}", [GtsAdminController::class, 'index'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.index");
        Route::get("/{$module}/create", [GtsAdminController::class, 'create'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.create");
        Route::post("/{$module}", [GtsAdminController::class, 'store'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.store");
        Route::get("/{$module}/{id}/edit", [GtsAdminController::class, 'edit'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.edit");
        Route::put("/{$module}/{id}", [GtsAdminController::class, 'update'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.update");
        Route::delete("/{$module}/{id}", [GtsAdminController::class, 'destroy'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.destroy");
    }
});

Route::middleware(['auth', 'role:admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/portal', [AdminController::class, 'portalChooser'])->middleware('permission:view_dashboard')->name('portal');
    Route::get('/dashboard', [AdminController::class, 'dashboard'])->middleware('permission:view_dashboard')->name('dashboard');
    Route::get('/plus-tracker', [AdminController::class, 'plusTracker'])->middleware('permission:manage_tasks')->name('tracker');
    Route::get('/reports', [AdminController::class, 'reports'])->middleware('permission:view_reports')->name('reports');
    Route::get('/notifications', [AdminController::class, 'notifications'])->middleware('permission:view_dashboard')->name('notifications');
    Route::post('/notifications/read', [AdminController::class, 'readNotifications'])->middleware('permission:view_dashboard')->name('notifications.read');
    Route::get('/clients/{client}/profile', [AdminController::class, 'clientProfile'])->middleware('permission:manage_clients')->name('clients.profile');
    Route::get('/designers/{designer}/profile', [AdminController::class, 'designerProfile'])->middleware('permission:manage_designers')->name('designers.profile');
    Route::middleware('permission:edit_settings')->group(function () {
        Route::get('/settings', [SettingsController::class, 'index'])->name('settings');
        Route::get('/settings/theme', [SettingsController::class, 'show'])->defaults('section', 'brand')->name('settings.theme');
        Route::get('/settings/fonts', [SettingsController::class, 'show'])->defaults('section', 'brand')->name('settings.fonts');
        Route::get('/settings/{section}', [SettingsController::class, 'show'])->name('settings.section');
        Route::post('/settings/profile', [SettingsController::class, 'updateProfile'])->name('settings.profile.save');
        Route::post('/settings/website', [SettingsController::class, 'updateWebsite'])->name('settings.website.save');
        Route::post('/settings/brand', [SettingsController::class, 'updateBrand'])->name('settings.brand.save');
        Route::post('/settings/payment-method', [SettingsController::class, 'savePaymentMethod'])->name('settings.payment-method.save');
        Route::delete('/settings/payment-method/{method}', [SettingsController::class, 'deletePaymentMethod'])->name('settings.payment-method.destroy');
        Route::post('/settings/roles', [SettingsController::class, 'updateRoles'])->name('settings.roles.save');
        Route::post('/settings/notifications', [SettingsController::class, 'updateNotifications'])->name('settings.notifications.save');
        Route::post('/settings/security', [SettingsController::class, 'updateSecurity'])->name('settings.security.save');
        Route::post('/settings/security/logout-sessions', [SettingsController::class, 'logoutSessions'])->name('settings.security.logout-sessions');
        Route::post('/settings/system', [SettingsController::class, 'updateSystem'])->name('settings.system.save');
        Route::post('/settings/modules', [SettingsController::class, 'updateModules'])->name('settings.modules.save');
        Route::post('/settings/backup', [SettingsController::class, 'backupOptions'])->name('settings.backup.save');
        Route::get('/settings/backup/export/{type}', [SettingsController::class, 'export'])->name('settings.export');
        Route::get('/settings/backup/download/{export}', [SettingsController::class, 'download'])->name('settings.export.download');
    });

    foreach (['clients', 'designers', 'client-subscriptions', 'services', 'plans', 'tasks', 'requests', 'payments', 'payment-methods', 'portfolio', 'about-sections', 'team-members'] as $module) {
        $permission = match ($module) {
            'clients', 'client-subscriptions' => 'manage_clients',
            'designers' => 'manage_designers',
            'plans', 'services' => 'manage_plans',
            'payments', 'payment-methods' => 'manage_payments',
            'portfolio', 'about-sections', 'team-members' => 'manage_portfolio',
            default => 'manage_tasks',
        };
        Route::get("/{$module}", [AdminController::class, 'index'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.index");
        Route::get("/{$module}/create", [AdminController::class, 'create'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.create");
        Route::post("/{$module}", [AdminController::class, 'store'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.store");
        Route::get("/{$module}/{id}/edit", [AdminController::class, 'edit'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.edit");
        Route::put("/{$module}/{id}", [AdminController::class, 'update'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.update");
        Route::delete("/{$module}/{id}", [AdminController::class, 'destroy'])->middleware("permission:{$permission}")->defaults('module', $module)->name("{$module}.destroy");
    }
});

Route::middleware(['auth', 'role:gts_client', 'maintenance', 'module:gts_portal_enabled,1'])->prefix('gts-client')->name('gts-client.')->group(function () {
    Route::get('/dashboard', [GtsClientController::class, 'dashboard'])->middleware('permission:view_dashboard')->name('dashboard');
    Route::get('/wallet', [GtsClientController::class, 'wallet'])->middleware('permission:view_dashboard')->name('wallet');
    Route::post('/wallet/topup', [GtsClientController::class, 'requestTopup'])->middleware(['permission:view_dashboard', 'module:gts_deposit_enabled,1'])->name('wallet.topup');
    Route::get('/services', [GtsClientController::class, 'services'])->middleware('permission:view_dashboard')->name('services');
    Route::post('/orders', [GtsClientController::class, 'storeOrder'])->middleware(['permission:submit_requests', 'module:gts_order_create_enabled,1'])->name('orders.store');
    Route::get('/orders', [GtsClientController::class, 'orders'])->middleware('permission:view_dashboard')->name('orders');
    Route::get('/orders/{order}', [GtsClientController::class, 'showOrder'])->middleware('permission:view_dashboard')->name('orders.show');
    Route::get('/settings', [GtsClientController::class, 'settings'])->middleware('permission:view_dashboard')->name('settings');
    Route::post('/settings', [GtsClientController::class, 'updateSettings'])->middleware('permission:view_dashboard')->name('settings.update');
});

Route::middleware(['auth', 'role:client', 'maintenance', 'module:client_portal_enabled,1'])->prefix('client')->name('client.')->group(function () {
    Route::get('/dashboard', [ClientController::class, 'dashboard'])->middleware('permission:view_dashboard')->name('dashboard');
    Route::get('/my-plan', [ClientController::class, 'plan'])->middleware('permission:view_dashboard')->name('plan');
    Route::post('/my-plan/select', [ClientController::class, 'selectPlan'])->middleware('permission:view_dashboard')->name('plan.select');
    Route::get('/requests', [ClientController::class, 'requests'])->middleware('permission:submit_requests')->name('requests');
    Route::post('/requests', [ClientController::class, 'storeRequest'])->middleware('permission:submit_requests')->name('requests.store');
    Route::get('/work', [ClientController::class, 'work'])->middleware('permission:view_dashboard')->name('work');
    Route::get('/downloads', [ClientController::class, 'downloads'])->middleware('permission:download_files')->name('downloads');
    Route::get('/payments', [ClientController::class, 'payments'])->middleware('permission:view_dashboard')->name('payments');
    Route::post('/payments', [ClientController::class, 'storePayment'])->middleware('permission:upload_files')->name('payments.store');
    Route::get('/messages', [ClientController::class, 'messages'])->middleware('permission:view_dashboard')->name('messages');
    Route::post('/messages', [ClientController::class, 'storeMessage'])->middleware('permission:view_dashboard')->name('messages.store');
    Route::get('/profile', [ClientController::class, 'profile'])->middleware('permission:view_dashboard')->name('profile');
    Route::post('/profile', [ClientController::class, 'updateProfile'])->middleware('permission:view_dashboard')->name('profile.update');
    Route::get('/change-password', [ClientController::class, 'profile'])->middleware('permission:view_dashboard')->name('password');
});

Route::middleware(['auth', 'role:designer'])->prefix('designer')->name('designer.')->group(function () {
    Route::get('/dashboard', [DesignerController::class, 'dashboard'])->middleware('permission:view_dashboard')->name('dashboard');
    Route::get('/tasks', [DesignerController::class, 'tasks'])->middleware('permission:manage_tasks')->name('tasks');
    Route::get('/tasks/{task}', [DesignerController::class, 'showTask'])->middleware('permission:manage_tasks')->name('tasks.show');
    Route::post('/tasks/{task}/submit', [DesignerController::class, 'submitWork'])->middleware('permission:upload_files')->name('tasks.submit');
    Route::get('/upload-work', [DesignerController::class, 'tasks'])->middleware('permission:upload_files')->name('upload');
    Route::get('/submitted-work', [DesignerController::class, 'submittedWork'])->middleware('permission:manage_tasks')->name('submitted');
    Route::get('/revision-tasks', [DesignerController::class, 'revisionTasks'])->middleware('permission:manage_tasks')->name('revisions');
    Route::get('/completed-tasks', [DesignerController::class, 'completedTasks'])->middleware('permission:manage_tasks')->name('completed');
    Route::get('/messages', [DesignerController::class, 'messages'])->middleware('permission:view_dashboard')->name('messages');
    Route::post('/messages', [DesignerController::class, 'storeMessage'])->middleware('permission:view_dashboard')->name('messages.store');
    Route::get('/profile', [DesignerController::class, 'profile'])->middleware('permission:view_dashboard')->name('profile');
    Route::post('/profile', [DesignerController::class, 'updateProfile'])->middleware('permission:view_dashboard')->name('profile.update');
    Route::get('/change-password', [DesignerController::class, 'profile'])->middleware('permission:view_dashboard')->name('password');
});
