<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ClientController;
use App\Http\Controllers\Admin\ClientOnboardingController;
use App\Http\Controllers\Admin\FirstReportWizardController;
use App\Http\Controllers\Admin\OperationsController;
use App\Http\Controllers\Admin\TaskBoardController;
use App\Http\Controllers\Admin\HealthScoreController;
use App\Http\Controllers\Admin\AgencyManagementReportController;
use App\Http\Controllers\Admin\AgencyManagementMailController;
use App\Http\Controllers\Admin\NotificationCenterController;
use App\Http\Controllers\Admin\ClientPortalUserController;
use App\Http\Controllers\Admin\GoogleAdsAccountController;
use App\Http\Controllers\Admin\MetaAdsAccountController;
use App\Http\Controllers\Admin\SearchConsoleController;
use App\Http\Controllers\Admin\Ga4Controller;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\ReportScheduleController;
use App\Http\Controllers\Admin\ReportTemplateController;
use App\Http\Controllers\Admin\BrandKitController;
use App\Http\Controllers\Admin\MailSettingController;
use App\Http\Controllers\Admin\AlertRuleController;
use App\Http\Controllers\Admin\AiSettingsController;
use App\Http\Controllers\Admin\ApiSdkController;
use App\Http\Controllers\Admin\ReportAiInsightController;
use App\Http\Controllers\Admin\SystemController;
use App\Http\Controllers\Admin\SystemHealthController;
use App\Http\Controllers\Admin\SyncController;
use App\Http\Controllers\Admin\DataQualityController;
use App\Http\Controllers\Admin\PerformanceController;
use App\Http\Controllers\Admin\Integrations\PlatformConnectionTestController;
use App\Http\Controllers\Admin\Integrations\GoogleOAuthController;
use App\Http\Controllers\Admin\Integrations\AccountDiscoveryController;
use App\Http\Controllers\Admin\Integrations\DataPreviewController;
use App\Http\Controllers\Portal\PortalAuthController;
use App\Http\Controllers\Portal\PortalDashboardController;

Route::redirect('/', '/admin');

Route::prefix('portal')->name('portal.')->group(function () {
    Route::get('/login', [PortalAuthController::class, 'loginForm'])->name('login');
    Route::post('/login', [PortalAuthController::class, 'login'])->name('login.submit');
    Route::post('/logout', [PortalAuthController::class, 'logout'])->middleware('auth')->name('logout');

    Route::middleware('auth')->group(function () {
        Route::get('/', [PortalDashboardController::class, 'index'])->name('dashboard');
        Route::get('/reports', [PortalDashboardController::class, 'reports'])->name('reports');
        Route::get('/reports/{report}', [PortalDashboardController::class, 'showReport'])->name('reports.show');
        Route::get('/reports/{report}/download/{type}', [PortalDashboardController::class, 'downloadReport'])->name('reports.download');
        Route::get('/alerts', [PortalDashboardController::class, 'alerts'])->name('alerts');
    });
});

Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');






    Route::prefix('notifications')->name('notifications.')->group(function () {
        Route::get('/', [NotificationCenterController::class, 'index'])->name('index');
        Route::get('/dropdown', [NotificationCenterController::class, 'dropdown'])->name('dropdown');
        Route::post('/build', [NotificationCenterController::class, 'build'])->name('build');
        Route::post('/mark-all-read', [NotificationCenterController::class, 'markAllRead'])->name('mark-all-read');
        Route::get('/{notification}', [NotificationCenterController::class, 'show'])->name('show');
        Route::post('/{notification}/read', [NotificationCenterController::class, 'markRead'])->name('read');
        Route::post('/{notification}/dismiss', [NotificationCenterController::class, 'dismiss'])->name('dismiss');
        Route::delete('/{notification}', [NotificationCenterController::class, 'destroy'])->name('destroy');
    });

    Route::prefix('operations')->name('operations.')->group(function () {
        Route::get('/', [OperationsController::class, 'index'])->name('index');
        Route::get('/clients/{client}', [OperationsController::class, 'show'])->name('show');
        Route::post('/clients/{client}/tasks', [OperationsController::class, 'storeTask'])->name('tasks.store');
        Route::patch('/tasks/{task}', [OperationsController::class, 'updateTask'])->name('tasks.update');
    });

    Route::prefix('task-board')->name('task-board.')->group(function () {
        Route::get('/', [TaskBoardController::class, 'index'])->name('index');
        Route::post('/generate', [TaskBoardController::class, 'generate'])->name('generate');
        Route::post('/tasks', [TaskBoardController::class, 'store'])->name('tasks.store');
        Route::patch('/tasks/{task}', [TaskBoardController::class, 'update'])->name('tasks.update');
        Route::post('/tasks/{task}/complete', [TaskBoardController::class, 'complete'])->name('tasks.complete');
        Route::delete('/tasks/{task}', [TaskBoardController::class, 'destroy'])->name('tasks.destroy');
    });



    Route::prefix('management-reports')->name('management-reports.')->group(function () {
        Route::get('/', [AgencyManagementReportController::class, 'index'])->name('index');
        Route::post('/generate', [AgencyManagementReportController::class, 'generate'])->name('generate');
        Route::get('/mail-settings', [AgencyManagementMailController::class, 'settings'])->name('mail-settings');
        Route::post('/mail-settings', [AgencyManagementMailController::class, 'save'])->name('mail-settings.save');
        Route::post('/send-latest', [AgencyManagementMailController::class, 'sendLatest'])->name('send-latest');
        Route::get('/{managementReport}', [AgencyManagementReportController::class, 'show'])->name('show');
        Route::post('/{managementReport}/send', [AgencyManagementMailController::class, 'send'])->name('send');
        Route::post('/{managementReport}/regenerate', [AgencyManagementReportController::class, 'regenerate'])->name('regenerate');
        Route::get('/{managementReport}/download/{type}', [AgencyManagementReportController::class, 'download'])->name('download');
    });

    Route::prefix('health-scores')->name('health-scores.')->group(function () {
        Route::get('/', [HealthScoreController::class, 'index'])->name('index');
        Route::post('/calculate-all', [HealthScoreController::class, 'calculateAll'])->name('calculate-all');
        Route::get('/{client}', [HealthScoreController::class, 'show'])->name('show');
        Route::post('/{client}/calculate', [HealthScoreController::class, 'calculateClient'])->name('calculate-client');
    });

    Route::prefix('first-report')->name('first-report.')->group(function () {
        Route::get('/', [FirstReportWizardController::class, 'index'])->name('index');
        Route::get('/{client}', [FirstReportWizardController::class, 'show'])->name('show');
        Route::post('/{client}/preview', [FirstReportWizardController::class, 'preview'])->name('preview');
        Route::post('/{client}/sync', [FirstReportWizardController::class, 'sync'])->name('sync');
        Route::post('/{client}/quality', [FirstReportWizardController::class, 'quality'])->name('quality');
        Route::post('/{client}/generate', [FirstReportWizardController::class, 'generate'])->name('generate');
        Route::post('/{client}/ai', [FirstReportWizardController::class, 'ai'])->name('ai');
        Route::post('/{client}/send-test', [FirstReportWizardController::class, 'sendTest'])->name('send-test');
        Route::post('/{client}/run-all', [FirstReportWizardController::class, 'runAll'])->name('run-all');
    });

    Route::prefix('onboarding')->name('onboarding.')->group(function () {
        Route::get('/', [ClientOnboardingController::class, 'index'])->name('index');
        Route::get('/start', [ClientOnboardingController::class, 'start'])->name('start');
        Route::post('/client', [ClientOnboardingController::class, 'saveClient'])->name('client.save');
        Route::get('/{client}/integrations', [ClientOnboardingController::class, 'integrations'])->name('integrations');
        Route::post('/{client}/integrations', [ClientOnboardingController::class, 'saveIntegrations'])->name('integrations.save');
        Route::get('/{client}/brand', [ClientOnboardingController::class, 'brand'])->name('brand');
        Route::post('/{client}/brand', [ClientOnboardingController::class, 'saveBrand'])->name('brand.save');
        Route::get('/{client}/report', [ClientOnboardingController::class, 'report'])->name('report');
        Route::post('/{client}/report', [ClientOnboardingController::class, 'saveReport'])->name('report.save');
        Route::get('/{client}/summary', [ClientOnboardingController::class, 'summary'])->name('summary');
        Route::post('/{client}/complete', [ClientOnboardingController::class, 'complete'])->name('complete');
    });

    Route::resource('clients', ClientController::class);
    Route::get('clients/{client}/portal-users', [ClientPortalUserController::class, 'index'])->name('clients.portal-users.index');
    Route::get('clients/{client}/portal-users/create', [ClientPortalUserController::class, 'create'])->name('clients.portal-users.create');
    Route::post('clients/{client}/portal-users', [ClientPortalUserController::class, 'store'])->name('clients.portal-users.store');
    Route::patch('clients/{client}/portal-users/{user}', [ClientPortalUserController::class, 'update'])->name('clients.portal-users.update');
    Route::delete('clients/{client}/portal-users/{user}', [ClientPortalUserController::class, 'destroy'])->name('clients.portal-users.destroy');

    Route::prefix('accounts')->name('accounts.')->group(function () {
        Route::resource('google-ads', GoogleAdsAccountController::class)->parameters(['google-ads' => 'connection']);
        Route::resource('meta-ads', MetaAdsAccountController::class)->parameters(['meta-ads' => 'connection']);
        Route::resource('search-console', SearchConsoleController::class)->parameters(['search-console' => 'connection']);
        Route::resource('ga4', Ga4Controller::class)->parameters(['ga4' => 'connection']);

        Route::post('connections/{connection}/test', PlatformConnectionTestController::class)->name('connections.test');
    });


    Route::prefix('integrations/discovery')->name('integrations.discovery.')->group(function () {
        Route::get('/', [AccountDiscoveryController::class, 'index'])->name('index');
        Route::post('/connections/{connection}/discover', [AccountDiscoveryController::class, 'discover'])->name('discover');
        Route::post('/connections/{connection}/attach', [AccountDiscoveryController::class, 'attach'])->name('attach');
    });

    Route::prefix('integrations/preview')->name('integrations.preview.')->group(function () {
        Route::get('/', [DataPreviewController::class, 'index'])->name('index');
        Route::post('/run', [DataPreviewController::class, 'run'])->name('run');
        Route::post('/connections/{connection}', [DataPreviewController::class, 'runConnection'])->name('connection');
    });

    Route::prefix('integrations/google')->name('integrations.google.')->group(function () {
        Route::get('{connection}/redirect', [GoogleOAuthController::class, 'redirect'])->name('redirect');
        Route::get('callback', [GoogleOAuthController::class, 'callback'])->name('callback');
    });

    Route::prefix('sync')->name('sync.')->group(function () {
        Route::get('/', [SyncController::class, 'index'])->name('index');
        Route::get('/quality', [DataQualityController::class, 'index'])->name('quality');
        Route::post('/quality/connections/{connection}', [DataQualityController::class, 'runConnection'])->name('quality.connection');
        Route::post('/connections/{connection}', [SyncController::class, 'syncConnection'])->name('connection');
        Route::post('/all', [SyncController::class, 'syncAll'])->name('all');
    });

    Route::prefix('performance')->name('performance.')->group(function () {
        Route::get('/', [PerformanceController::class, 'index'])->name('index');
        Route::get('/{platform}', [PerformanceController::class, 'index'])->name('platform');
    });

    Route::post('brand-kits/{brandKit}/duplicate', [BrandKitController::class, 'duplicate'])->name('brand-kits.duplicate');
    Route::resource('brand-kits', BrandKitController::class);

    Route::post('report-templates/seed-defaults', [ReportTemplateController::class, 'seedDefaults'])->name('report-templates.seed-defaults');
    Route::post('report-templates/{reportTemplate}/duplicate', [ReportTemplateController::class, 'duplicate'])->name('report-templates.duplicate');
    Route::get('report-templates/{reportTemplate}/preview', [ReportTemplateController::class, 'preview'])->name('report-templates.preview');
    Route::resource('report-templates', ReportTemplateController::class);

    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/', [ReportController::class, 'index'])->name('index');
        Route::get('/create', [ReportController::class, 'create'])->name('create');
        Route::post('/generate', [ReportController::class, 'generate'])->name('generate');
        Route::get('/archive', [ReportController::class, 'archive'])->name('archive');
        Route::get('/{report}', [ReportController::class, 'show'])->name('show');
        Route::post('/{report}/send', [ReportController::class, 'send'])->name('send');
        Route::post('/{report}/ai-insights', [ReportAiInsightController::class, 'generate'])->name('ai.generate');
        Route::delete('/{report}/ai-insights', [ReportAiInsightController::class, 'clear'])->name('ai.clear');
        Route::post('/{report}/regenerate', [ReportController::class, 'regenerate'])->name('regenerate');
        Route::get('/{report}/download/{type}', [ReportController::class, 'download'])->name('download');
    });

    Route::post('report-schedules/{reportSchedule}/run', [ReportScheduleController::class, 'run'])->name('report-schedules.run');
    Route::resource('report-schedules', ReportScheduleController::class);

    Route::post('alert-rules/run-all', [AlertRuleController::class, 'evaluateAll'])->name('alert-rules.evaluate-all');
    Route::post('alert-rules/{alertRule}/evaluate', [AlertRuleController::class, 'evaluate'])->name('alert-rules.evaluate');
    Route::get('alert-logs', [AlertRuleController::class, 'logs'])->name('alert-logs.index');
    Route::post('alert-logs/{alertLog}/resolve', [AlertRuleController::class, 'resolve'])->name('alert-logs.resolve');
    Route::resource('alert-rules', AlertRuleController::class);

    Route::prefix('ai')->name('ai.')->group(function () {
        Route::get('/settings', [AiSettingsController::class, 'index'])->name('settings');
        Route::post('/settings', [AiSettingsController::class, 'save'])->name('settings.save');
    });

    Route::prefix('mail')->name('mail.')->group(function () {
        Route::get('/settings', [MailSettingController::class, 'settings'])->name('settings');
        Route::post('/settings', [MailSettingController::class, 'save'])->name('save');
        Route::post('/test', [MailSettingController::class, 'test'])->name('test');
        Route::get('/logs', [MailSettingController::class, 'logs'])->name('logs');
    });

    Route::prefix('system')->name('system.')->group(function () {
        Route::get('/health', SystemHealthController::class)->name('health');
        Route::get('/settings', [SystemController::class, 'settings'])->name('settings');
        Route::post('/settings', [SystemController::class, 'saveSettings'])->name('settings.save');
        Route::get('/logs', [SystemController::class, 'logs'])->name('logs');
        Route::get('/queue', [SystemController::class, 'queue'])->name('queue');
        Route::get('/token-vault', [SystemController::class, 'tokenVault'])->name('token-vault');
        Route::get('/api-sdk', [ApiSdkController::class, 'index'])->name('api-sdk');
        Route::post('/api-sdk/connections/{connection}/probe', [ApiSdkController::class, 'probe'])->name('api-sdk.probe');
    });
});
