<?php

/**
 * ITSWEBER CMS – Admin Routes
 */

/** @var \App\Core\Router $router */

$_slug = admin_url();

// Admin login / logout
$router->get($_slug . '/login', 'App\Http\Controllers\Admin\AuthController@loginForm');
$router->post($_slug . '/login', 'App\Http\Controllers\Admin\AuthController@login');
$router->get($_slug . '/logout', 'App\Http\Controllers\Admin\AuthController@logout');

// Password reset (public – no auth middleware required)
$router->get($_slug . '/forgot-password', 'App\Http\Controllers\Admin\AuthController@forgotPasswordForm');
$router->post($_slug . '/forgot-password', 'App\Http\Controllers\Admin\AuthController@forgotPasswordSend');
$router->get($_slug . '/reset-password', 'App\Http\Controllers\Admin\AuthController@resetPasswordForm');
$router->post($_slug . '/reset-password', 'App\Http\Controllers\Admin\AuthController@resetPasswordSave');

// TOTP 2FA verify (public – pending 2FA session)
$router->get($_slug . '/login/verify-totp', 'App\Http\Controllers\Admin\AuthController@totpVerifyForm');
$router->post($_slug . '/login/verify-totp', 'App\Http\Controllers\Admin\AuthController@totpVerify');

// Admin routes (protected by middleware)
$router->group($_slug, [\App\Http\Middleware\AuthMiddleware::class], function ($router) {
    // Dashboard
    $router->get('', 'App\Http\Controllers\Admin\DashboardController@index');
    $router->get('/dashboard', 'App\Http\Controllers\Admin\DashboardController@index');
    $router->post('/dashboard/quick-draft', 'App\Http\Controllers\Admin\DashboardController@quickDraft');
    $router->post('/dashboard/save-layout', 'App\Http\Controllers\Admin\DashboardController@saveLayout');
    $router->post('/dashboard/toggle-panel', 'App\Http\Controllers\Admin\DashboardController@togglePanel');
    $router->post('/dashboard/create-robots', 'App\Http\Controllers\Admin\DashboardController@createRobots');

    // Pages (static routes before parameterised – ADR-014)
    $router->get('/pages', 'App\Http\Controllers\Admin\PageController@index');
    $router->get('/pages/create', 'App\Http\Controllers\Admin\PageController@create');
    $router->post('/pages/store', 'App\Http\Controllers\Admin\PageController@store');
    $router->get('/pages/edit/{id}', 'App\Http\Controllers\Admin\PageController@edit');
    $router->post('/pages/update/{id}', 'App\Http\Controllers\Admin\PageController@update');
    $router->post('/pages/delete/{id}', 'App\Http\Controllers\Admin\PageController@delete');
    $router->post('/pages/bulk', 'App\Http\Controllers\Admin\PageController@bulk');
    $router->post('/pages/clone/{id}', 'App\Http\Controllers\Admin\PageController@clonePage');

    // Content Revisions (AJAX endpoints – static segments BEFORE parameterised, ADR-014)
    $router->get('/revisions/view/{revId}', 'App\Http\Controllers\Admin\RevisionController@view');
    $router->post('/revisions/restore/{revId}', 'App\Http\Controllers\Admin\RevisionController@restore');
    $router->get('/revisions/{type}/{id}', 'App\Http\Controllers\Admin\RevisionController@list');

    // Preview – draft/published content rendered in frontend theme (auth-protected)
    $router->get('/preview/page/{id}', 'App\Http\Controllers\Admin\PreviewController@previewPage');
    $router->get('/preview/blog/{id}', 'App\Http\Controllers\Admin\PreviewController@previewBlog');

    // Blog (static routes before parameterised – ADR-014)
    $router->get('/blog', 'App\Http\Controllers\Admin\BlogController@index');
    $router->get('/blog/create', 'App\Http\Controllers\Admin\BlogController@create');
    $router->post('/blog/store', 'App\Http\Controllers\Admin\BlogController@store');
    $router->get('/blog/edit/{id}', 'App\Http\Controllers\Admin\BlogController@edit');
    $router->post('/blog/update/{id}', 'App\Http\Controllers\Admin\BlogController@update');
    $router->post('/blog/delete/{id}', 'App\Http\Controllers\Admin\BlogController@delete');
    $router->post('/blog/bulk', 'App\Http\Controllers\Admin\BlogController@bulk');
    $router->post('/blog/clone/{id}', 'App\Http\Controllers\Admin\BlogController@clonePost');

    // Blog categories
    $router->get('/blog/categories', 'App\Http\Controllers\Admin\BlogCategoryController@index');
    $router->post('/blog/categories/store', 'App\Http\Controllers\Admin\BlogCategoryController@store');
    $router->post('/blog/categories/update/{id}', 'App\Http\Controllers\Admin\BlogCategoryController@update');
    $router->post('/blog/categories/delete/{id}', 'App\Http\Controllers\Admin\BlogCategoryController@delete');

    // Blog tags (static routes BEFORE parameterised – ADR-014)
    $router->get('/blog/tags', 'App\Http\Controllers\Admin\BlogTagController@index');
    $router->post('/blog/tags/update/{id}', 'App\Http\Controllers\Admin\BlogTagController@update');
    $router->post('/blog/tags/delete/{id}', 'App\Http\Controllers\Admin\BlogTagController@delete');

    // Gallery – album list / create / edit / delete
    $router->get('/gallery', 'App\Http\Controllers\Admin\GalleryController@index');
    $router->get('/gallery/create', 'App\Http\Controllers\Admin\GalleryController@create');
    $router->post('/gallery/store', 'App\Http\Controllers\Admin\GalleryController@store');
    $router->get('/gallery/edit/{id}', 'App\Http\Controllers\Admin\GalleryController@edit');
    $router->post('/gallery/update/{id}', 'App\Http\Controllers\Admin\GalleryController@update');
    $router->post('/gallery/bulk', 'App\Http\Controllers\Admin\GalleryController@bulk');
    $router->post('/gallery/delete/{id}', 'App\Http\Controllers\Admin\GalleryController@delete');

    // Gallery – image management (static routes BEFORE parameterised – ADR-014)
    $router->post('/gallery/images/reorder', 'App\Http\Controllers\Admin\GalleryController@reorderImages');
    $router->post('/gallery/images/update/{id}', 'App\Http\Controllers\Admin\GalleryController@updateImage');
    $router->post('/gallery/images/delete/{id}', 'App\Http\Controllers\Admin\GalleryController@deleteImage');

    // Gallery – per-album image actions
    $router->get('/gallery/album/{id}/images', 'App\Http\Controllers\Admin\GalleryController@images');
    $router->post('/gallery/album/{id}/upload', 'App\Http\Controllers\Admin\GalleryController@uploadImage');
    $router->post('/gallery/album/{id}/add-from-media', 'App\Http\Controllers\Admin\GalleryController@addFromMedia'); // Phase 2.2
    $router->post('/gallery/album/{id}/set-cover/{imageId}', 'App\Http\Controllers\Admin\GalleryController@setCover');

    // Media – central manager (Phase 4a) – static routes BEFORE parameterised (ADR-014)
    $router->get('/media',                          'App\Http\Controllers\Admin\MediaController@index');
    $router->get('/media/optimize',                 'App\Http\Controllers\Admin\MediaController@optimize');
    $router->post('/media/optimize',                'App\Http\Controllers\Admin\MediaController@runOptimize');
    $router->get('/media/picker-images',            'App\Http\Controllers\Admin\MediaController@pickerImages');
    $router->get('/media/picker-data',              'App\Http\Controllers\Admin\MediaController@pickerImages'); // Phase 2.2: alias for gallery picker
    $router->get('/media/picker-videos',            'App\Http\Controllers\Admin\MediaController@pickerVideos');
    $router->post('/media/upload',                  'App\Http\Controllers\Admin\MediaController@upload');
    $router->post('/media/folders/create',          'App\Http\Controllers\Admin\MediaController@createFolder');
    $router->post('/media/folders/rename/{id}',     'App\Http\Controllers\Admin\MediaController@renameFolder');
    $router->post('/media/folders/delete/{id}',     'App\Http\Controllers\Admin\MediaController@deleteFolder');
    $router->get('/media/edit/{id}',                'App\Http\Controllers\Admin\MediaController@edit');
    $router->post('/media/update/{id}',             'App\Http\Controllers\Admin\MediaController@update');
    $router->post('/media/move/{id}',               'App\Http\Controllers\Admin\MediaController@move');
    $router->post('/media/delete/{id}',             'App\Http\Controllers\Admin\MediaController@delete');
    $router->post('/media/sync',                    'App\Http\Controllers\Admin\MediaController@sync');

    // Menus – overview (list all menus with their items)
    $router->get('/menus', 'App\Http\Controllers\Admin\MenuController@index');
    $router->post('/menus/save', 'App\Http\Controllers\Admin\MenuController@save');

    // Menu items CRUD (specific static routes BEFORE parameterised routes – ADR-014 pattern)
    $router->post('/menus/items/store', 'App\Http\Controllers\Admin\MenuController@storeItem');
    $router->post('/menus/items/reorder', 'App\Http\Controllers\Admin\MenuController@reorder');
    $router->post('/menus/items/update/{id}', 'App\Http\Controllers\Admin\MenuController@updateItem');
    $router->post('/menus/items/delete/{id}', 'App\Http\Controllers\Admin\MenuController@deleteItem');

    // Themes – static routes BEFORE parameterised (ADR-014 pattern)
    $router->get('/themes', 'App\Http\Controllers\Admin\ThemeController@index');
    $router->get('/themes/upload', 'App\Http\Controllers\Admin\ThemeController@uploadForm');    // Phase 10a
    $router->post('/themes/upload', 'App\Http\Controllers\Admin\ThemeController@handleUpload'); // Phase 10a
    $router->post('/themes/activate', 'App\Http\Controllers\Admin\ThemeController@activate');
    $router->post('/themes/uninstall', 'App\Http\Controllers\Admin\ThemeController@uninstall'); // Phase 13a

    // Plugins – static routes BEFORE parameterised (ADR-014 pattern) – Phase 16/17
    $router->get('/plugins', 'App\Http\Controllers\Admin\PluginController@index');
    $router->get('/plugins/upload', 'App\Http\Controllers\Admin\PluginController@uploadForm');
    $router->post('/plugins/upload', 'App\Http\Controllers\Admin\PluginController@handleUpload');
    $router->post('/plugins/uninstall', 'App\Http\Controllers\Admin\PluginController@uninstall');
    $router->post('/plugins/activate', 'App\Http\Controllers\Admin\PluginController@activate');
    $router->post('/plugins/deactivate', 'App\Http\Controllers\Admin\PluginController@deactivate');

    // Marketplace (v3.4.0) – static routes BEFORE parameterised (ADR-014)
    $router->get('/marketplace', 'App\Http\Controllers\Admin\MarketplaceController@index');
    $router->get('/marketplace/licenses', 'App\Http\Controllers\Admin\MarketplaceController@licenses');
    $router->post('/marketplace/licenses', 'App\Http\Controllers\Admin\MarketplaceController@saveLicense');
    $router->post('/marketplace/settings', 'App\Http\Controllers\Admin\MarketplaceController@settings');
    $router->post('/marketplace/install', 'App\Http\Controllers\Admin\MarketplaceController@install');
    $router->post('/marketplace/update', 'App\Http\Controllers\Admin\MarketplaceController@update');
    $router->post('/marketplace/rollback', 'App\Http\Controllers\Admin\MarketplaceController@rollback');
    $router->post('/marketplace/allowlist/add', 'App\Http\Controllers\Admin\MarketplaceController@allowlistAdd');
    $router->post('/marketplace/allowlist/remove', 'App\Http\Controllers\Admin\MarketplaceController@allowlistRemove');
    $router->get('/marketplace/audit-log', 'App\Http\Controllers\Admin\MarketplaceAuditController@index');
    // Parameterised routes last (ADR-014)
    $router->get('/marketplace/backups/{slug}', 'App\Http\Controllers\Admin\MarketplaceController@backups');
    $router->get('/marketplace/preview/{slug}', 'App\Http\Controllers\Admin\MarketplaceController@preview');
    $router->get('/marketplace/detail/{slug}', 'App\Http\Controllers\Admin\MarketplaceController@detail');

    // Users
    $router->get('/users', 'App\Http\Controllers\Admin\UserController@index');
    $router->get('/users/create', 'App\Http\Controllers\Admin\UserController@create');
    $router->post('/users/store', 'App\Http\Controllers\Admin\UserController@store');
    $router->post('/users/bulk', 'App\Http\Controllers\Admin\UserController@bulk');
    $router->get('/users/edit/{id}', 'App\Http\Controllers\Admin\UserController@edit');
    $router->post('/users/update/{id}', 'App\Http\Controllers\Admin\UserController@update');
    $router->post('/users/delete/{id}', 'App\Http\Controllers\Admin\UserController@delete');

    // Two-Factor Authentication (3.3.0 B1)
    $router->get('/2fa', 'App\Http\Controllers\Admin\AuthController@setup2fa');
    $router->post('/2fa/enable', 'App\Http\Controllers\Admin\AuthController@enable2fa');
    $router->post('/2fa/disable', 'App\Http\Controllers\Admin\AuthController@disable2fa');
    $router->post('/2fa/regenerate-codes', 'App\Http\Controllers\Admin\AuthController@regenerateCodes');

    // Settings
    $router->get('/settings', 'App\Http\Controllers\Admin\SettingsController@index');
    $router->post('/settings/save', 'App\Http\Controllers\Admin\SettingsController@save');
    $router->post('/settings/language', 'App\Http\Controllers\Admin\SettingsController@language');

    // Page Cache (3.3.0 B2)
    $router->post('/settings/clear-page-cache', 'App\Http\Controllers\Admin\SettingsController@clearCache');

    // Site Notice (3.2.0) – global announcement bar settings
    $router->get('/settings/site-notice', 'App\Http\Controllers\Admin\SiteNoticeController@index');
    $router->post('/settings/site-notice', 'App\Http\Controllers\Admin\SiteNoticeController@save');

    // Mail Settings & Templates (3.3.0 Phase 4)
    $router->get('/settings/mail', 'App\Http\Controllers\Admin\MailController@index');
    $router->post('/settings/mail/save', 'App\Http\Controllers\Admin\MailController@save');
    $router->post('/settings/mail/test', 'App\Http\Controllers\Admin\MailController@test');
    $router->get('/settings/mail/template/{id}', 'App\Http\Controllers\Admin\MailController@templateEdit');
    $router->post('/settings/mail/template/{id}/save', 'App\Http\Controllers\Admin\MailController@templateSave');

    // Redirects (3.3.0 A2) – static routes BEFORE parameterised (ADR-014)
    $router->get('/redirects', 'App\Http\Controllers\Admin\RedirectController@index');
    $router->get('/redirects/create', 'App\Http\Controllers\Admin\RedirectController@create');
    $router->post('/redirects/store', 'App\Http\Controllers\Admin\RedirectController@store');
    $router->post('/redirects/bulk', 'App\Http\Controllers\Admin\RedirectController@bulk');
    $router->get('/redirects/edit/{id}', 'App\Http\Controllers\Admin\RedirectController@edit');
    $router->post('/redirects/update/{id}', 'App\Http\Controllers\Admin\RedirectController@update');
    $router->post('/redirects/delete/{id}', 'App\Http\Controllers\Admin\RedirectController@delete');

    // Consent / Cookie-Einstellungen (Phase 6d)
    $router->get('/consent', 'App\Http\Controllers\Admin\ConsentController@index');
    $router->post('/consent/save', 'App\Http\Controllers\Admin\ConsentController@save');

    // Contact Form Builder (3.3.0 F5)
    $router->get('/contact-form',                                     'App\Http\Controllers\Admin\ContactFormController@index');
    $router->post('/contact-form/save',                               'App\Http\Controllers\Admin\ContactFormController@save');
    $router->get('/contact-form/export',                              'App\Http\Controllers\Admin\ContactFormController@export');
    $router->get('/contact-form/submission/{id}',                     'App\Http\Controllers\Admin\ContactFormController@viewSubmission');
    $router->post('/contact-form/submission/{id}/delete',             'App\Http\Controllers\Admin\ContactFormController@deleteSubmission');
    $router->post('/contact-form/bulk',                               'App\Http\Controllers\Admin\ContactFormController@bulk');

    // Activity Log (3.3.0 F9)
    $router->get('/activity-log', 'App\Http\Controllers\Admin\ActivityLogController@index');

    // Page Builder (3.2.0) – Homepage sections
    $router->get('/pagebuilder',                          'App\Http\Controllers\Admin\PageBuilderController@index');
    $router->post('/pagebuilder/section/add',             'App\Http\Controllers\Admin\PageBuilderController@addSection');
    $router->post('/pagebuilder/section/import',         'App\Http\Controllers\Admin\PageBuilderController@importSection');
    $router->post('/pagebuilder/section/{id}/save',       'App\Http\Controllers\Admin\PageBuilderController@saveSection');
    $router->post('/pagebuilder/section/{id}/toggle',     'App\Http\Controllers\Admin\PageBuilderController@toggleSection');
    $router->post('/pagebuilder/section/{id}/delete',     'App\Http\Controllers\Admin\PageBuilderController@deleteSection');
    $router->post('/pagebuilder/section/{id}/duplicate',  'App\Http\Controllers\Admin\PageBuilderController@duplicateSection');
    $router->post('/pagebuilder/reorder',                 'App\Http\Controllers\Admin\PageBuilderController@reorder');
    $router->get('/pagebuilder/section/{id}/form',        'App\Http\Controllers\Admin\PageBuilderController@loadForm');
    $router->post('/pagebuilder/section/{id}/preview',   'App\Http\Controllers\Admin\PageBuilderController@previewSection');
    $router->get('/pagebuilder/section/{id}/export',     'App\Http\Controllers\Admin\PageBuilderController@exportSection');
    $router->get('/pagebuilder/section/{id}/revisions',  'App\Http\Controllers\Admin\PageBuilderController@revisions');
    $router->post('/pagebuilder/section/{id}/restore/{revisionId}', 'App\Http\Controllers\Admin\PageBuilderController@restoreRevision');

    // Page Builder for Pages (3.3.0)
    $router->get('/pages/{id}/pagebuilder',              'App\Http\Controllers\Admin\PageBuilderController@index');
    $router->post('/pages/{id}/pagebuilder/section/add', 'App\Http\Controllers\Admin\PageBuilderController@addSection');
    $router->post('/pages/{id}/pagebuilder/section/import', 'App\Http\Controllers\Admin\PageBuilderController@importSection');

    // Page Builder for Blog Posts (3.3.0)
    $router->get('/blog/{id}/pagebuilder',               'App\Http\Controllers\Admin\PageBuilderController@index');
    $router->post('/blog/{id}/pagebuilder/section/add',  'App\Http\Controllers\Admin\PageBuilderController@addSection');
    $router->post('/blog/{id}/pagebuilder/section/import', 'App\Http\Controllers\Admin\PageBuilderController@importSection');

    // Events (Phase 4b) – static routes BEFORE parameterised (ADR-014)
    $router->get('/events', 'App\Http\Controllers\Admin\EventController@index');
    $router->get('/events/create', 'App\Http\Controllers\Admin\EventController@create');
    $router->post('/events/store', 'App\Http\Controllers\Admin\EventController@store');
    $router->post('/events/upload-image', 'App\Http\Controllers\Admin\EventController@uploadImage'); // Phase 4b.1
    $router->get('/events/categories', 'App\Http\Controllers\Admin\EventCategoryController@index');
    $router->post('/events/categories/store', 'App\Http\Controllers\Admin\EventCategoryController@store');
    $router->post('/events/categories/update/{id}', 'App\Http\Controllers\Admin\EventCategoryController@update');
    $router->post('/events/categories/delete/{id}', 'App\Http\Controllers\Admin\EventCategoryController@delete');
    $router->post('/events/bulk', 'App\Http\Controllers\Admin\EventController@bulk');
    $router->get('/events/edit/{id}', 'App\Http\Controllers\Admin\EventController@edit');
    $router->post('/events/update/{id}', 'App\Http\Controllers\Admin\EventController@update');
    $router->post('/events/delete/{id}', 'App\Http\Controllers\Admin\EventController@delete');

    // Downloads (Phase 8) – static routes BEFORE parameterised (ADR-014)
    $router->get('/downloads', 'App\Http\Controllers\Admin\DownloadController@index');
    $router->get('/downloads/create', 'App\Http\Controllers\Admin\DownloadController@create');
    $router->post('/downloads/store', 'App\Http\Controllers\Admin\DownloadController@store');
    $router->post('/downloads/upload-file', 'App\Http\Controllers\Admin\DownloadController@uploadFile');
    $router->post('/downloads/upload-image', 'App\Http\Controllers\Admin\DownloadController@uploadImage');
    $router->get('/downloads/categories', 'App\Http\Controllers\Admin\DownloadCategoryController@index');
    $router->post('/downloads/categories/store', 'App\Http\Controllers\Admin\DownloadCategoryController@store');
    $router->post('/downloads/categories/update/{id}', 'App\Http\Controllers\Admin\DownloadCategoryController@update');
    $router->post('/downloads/categories/delete/{id}', 'App\Http\Controllers\Admin\DownloadCategoryController@delete');
    $router->post('/downloads/bulk', 'App\Http\Controllers\Admin\DownloadController@bulk');
    $router->get('/downloads/edit/{id}', 'App\Http\Controllers\Admin\DownloadController@edit');
    $router->post('/downloads/update/{id}', 'App\Http\Controllers\Admin\DownloadController@update');
    $router->post('/downloads/delete/{id}', 'App\Http\Controllers\Admin\DownloadController@delete');

    // System
    $router->get('/system', 'App\Http\Controllers\Admin\SystemController@index');
    $router->get('/system/health', 'App\Http\Controllers\Admin\SystemController@healthCheck');
    $router->get('/system/error-log', 'App\Http\Controllers\Admin\SystemController@errorLog');
    $router->post('/system/error-log/clear', 'App\Http\Controllers\Admin\SystemController@clearErrorLog');

    // Backup, Restore & Update (Phase 6e)
    $router->get('/backup',                         'App\Http\Controllers\Admin\BackupController@index');
    $router->post('/backup/create',                 'App\Http\Controllers\Admin\BackupController@createBackup');
    $router->post('/backup/restore',                'App\Http\Controllers\Admin\BackupController@restoreUpload');
    $router->post('/backup/restore/execute',        'App\Http\Controllers\Admin\BackupController@restoreExecute');
    $router->post('/backup/update',                 'App\Http\Controllers\Admin\BackupController@updateUpload');
    $router->post('/backup/update/execute',         'App\Http\Controllers\Admin\BackupController@updateExecute');

    // FTP Backup (Phase 3.0.4)
    $router->get('/backup/ftp',                     'App\Http\Controllers\Admin\BackupController@ftpSettings');
    $router->post('/backup/ftp/save',               'App\Http\Controllers\Admin\BackupController@saveFtpSettings');
    $router->post('/backup/ftp/test',               'App\Http\Controllers\Admin\BackupController@testFtpConnection');
    $router->post('/backup/ftp/backup',             'App\Http\Controllers\Admin\BackupController@runFtpBackup');

    // Settings – branding upload (Phase 6e)
    $router->post('/settings/branding/upload',      'App\Http\Controllers\Admin\SettingsController@uploadBranding');

    // Maintenance mode (Phase 19)
    $router->get('/maintenance',         'App\Http\Controllers\Admin\MaintenanceController@index');
    $router->post('/maintenance/enable', 'App\Http\Controllers\Admin\MaintenanceController@enable');
    $router->post('/maintenance/disable','App\Http\Controllers\Admin\MaintenanceController@disable');
    $router->get('/maintenance/preview','App\Http\Controllers\Admin\MaintenanceController@preview');

    // CMS Core Auto-Update (v3.4.0, Sprint 2.6) – static routes before parameterised (ADR-014)
    $router->get('/system/updates',             'App\Http\Controllers\Admin\SystemUpdateController@index');
    $router->post('/system/updates/check',      'App\Http\Controllers\Admin\SystemUpdateController@check');
    $router->post('/system/updates/install',    'App\Http\Controllers\Admin\SystemUpdateController@install');
    $router->get('/system/updates/status',      'App\Http\Controllers\Admin\SystemUpdateController@status');
    $router->post('/system/updates/rollback',   'App\Http\Controllers\Admin\SystemUpdateController@rollback');
});
