<?php

use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\DnsTemplateRecordController;
use App\Http\Controllers\Admin\NameServerController;
use App\Http\Controllers\Admin\PlanController as AdminPlanController;
use App\Http\Controllers\Admin\PowerDNSController;
use App\Http\Controllers\Admin\PowerDNSTemplateController;
use App\Http\Controllers\Admin\ResellerPlanController as AdminResellerPlanController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\EmailTemplateController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\DnsRecordController;
use App\Http\Controllers\DnsZoneController;
use App\Http\Controllers\PowerDNSUserController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\Reseller\ClientController;
use App\Http\Controllers\Reseller\ResellerDashboardController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\DnsHealthCheckController;
use App\Http\Controllers\ActivityController;
use App\Http\Controllers\SessionController;
use App\Http\Controllers\HelpController;
use App\Http\Controllers\LandingController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

// Landing Page - cu date dinamice din controller
Route::get('/', [LandingController::class, 'index'])->name('home');

// Landing Pages publice (pagini dinamice create din admin)
Route::get('/pagina/{slug}', [LandingController::class, 'showPage'])->name('landing.page');

// Dashboard - folosește DashboardController pentru date optimizate
Route::get('/dashboard', [DashboardController::class, 'index'])
    ->middleware(['auth', 'verified', '2fa'])
    ->name('dashboard');

// Public Plans Page
Route::get('/plans', [\App\Http\Controllers\PlanController::class, 'index'])->name('plans.public');

// Two-Factor Challenge și Verify Routes (FĂRĂ middleware 2fa)
Route::middleware(['auth', 'verified'])->prefix('auth/two-factor')->name('auth.two-factor.')->group(function () {
    Route::get('/challenge', function() {
        return view('auth.two-factor-challenge');
    })->name('challenge');
    Route::post('/verify', [\App\Http\Controllers\Auth\TwoFactorController::class, 'verify'])->name('verify');
});

Route::middleware(['auth', 'verified', '2fa'])->group(function () {
    // Two-Factor Authentication Settings Routes (CU middleware 2fa)
    Route::prefix('auth/two-factor')->name('auth.two-factor.')->group(function () {
        Route::get('/', [\App\Http\Controllers\Auth\TwoFactorController::class, 'index'])->name('index');
        Route::post('/enable', [\App\Http\Controllers\Auth\TwoFactorController::class, 'enable'])->name('enable');
        Route::post('/confirm', [\App\Http\Controllers\Auth\TwoFactorController::class, 'confirm'])->name('confirm');
        Route::post('/disable', [\App\Http\Controllers\Auth\TwoFactorController::class, 'disable'])->name('disable');
        Route::post('/regenerate', [\App\Http\Controllers\Auth\TwoFactorController::class, 'regenerateRecoveryCodes'])->name('regenerate');
        Route::delete('/devices/{device}', [\App\Http\Controllers\Auth\TwoFactorController::class, 'revokeTrustedDevice'])->name('revoke-device');
        Route::delete('/devices', [\App\Http\Controllers\Auth\TwoFactorController::class, 'revokeAllTrustedDevices'])->name('revoke-all-devices');
    });

    // Profil utilizator
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    // DNS Health Check - Verificare propagare DNS
    Route::prefix('dns-health-check')->name('dns-health-check.')->group(function () {
        Route::get('/', [DnsHealthCheckController::class, 'index'])->name('index');
        Route::post('/check', [DnsHealthCheckController::class, 'check'])->name('check');
        Route::post('/quick-check', [DnsHealthCheckController::class, 'quickCheck'])->name('quick-check');
    });

    // Activitate utilizator - Jurnal acțiuni
    Route::prefix('activity')->name('activity.')->group(function () {
        Route::get('/', [ActivityController::class, 'index'])->name('index');
        Route::get('/export', [ActivityController::class, 'export'])->name('export');
        Route::post('/cleanup', [ActivityController::class, 'cleanup'])->name('cleanup');
    });

    // Sesiuni active și istoric login
    Route::prefix('sessions')->name('sessions.')->group(function () {
        Route::get('/', [SessionController::class, 'index'])->name('index');
        Route::delete('/{session}', [SessionController::class, 'revoke'])->name('revoke');
        Route::post('/revoke-all', [SessionController::class, 'revokeAll'])->name('revoke-all');
    });

    // Pagină de ajutor și documentație
    Route::prefix('help')->name('help.')->group(function () {
        Route::get('/', [HelpController::class, 'index'])->name('index');
        Route::get('/search', [HelpController::class, 'search'])->name('search');
        Route::get('/{slug}', [HelpController::class, 'show'])->name('show');
    });

    // Notifications Routes
    Route::get('/notifications', [\App\Http\Controllers\NotificationController::class, 'index'])->name('notifications.index');
    Route::post('/notifications/mark-all-read', [\App\Http\Controllers\NotificationController::class, 'markAllAsRead'])->name('notifications.mark-all-read');
    Route::post('/notifications/{id}/mark-read', [\App\Http\Controllers\NotificationController::class, 'markAsRead'])->name('notifications.mark-read');
    Route::delete('/notifications/delete-all', [\App\Http\Controllers\NotificationController::class, 'deleteAll'])->name('notifications.delete-all');
    Route::delete('/notifications/{id}', [\App\Http\Controllers\NotificationController::class, 'destroy'])->name('notifications.destroy');

    // DNS Zones Routes
    Route::get('dns-zones/search', [DnsZoneController::class, 'search'])->name('dns-zones.search');
    Route::get('dns-zones/export-list', [DnsZoneController::class, 'exportList'])->name('dns-zones.export-list');
    Route::post('dns-zones/bulk-action', [DnsZoneController::class, 'bulkAction'])->name('dns-zones.bulk-action');
    Route::resource('dns-zones', DnsZoneController::class)->except(['show']);

    // Rută personalizată pentru show care acceptă ID sau nume domeniu
    Route::get('dns-zones/{identifier}', [DnsZoneController::class, 'show'])
        ->name('dns-zones.show')
        ->where('identifier', '[a-zA-Z0-9\-\.]+');

    // Toggle sincronizare PowerDNS
    Route::patch('dns-zones/{dnsZone}/toggle-sync', [DnsZoneController::class, 'toggleSync'])
        ->name('dns-zones.toggle-sync');

    // Toggle active/inactive zone
    Route::patch('dns-zones/{dnsZone}/toggle-active', [DnsZoneController::class, 'toggleActive'])
        ->name('dns-zones.toggle-active');

    // Toggle DNSSEC
    Route::patch('dns-zones/{dnsZone}/toggle-dnssec', [DnsZoneController::class, 'toggleDnssec'])
        ->name('dns-zones.toggle-dnssec');

    // Get DNSSEC data
    Route::get('dns-zones/{dnsZone}/dnssec-data', [DnsZoneController::class, 'getDnssecData'])
        ->name('dns-zones.dnssec-data');

    // Get DNSSEC data from all nameservers
    Route::get('dns-zones/{dnsZone}/dnssec-data-all', [DnsZoneController::class, 'getAllNameserversDnssecData'])
        ->name('dns-zones.dnssec-data-all');

    // Resync zone to name server
    Route::post('dns-zones/{dnsZone}/resync', [DnsZoneController::class, 'resyncToNameServer'])
        ->name('dns-zones.resync');

    // Transfer zone to another user (Admin only)
    Route::post('dns-zones/{dnsZone}/transfer', [DnsZoneController::class, 'transferToUser'])
        ->name('dns-zones.transfer')
        ->middleware('admin');

    // DNS Statistics Routes
    Route::get('dns-zones/{identifier}/statistics', [DnsZoneController::class, 'statistics'])
        ->name('dns-zones.statistics');
    Route::get('dns-zones/{identifier}/statistics/data', [DnsZoneController::class, 'statisticsData'])
        ->name('dns-zones.statistics.data');

    // DNS Zone Export Route
    Route::get('dns-zones/{zone}/export', [DnsZoneController::class, 'export'])
        ->name('dns-zones.export');

    // DNS Zone Import Route
    Route::post('dns-zones/{dnsZone}/import', [DnsZoneController::class, 'import'])
        ->name('dns-zones.import');
    Route::post('dns-zones/{identifier}/statistics/collect', [DnsZoneController::class, 'collectStatistics'])
        ->name('dns-zones.statistics.collect');
    Route::get('dns-zones/{identifier}/statistics/export', [DnsZoneController::class, 'exportStatistics'])
        ->name('dns-zones.statistics.export');

    // DNS Zone Recheck Nameservers (permite reactivare zone suspendate)
    Route::post('dns-zones/{dnsZone}/recheck-nameservers', [DnsZoneController::class, 'recheckNameservers'])
        ->name('dns-zones.recheck-nameservers');

    // DNS Records Routes (nested under zones) - cu middleware pentru zone suspendate
    Route::prefix('dns-zones/{dnsZone}')->middleware('check.zone.suspended')->group(function () {
        Route::get('records', [DnsRecordController::class, 'index'])->name('dns-records.index');
        Route::get('records/create', [DnsRecordController::class, 'create'])->name('dns-records.create');
        Route::post('records', [DnsRecordController::class, 'store'])->name('dns-records.store');
        // Bulk actions
        Route::post('records/bulk-toggle', [DnsRecordController::class, 'bulkToggle'])->name('dns-records.bulk-toggle');
        Route::post('records/bulk-delete', [DnsRecordController::class, 'bulkDelete'])->name('dns-records.bulk-delete');
        // Rutele specifice trebuie să fie înaintea rutelor generice
        Route::get('records/{record}/changelog', [DnsRecordController::class, 'changelog'])->name('dns-records.changelog')->withoutMiddleware('check.zone.suspended');
        Route::get('records/{record}/edit', [DnsRecordController::class, 'edit'])->name('dns-records.edit');
        Route::post('records/{record}/toggle-status', [DnsRecordController::class, 'toggleStatus'])->name('dns-records.toggle-status');
        Route::post('records/{record}/restore/{changelogId}', [DnsRecordController::class, 'restoreRecord'])->name('dns-records.restore');
        // Rutele generice
        Route::get('records/{record}', [DnsRecordController::class, 'show'])->name('dns-records.show');
        Route::patch('records/{record}', [DnsRecordController::class, 'update'])->name('dns-records.update');
        Route::delete('records/{record}', [DnsRecordController::class, 'destroy'])->name('dns-records.destroy');
    });

    // PowerDNS Records Routes (pentru utilizatori)
    Route::prefix('powerdns')->name('powerdns.')->group(function () {
        Route::get('zones/{zoneName}/records/{recordName}/{recordType}/edit', [PowerDNSUserController::class, 'editRecord'])->name('records.edit');
        Route::patch('zones/{zoneName}/records/{recordName}/{recordType}', [PowerDNSUserController::class, 'updateRecord'])->name('records.update');
        Route::delete('zones/{zoneName}/records/{recordName}', [PowerDNSUserController::class, 'deleteRecord'])->name('records.delete');
        Route::get('zones/{zoneName}/records/create', [PowerDNSUserController::class, 'createRecord'])->name('records.create');
        Route::post('zones/{zoneName}/records', [PowerDNSUserController::class, 'storeRecord'])->name('records.store');
        Route::get('zones/{zoneName}/records/{recordName}/changelog', [PowerDNSUserController::class, 'recordChangelog'])->name('records.changelog');
        Route::post('zones/{zoneName}/records/{recordName}/restore/{changelogId}', [PowerDNSUserController::class, 'restoreRecord'])->name('records.restore');
        Route::post('zones/{zoneName}/records/{recordName}/toggle-status', [PowerDNSUserController::class, 'toggleRecordStatus'])->name('records.toggle-status');
        Route::get('activity', [PowerDNSUserController::class, 'activityLog'])->name('activity');
    });

    // API Tokens Management Routes
    Route::prefix('api-tokens')->name('api-tokens.')->group(function () {
        Route::get('/', [App\Http\Controllers\ApiTokenController::class, 'index'])->name('index');
        Route::post('/', [App\Http\Controllers\ApiTokenController::class, 'store'])->name('store');
        Route::get('/{token}/edit', [App\Http\Controllers\ApiTokenController::class, 'edit'])->name('edit');
        Route::put('/{token}', [App\Http\Controllers\ApiTokenController::class, 'update'])->name('update');
        Route::patch('/{token}/regenerate', [App\Http\Controllers\ApiTokenController::class, 'regenerate'])->name('regenerate');
        Route::patch('/{token}/toggle', [App\Http\Controllers\ApiTokenController::class, 'toggle'])->name('toggle');
        Route::delete('/{token}', [App\Http\Controllers\ApiTokenController::class, 'destroy'])->name('destroy');
        Route::get('/{token}/usage', [App\Http\Controllers\ApiTokenController::class, 'usage'])->name('usage');
    });

    // Dynamic DNS Management Routes
    Route::prefix('dyndns')->name('dyndns.')->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\DynamicDnsController::class, 'index'])->name('index');
        Route::get('/create', [App\Http\Controllers\Admin\DynamicDnsController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\Admin\DynamicDnsController::class, 'store'])->name('store');
        Route::get('/{dynamicDnsClient}', [App\Http\Controllers\Admin\DynamicDnsController::class, 'show'])->name('show');
        Route::get('/{dynamicDnsClient}/edit', [App\Http\Controllers\Admin\DynamicDnsController::class, 'edit'])->name('edit');
        Route::put('/{dynamicDnsClient}', [App\Http\Controllers\Admin\DynamicDnsController::class, 'update'])->name('update');
        Route::post('/{dynamicDnsClient}/toggle', [App\Http\Controllers\Admin\DynamicDnsController::class, 'toggle'])->name('toggle');
        Route::post('/{dynamicDnsClient}/regenerate-token', [App\Http\Controllers\Admin\DynamicDnsController::class, 'regenerateToken'])->name('regenerate-token');
        Route::post('/{dynamicDnsClient}/force-sync', [App\Http\Controllers\Admin\DynamicDnsController::class, 'forceSync'])->name('force-sync');
        Route::delete('/{dynamicDnsClient}', [App\Http\Controllers\Admin\DynamicDnsController::class, 'destroy'])->name('destroy');
        Route::get('/{dynamicDnsClient}/download-script', [App\Http\Controllers\Admin\DynamicDnsController::class, 'downloadScript'])->name('download-script');
        Route::get('/{dynamicDnsClient}/download-windows-powershell', [App\Http\Controllers\Admin\DynamicDnsController::class, 'downloadWindowsScript'])->name('download-windows-powershell');
        Route::get('/{dynamicDnsClient}/download-windows-batch', [App\Http\Controllers\Admin\DynamicDnsController::class, 'downloadWindowsBatch'])->name('download-windows-batch');
    });

    // Cloudflare Integration Routes
    Route::prefix('cloudflare')->name('cloudflare.')->group(function () {
        Route::get('/', [App\Http\Controllers\CloudflareController::class, 'index'])->name('index');
        Route::get('/logs', [App\Http\Controllers\CloudflareController::class, 'syncLogs'])->name('logs');

        // Credentials management
        Route::get('/credentials/create', [App\Http\Controllers\CloudflareController::class, 'createCredential'])->name('credentials.create');
        Route::post('/credentials', [App\Http\Controllers\CloudflareController::class, 'storeCredential'])->name('credentials.store');
        Route::post('/credentials/{credential}/verify', [App\Http\Controllers\CloudflareController::class, 'verifyCredential'])->name('credentials.verify');
        Route::delete('/credentials/{credential}', [App\Http\Controllers\CloudflareController::class, 'destroyCredential'])->name('credentials.destroy');
        Route::get('/credentials/{credential}/zones', [App\Http\Controllers\CloudflareController::class, 'listCloudflareZones'])->name('credentials.zones');

        // Zone linking
        Route::get('/link/{zone}', [App\Http\Controllers\CloudflareController::class, 'showLinkZone'])->name('zones.link');
        Route::post('/link/{zone}', [App\Http\Controllers\CloudflareController::class, 'linkZone'])->name('zones.link.store');
        Route::get('/zones/{cfZone}', [App\Http\Controllers\CloudflareController::class, 'showZone'])->name('zones.show');
        Route::post('/zones/{cfZone}/sync', [App\Http\Controllers\CloudflareController::class, 'syncZone'])->name('zones.sync');
        Route::post('/zones/{cfZone}/purge', [App\Http\Controllers\CloudflareController::class, 'purgeCache'])->name('zones.purge');
        Route::delete('/zones/{cfZone}', [App\Http\Controllers\CloudflareController::class, 'unlinkZone'])->name('zones.unlink');

        // Record proxy toggle
        Route::post('/records/{setting}/toggle-proxy', [App\Http\Controllers\CloudflareController::class, 'toggleRecordProxy'])->name('records.toggle-proxy');

        // API endpoints
        Route::get('/api/zones', [App\Http\Controllers\CloudflareController::class, 'apiGetZones'])->name('api.zones');

        // Import from Cloudflare
        Route::get('/credentials/{credential}/import', [App\Http\Controllers\CloudflareController::class, 'showImportZones'])->name('credentials.import.show');
        Route::post('/credentials/{credential}/import', [App\Http\Controllers\CloudflareController::class, 'importZones'])->name('credentials.import');
    });    // API Documentation (necesită autentificare)
    Route::get('/api-docs', [App\Http\Controllers\ApiDocumentationController::class, 'index'])->name('api-docs');

    // Leave Impersonation - trebuie să fie accesibil din orice zonă (dar autentificat)
    Route::post('admin/leave-impersonation', [AdminUserController::class, 'leaveImpersonation'])->name('admin.users.leave-impersonation');

    // Admin Routes - doar pentru administratori
    Route::prefix('admin')->name('admin.')->middleware('admin')->group(function () {
        Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');
        Route::resource('plans', AdminPlanController::class);
        Route::post('plans/{plan}/toggle-status', [AdminPlanController::class, 'toggleStatus'])->name('plans.toggle-status');
        Route::post('plans/{plan}/toggle-recommended', [AdminPlanController::class, 'toggleRecommended'])->name('plans.toggle-recommended');
        Route::post('plans/{plan}/duplicate', [AdminPlanController::class, 'duplicate'])->name('plans.duplicate');
        Route::resource('reseller-plans', AdminResellerPlanController::class);
        Route::resource('users', AdminUserController::class);
        Route::post('users/{user}/impersonate', [AdminUserController::class, 'impersonate'])->name('users.impersonate');
        Route::post('users/{user}/toggle-status', [AdminUserController::class, 'toggleStatus'])->name('users.toggle-status');
        Route::post('users/{user}/reset-password', [AdminUserController::class, 'resetPassword'])->name('users.reset-password');
        Route::post('users/bulk-action', [AdminUserController::class, 'bulkAction'])->name('users.bulk-action');
        Route::get('users-export', [AdminUserController::class, 'export'])->name('users.export');

        // Settings Routes
        Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
        Route::get('/settings/smtp', [SettingsController::class, 'smtp'])->name('settings.smtp');
        Route::get('/settings/seo', [SettingsController::class, 'seo'])->name('settings.seo');
        Route::get('/settings/email-templates', [SettingsController::class, 'emailTemplates'])->name('settings.email-templates');
        Route::get('/settings/captcha', [SettingsController::class, 'captcha'])->name('settings.captcha');
        Route::put('/settings/captcha', [SettingsController::class, 'updateCaptcha'])->name('settings.captcha.update');
        // Rute noi pentru testare și gestionare CAPTCHA
        Route::post('/settings/captcha/test', [SettingsController::class, 'testCaptcha'])->name('settings.captcha.test');
        Route::post('/settings/captcha/clear-stats', [SettingsController::class, 'clearCaptchaStats'])->name('settings.captcha.clear-stats');
        Route::post('/settings/captcha/unblock-ip', [SettingsController::class, 'unblockCaptchaIp'])->name('settings.captcha.unblock-ip');
        Route::get('/settings/cronjobs', [SettingsController::class, 'cronjobs'])->name('settings.cronjobs');
        Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');
        Route::get('/settings/test-email', [SettingsController::class, 'testEmail'])->name('settings.test-email');
        Route::post('/settings/toggle-maintenance', [SettingsController::class, 'toggleMaintenance'])->name('settings.toggle-maintenance');
        Route::get('/settings/export', [SettingsController::class, 'exportSettings'])->name('settings.export');
        Route::post('/cache/clear', [SettingsController::class, 'clearCache'])->name('cache.clear');

        // CronJobs Routes
        Route::prefix('cronjobs')->name('cronjobs.')->group(function () {
            Route::get('/', [App\Http\Controllers\Admin\CronJobController::class, 'index'])->name('index');
            Route::post('/run', [App\Http\Controllers\Admin\CronJobController::class, 'runScheduler'])->name('run');
            Route::post('/restart-queue', [App\Http\Controllers\Admin\CronJobController::class, 'restartQueue'])->name('restart-queue');
            Route::get('/queue-status', [App\Http\Controllers\Admin\CronJobController::class, 'getQueueStatusAjax'])->name('queue-status');
            Route::get('/failed-jobs', [App\Http\Controllers\Admin\CronJobController::class, 'getFailedJobs'])->name('failed-jobs');
            Route::post('/retry-failed', [App\Http\Controllers\Admin\CronJobController::class, 'retryFailedJobs'])->name('retry-failed');
            Route::get('/logs', [App\Http\Controllers\Admin\CronJobController::class, 'getSchedulerLog'])->name('logs');
            Route::get('/download-script', [App\Http\Controllers\Admin\CronJobController::class, 'downloadSetupScript'])->name('download-script');
            Route::post('/save-settings', [App\Http\Controllers\Admin\CronJobController::class, 'saveSettings'])->name('save-settings');
        });

        // Email Templates Routes
        Route::resource('email-templates', EmailTemplateController::class);
        Route::get('email-templates/{emailTemplate}/preview', [EmailTemplateController::class, 'preview'])->name('email-templates.preview');
        Route::post('email-templates/{emailTemplate}/test', [EmailTemplateController::class, 'test'])->name('email-templates.test');
        Route::post('email-templates/{emailTemplate}/duplicate', [EmailTemplateController::class, 'duplicate'])->name('email-templates.duplicate');
        Route::post('email-templates/generate-defaults', [EmailTemplateController::class, 'generateDefaults'])->name('email-templates.generate-defaults');
        Route::get('email-templates-export', [EmailTemplateController::class, 'export'])->name('email-templates.export');
        Route::post('email-templates-import', [EmailTemplateController::class, 'import'])->name('email-templates.import');

        // PowerDNS Routes
        Route::prefix('powerdns')->name('powerdns.')->group(function () {
            Route::get('/', [PowerDNSController::class, 'index'])->name('index');
            Route::get('/servers', [PowerDNSController::class, 'servers'])->name('servers');
            Route::get('/zones', [PowerDNSController::class, 'zones'])->name('zones');
            Route::get('/zones/{zoneId}', [PowerDNSController::class, 'zoneShow'])->name('zones.show');
            Route::delete('/zones/{zoneId}', [PowerDNSController::class, 'deleteZone'])->name('zones.delete');
            Route::delete('/zones/{zoneId}/records', [PowerDNSController::class, 'deleteRecord'])->name('records.delete');

            // Zone Assignment Routes
            Route::get('/zones/{zoneId}/assign', [PowerDNSController::class, 'assignZone'])->name('zones.assign');
            Route::post('/zones/{zoneId}/assign', [PowerDNSController::class, 'storeAssignment'])->name('zones.store-assignment');
            Route::delete('/zones/{zoneId}/assignments/{assignmentId}', [PowerDNSController::class, 'removeAssignment'])->name('zones.remove-assignment');
        });

        // PowerDNS Templates Routes
        Route::resource('powerdns-templates', PowerDNSTemplateController::class);
        Route::post('powerdns-templates/{template}/duplicate', [PowerDNSTemplateController::class, 'duplicate'])->name('powerdns-templates.duplicate');

        // PowerDNS Template Records Routes
        Route::prefix('powerdns-templates/{template}')->name('powerdns-templates.')->group(function () {
            Route::get('/records/create', [DnsTemplateRecordController::class, 'create'])->name('records.create');
            Route::post('/records', [DnsTemplateRecordController::class, 'store'])->name('records.store');
            Route::get('/records/{record}/edit', [DnsTemplateRecordController::class, 'edit'])->name('records.edit');
            Route::put('/records/{record}', [DnsTemplateRecordController::class, 'update'])->name('records.update');
            Route::delete('/records/{record}', [DnsTemplateRecordController::class, 'destroy'])->name('records.destroy');
        });

        // Name Server Routes
        Route::resource('nameservers', NameServerController::class)->parameters(['nameservers' => 'nameServer']);
        Route::post('nameservers/{nameServer}/test', [NameServerController::class, 'testConnection'])->name('nameservers.test');
        Route::post('nameservers/{nameServer}/check-health', [NameServerController::class, 'checkHealth'])->name('nameservers.check-health');
        Route::post('nameservers/{nameServer}/sync-zones', [NameServerController::class, 'syncZones'])->name('nameservers.sync-zones');
        Route::post('nameservers/{nameServer}/toggle-active', [NameServerController::class, 'toggleActive'])->name('nameservers.toggle-active');
        Route::get('nameservers/{nameServer}/search-zones', [NameServerController::class, 'searchZones'])->name('nameservers.search-zones');
        Route::get('nameservers/docs/query-logs-setup', function () {
            return view('admin.nameservers.query-logs-setup');
        })->name('nameservers.query-logs-setup');

        // Backup Routes
        Route::prefix('backups')->name('backups.')->group(function () {
            Route::get('/', [App\Http\Controllers\Admin\BackupController::class, 'index'])->name('index');
            Route::post('/', [App\Http\Controllers\Admin\BackupController::class, 'store'])->name('store');

            // Rute specifice (trebuie ÎNAINTE de rutele cu parametri!)
            Route::post('/bulk-delete', [App\Http\Controllers\Admin\BackupController::class, 'bulkDelete'])->name('bulk-delete');

            // Jurnal Activitate (ÎNAINTE de {backup})
            Route::get('/activity-log', [App\Http\Controllers\Admin\BackupController::class, 'activityLog'])->name('activity-log');

            // Programări Backup (ÎNAINTE de {backup})
            Route::get('/schedules', [App\Http\Controllers\Admin\BackupController::class, 'schedules'])->name('schedules');
            Route::post('/schedules', [App\Http\Controllers\Admin\BackupController::class, 'storeSchedule'])->name('schedules.store');
            Route::put('/schedules/{schedule}', [App\Http\Controllers\Admin\BackupController::class, 'updateSchedule'])->name('schedules.update');
            Route::post('/schedules/{schedule}/toggle', [App\Http\Controllers\Admin\BackupController::class, 'toggleSchedule'])->name('schedules.toggle');
            Route::delete('/schedules/{schedule}', [App\Http\Controllers\Admin\BackupController::class, 'destroySchedule'])->name('schedules.destroy');

            // Backup Settings & Destinations (ÎNAINTE de {backup})
            Route::get('/settings', [App\Http\Controllers\Admin\BackupDestinationController::class, 'index'])->name('settings');
            Route::post('/settings', [App\Http\Controllers\Admin\BackupDestinationController::class, 'updateSettings'])->name('settings.update');
            Route::post('/destinations', [App\Http\Controllers\Admin\BackupDestinationController::class, 'store'])->name('destinations.store');
            Route::put('/destinations/{destination}', [App\Http\Controllers\Admin\BackupDestinationController::class, 'update'])->name('destinations.update');
            Route::delete('/destinations/{destination}', [App\Http\Controllers\Admin\BackupDestinationController::class, 'destroy'])->name('destinations.destroy');
            Route::post('/destinations/{destination}/test', [App\Http\Controllers\Admin\BackupDestinationController::class, 'test'])->name('destinations.test');

            // Rute cu parametri (trebuie ULTIMELE!)
            Route::get('/{backup}', [App\Http\Controllers\Admin\BackupController::class, 'show'])->name('show');
            Route::get('/{backup}/download', [App\Http\Controllers\Admin\BackupController::class, 'download'])->name('download');
            Route::post('/{backup}/restore', [App\Http\Controllers\Admin\BackupController::class, 'restore'])->name('restore');
            Route::delete('/{backup}', [App\Http\Controllers\Admin\BackupController::class, 'destroy'])->name('destroy');
        });

        // Plugin Management Routes
        Route::prefix('plugins')->name('plugins.')->group(function () {
            Route::get('/', [App\Http\Controllers\Admin\PluginController::class, 'index'])->name('index');
            Route::post('/upload', [App\Http\Controllers\Admin\PluginController::class, 'upload'])->name('upload');
            Route::post('/{plugin}/toggle', [App\Http\Controllers\Admin\PluginController::class, 'toggle'])->name('toggle');
            Route::post('/{plugin}/activate', [App\Http\Controllers\Admin\PluginController::class, 'activate'])->name('activate');
            Route::post('/{plugin}/deactivate', [App\Http\Controllers\Admin\PluginController::class, 'deactivate'])->name('deactivate');
            Route::delete('/{plugin}', [App\Http\Controllers\Admin\PluginController::class, 'destroy'])->name('destroy');
        });

        // Landing Pages Management
        Route::prefix('landing-pages')->name('landing-pages.')->group(function () {
            Route::get('/', [App\Http\Controllers\Admin\LandingPageController::class, 'index'])->name('index');
            Route::get('/create', [App\Http\Controllers\Admin\LandingPageController::class, 'create'])->name('create');
            Route::post('/', [App\Http\Controllers\Admin\LandingPageController::class, 'store'])->name('store');
            Route::get('/{landingPage}/edit', [App\Http\Controllers\Admin\LandingPageController::class, 'edit'])->name('edit');
            Route::put('/{landingPage}', [App\Http\Controllers\Admin\LandingPageController::class, 'update'])->name('update');
            Route::delete('/{landingPage}', [App\Http\Controllers\Admin\LandingPageController::class, 'destroy'])->name('destroy');
            Route::post('/{landingPage}/toggle-publish', [App\Http\Controllers\Admin\LandingPageController::class, 'togglePublish'])->name('toggle-publish');
            Route::post('/{landingPage}/duplicate', [App\Http\Controllers\Admin\LandingPageController::class, 'duplicate'])->name('duplicate');
            Route::post('/generate-slug', [App\Http\Controllers\Admin\LandingPageController::class, 'generateSlug'])->name('generate-slug');
        });

        // Landing Menus Management
        Route::prefix('landing-menus')->name('landing-menus.')->group(function () {
            Route::get('/', [App\Http\Controllers\Admin\LandingMenuController::class, 'index'])->name('index');
            Route::get('/create', [App\Http\Controllers\Admin\LandingMenuController::class, 'create'])->name('create');
            Route::post('/', [App\Http\Controllers\Admin\LandingMenuController::class, 'store'])->name('store');
            Route::get('/{landingMenu}/edit', [App\Http\Controllers\Admin\LandingMenuController::class, 'edit'])->name('edit');
            Route::put('/{landingMenu}', [App\Http\Controllers\Admin\LandingMenuController::class, 'update'])->name('update');
            Route::delete('/{landingMenu}', [App\Http\Controllers\Admin\LandingMenuController::class, 'destroy'])->name('destroy');
            Route::post('/{landingMenu}/toggle-active', [App\Http\Controllers\Admin\LandingMenuController::class, 'toggleActive'])->name('toggle-active');
            Route::post('/{landingMenu}/move-up', [App\Http\Controllers\Admin\LandingMenuController::class, 'moveUp'])->name('move-up');
            Route::post('/{landingMenu}/move-down', [App\Http\Controllers\Admin\LandingMenuController::class, 'moveDown'])->name('move-down');
            Route::post('/reorder', [App\Http\Controllers\Admin\LandingMenuController::class, 'reorder'])->name('reorder');
        });
    });

    // Security & Two-Factor Routes (Admin)
    Route::prefix('admin/security')->name('admin.security.')->middleware(['auth', 'verified', 'admin'])->group(function () {
        Route::get('/two-factor', [\App\Http\Controllers\Admin\TwoFactorAdminController::class, 'index'])->name('two-factor.index');
        Route::post('/two-factor/settings', [\App\Http\Controllers\Admin\TwoFactorAdminController::class, 'updateSettings'])->name('two-factor.update-settings');
        Route::post('/two-factor/users/{user}/disable', [\App\Http\Controllers\Admin\TwoFactorAdminController::class, 'disableForUser'])->name('two-factor.disable-user');
        Route::get('/two-factor/export', [\App\Http\Controllers\Admin\TwoFactorAdminController::class, 'export'])->name('two-factor.export');
        Route::post('/two-factor/bulk-disable', [\App\Http\Controllers\Admin\TwoFactorAdminController::class, 'bulkDisable'])->name('two-factor.bulk-disable');
        Route::post('/two-factor/send-reminder', [\App\Http\Controllers\Admin\TwoFactorAdminController::class, 'sendReminder'])->name('two-factor.send-reminder');
        Route::post('/two-factor/send-bulk-reminder', [\App\Http\Controllers\Admin\TwoFactorAdminController::class, 'sendBulkReminder'])->name('two-factor.send-bulk-reminder');
    });

    // Language Management Routes (outside admin group but with consistent naming)
    Route::prefix('admin/settings/languages')->name('admin.settings.languages.')->middleware(['auth', 'verified', 'admin'])->group(function () {
        Route::get('/', [App\Http\Controllers\Admin\LanguageController::class, 'index'])->name('index');
        Route::get('/create', [App\Http\Controllers\Admin\LanguageController::class, 'create'])->name('create');
        Route::post('/', [App\Http\Controllers\Admin\LanguageController::class, 'store'])->name('store');
        Route::get('/{language}/edit', [App\Http\Controllers\Admin\LanguageController::class, 'edit'])->name('edit');
        Route::put('/{language}', [App\Http\Controllers\Admin\LanguageController::class, 'update'])->name('update');
        Route::delete('/{language}', [App\Http\Controllers\Admin\LanguageController::class, 'destroy'])->name('destroy');
        Route::patch('/{language}/set-default', [App\Http\Controllers\Admin\LanguageController::class, 'setDefault'])->name('set-default');
        Route::patch('/{language}/toggle-active', [App\Http\Controllers\Admin\LanguageController::class, 'toggleActive'])->name('toggle-active');
        Route::get('/{language}/translations', [App\Http\Controllers\Admin\LanguageController::class, 'translations'])->name('translations');
        Route::put('/{language}/translations', [App\Http\Controllers\Admin\LanguageController::class, 'updateTranslations'])->name('update-translations');
        Route::post('/{language}/import', [App\Http\Controllers\Admin\LanguageController::class, 'importTranslations'])->name('import');
        Route::get('/{language}/export', [App\Http\Controllers\Admin\LanguageController::class, 'exportTranslations'])->name('export');
    });

    // Reseller Routes - doar pentru reselleri
    Route::prefix('reseller')->name('reseller.')->middleware('reseller')->group(function () {
        Route::get('/', [ResellerDashboardController::class, 'index'])->name('dashboard');
        Route::resource('clients', ClientController::class);
    });

    // PowerDNS User Routes - pentru utilizatorii cu zone atribuite
    Route::prefix('powerdns')->name('powerdns.user.')->group(function () {
        Route::get('/', [PowerDNSUserController::class, 'index'])->name('index');
        Route::get('/{zoneId}', [PowerDNSUserController::class, 'show'])->name('show');
        Route::get('/{zoneId}/records', [PowerDNSUserController::class, 'records'])->name('records');
        Route::delete('/{zoneId}/records', [PowerDNSUserController::class, 'deleteRecord'])->name('delete-record');
    });
});

require __DIR__.'/auth.php';

// Stripe Webhook Route - MUST be outside auth middleware
Route::post('/stripe/webhook', [App\Http\Controllers\StripeWebhookController::class, 'handleWebhook'])->name('cashier.webhook');

// Stripe Subscription Routes
Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/subscriptions', [App\Http\Controllers\SubscriptionController::class, 'index'])->name('subscription.index');
    Route::get('/subscriptions/checkout/{plan}', [App\Http\Controllers\SubscriptionController::class, 'checkout'])->name('subscription.checkout');
    Route::get('/subscriptions/success', [App\Http\Controllers\SubscriptionController::class, 'success'])->name('subscription.success');
    Route::get('/subscriptions/cancel', [App\Http\Controllers\SubscriptionController::class, 'cancel'])->name('subscription.cancel');
    Route::get('/subscriptions/billing-portal', [App\Http\Controllers\SubscriptionController::class, 'billingPortal'])->name('subscription.billing-portal');
    Route::post('/subscriptions/cancel-subscription', [App\Http\Controllers\SubscriptionController::class, 'cancelSubscription'])->name('subscription.cancel-subscription');
    Route::post('/subscriptions/resume', [App\Http\Controllers\SubscriptionController::class, 'resumeSubscription'])->name('subscription.resume');
    Route::post('/subscriptions/swap/{plan}', [App\Http\Controllers\SubscriptionController::class, 'swap'])->name('subscription.swap');
});
