<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\BlogController;
use App\Http\Controllers\Admin\BlogCategoryController;
use App\Http\Controllers\Admin\BlogToolsController;
use App\Http\Controllers\Admin\AdSenseController;
use App\Http\Controllers\Admin\SeoController;
use App\Http\Controllers\Admin\BlogCommentController as AdminBlogCommentController;
use App\Http\Controllers\Admin\LibraryCommentController as AdminLibraryCommentController;
use App\Http\Controllers\Admin\ForumCategoryController;
use App\Http\Controllers\Admin\ForumTopicController;
use App\Http\Controllers\Admin\ForumPostController;
use App\Http\Controllers\Admin\LibraryCategoryController;
use App\Http\Controllers\Admin\LibraryContentController;
use App\Http\Controllers\Admin\InterviewCategoryController;
use App\Http\Controllers\Admin\InterviewQuestionController;
use App\Http\Controllers\Admin\InterviewFlashcardController;
use App\Http\Controllers\Admin\ChatRoomController as AdminChatRoomController;
use App\Http\Controllers\Admin\ChatCategoryController as AdminChatCategoryController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\ModuleController;
use App\Http\Controllers\Admin\HomepageWidgetController;
use App\Http\Controllers\BlogController as PublicBlogController;
use App\Http\Controllers\BlogCommentController;
use App\Http\Controllers\LibraryCommentController;
use App\Http\Controllers\ForumController;
use App\Http\Controllers\ForumNotificationController;
use App\Http\Controllers\LibraryController;
use App\Http\Controllers\InterviewController;
use App\Http\Controllers\CalculatorController;
use App\Http\Controllers\ChatController;
use App\Http\Controllers\AIController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\StatisticsController;
use App\Http\Controllers\SitemapController;
use App\Http\Controllers\RobotsController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\PagesController;
use App\Http\Controllers\Admin\PageController;
use App\Http\Controllers\Admin\MenuController;
use App\Http\Controllers\Admin\SidebarController;
use App\Http\Controllers\Admin\HeaderFooterController;
use App\Http\Controllers\Admin\ThemeController;
use App\Http\Controllers\Admin\HomepageLayoutController;
use App\Http\Controllers\Admin\HomepageThemeController;
use App\Http\Controllers\Admin\AISettingController;
use App\Http\Controllers\Admin\SimulationQuestionController;
use Illuminate\Support\Facades\Route;

Route::get('/', [\App\Http\Controllers\WelcomeController::class, 'index'])->name('home');

// ── Static Pages ───────────────────────────────────────────────────────
Route::prefix('sayfa')->name('pages.')->group(function () {
    Route::get('/hakkimizda', [PagesController::class, 'about'])->name('about');
    Route::get('/gizlilik-politikasi', [PagesController::class, 'privacy'])->name('privacy');
    Route::get('/sss', [PagesController::class, 'faq'])->name('faq');
});
// Contact route alias (used in views)
Route::get('/iletisim', function () { return redirect('/'); })->name('contact');

// ── Global Search ──────────────────────────────────────────────────────
Route::get('/search', [SearchController::class, 'index'])->name('search');

// ── SEO Routes ────────────────────────────────────────────────────────
Route::get('/sitemap.xml', [SitemapController::class, 'index'])->name('sitemap');
Route::prefix('sitemap')->name('sitemap.')->group(function () {
    Route::get('/pages.xml',    [SitemapController::class, 'pages'])->name('pages');
    Route::get('/blog.xml',     [SitemapController::class, 'blog'])->name('blog');
    Route::get('/forum.xml',    [SitemapController::class, 'forum'])->name('forum');
    Route::get('/library.xml',  [SitemapController::class, 'library'])->name('library');
    Route::get('/interview.xml',[SitemapController::class, 'interview'])->name('interview');
});
Route::get('/robots.txt', [RobotsController::class, 'index'])->name('robots');

Route::get('/dashboard', [DashboardController::class, 'index'])->middleware(['auth', 'verified'])->name('dashboard');

// ── Public User Profile (accessible without login) ─────────────────────
Route::get('/kullanici/{userId}', [ProfileController::class, 'show'])->name('profile.show');

Route::middleware('auth')->group(function () {
    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');
});

// ── Admin Routes ───────────────────────────────────────────────────────
Route::prefix('admin')
    ->name('admin.')
    ->middleware(['auth', 'admin'])
    ->group(function () {
        Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');
        Route::get('/istatistikler', [StatisticsController::class, 'adminDashboard'])->name('statistics');
        Route::get('/api/chart-data', [StatisticsController::class, 'chartData'])->name('chart_data');

        // Blog
        Route::prefix('blog')->name('blog.')->group(function () {
            Route::get('/', [BlogController::class, 'index'])->name('index');
            Route::get('/create', [BlogController::class, 'create'])->name('create');
            Route::post('/', [BlogController::class, 'store'])->name('store');
            Route::get('/{blog}/edit', [BlogController::class, 'edit'])->name('edit');
            Route::put('/{blog}', [BlogController::class, 'update'])->name('update');
            Route::delete('/{blog}', [BlogController::class, 'destroy'])->name('destroy');
            // AJAX endpoints
            Route::post('/check-duplicate', [BlogController::class, 'checkDuplicate'])->name('check_duplicate');
            Route::post('/publish-checklist', [BlogController::class, 'publishChecklist'])->name('publish_checklist');
            Route::post('/ai-generate', [BlogController::class, 'aiGenerate'])->name('ai_generate');
        });

        // Blog Tools (AJAX)
        Route::prefix('tools')->name('tools.')->group(function () {
            Route::post('/pdf-extract',  [BlogToolsController::class, 'extractPdf'])->name('pdf_extract');
            Route::post('/seo-score',    [BlogToolsController::class, 'calculateSeoScore'])->name('seo_score');
            Route::post('/img-overlay',  [BlogToolsController::class, 'addImageOverlay'])->name('img_overlay');
            Route::post('/img-preview',  [BlogToolsController::class, 'previewOverlay'])->name('img_preview');
        });

        // SEO Settings Management
        Route::prefix('seo')->name('seo.')->group(function () {
            Route::get('/', [SeoController::class, 'index'])->name('index');
            Route::get('/{seo}/edit', [SeoController::class, 'edit'])->name('edit');
            Route::put('/{seo}', [SeoController::class, 'update'])->name('update');
            Route::post('/bulk-update', [SeoController::class, 'bulkUpdate'])->name('bulk_update');
            Route::post('/seed', [SeoController::class, 'seed'])->name('seed');
        });

        // AdSense Management
        Route::prefix('adsense')->name('adsense.')->group(function () {
            Route::get('/', [AdSenseController::class, 'index'])->name('index');
            Route::get('/create', [AdSenseController::class, 'create'])->name('create');
            Route::post('/', [AdSenseController::class, 'store'])->name('store');
            Route::get('/{adsense}/edit', [AdSenseController::class, 'edit'])->name('edit');
            Route::put('/{adsense}', [AdSenseController::class, 'update'])->name('update');
            Route::delete('/{adsense}', [AdSenseController::class, 'destroy'])->name('destroy');
            Route::post('/{adsense}/toggle', [AdSenseController::class, 'toggleActive'])->name('toggle');
        });

        // Blog Category Management
        Route::prefix('blog-categories')->name('blog-categories.')->group(function () {
            Route::get('/', [BlogCategoryController::class, 'index'])->name('index');
            Route::get('/create', [BlogCategoryController::class, 'create'])->name('create');
            Route::post('/', [BlogCategoryController::class, 'store'])->name('store');
            Route::get('/{category}/edit', [BlogCategoryController::class, 'edit'])->name('edit');
            Route::put('/{category}', [BlogCategoryController::class, 'update'])->name('update');
            Route::delete('/{category}', [BlogCategoryController::class, 'destroy'])->name('destroy');
        });

        // Blog Comments Management
        Route::prefix('blog-comments')->name('blog-comments.')->group(function () {
            Route::get('/', [AdminBlogCommentController::class, 'index'])->name('index');
            Route::get('/{comment}', [AdminBlogCommentController::class, 'show'])->name('show');
            Route::get('/{comment}/edit', [AdminBlogCommentController::class, 'edit'])->name('edit');
            Route::put('/{comment}', [AdminBlogCommentController::class, 'update'])->name('update');
            Route::delete('/{comment}', [AdminBlogCommentController::class, 'destroy'])->name('destroy');
            Route::post('/{comment}/approve', [AdminBlogCommentController::class, 'approve'])->name('approve');
            Route::post('/{comment}/reject', [AdminBlogCommentController::class, 'reject'])->name('reject');
            Route::post('/{comment}/reply', [AdminBlogCommentController::class, 'reply'])->name('reply');
            Route::post('/bulk-approve', [AdminBlogCommentController::class, 'bulkApprove'])->name('bulk_approve');
            Route::post('/bulk-delete', [AdminBlogCommentController::class, 'bulkDelete'])->name('bulk_delete');
        });

        // Forum Category Management
        Route::prefix('forum-categories')->name('forum-categories.')->group(function () {
            Route::get('/', [ForumCategoryController::class, 'index'])->name('index');
            Route::get('/create', [ForumCategoryController::class, 'create'])->name('create');
            Route::post('/', [ForumCategoryController::class, 'store'])->name('store');
            Route::get('/{forumCategory}/edit', [ForumCategoryController::class, 'edit'])->name('edit');
            Route::put('/{forumCategory}', [ForumCategoryController::class, 'update'])->name('update');
            Route::delete('/{forumCategory}', [ForumCategoryController::class, 'destroy'])->name('destroy');
        });

        // Forum Topic Management
        Route::prefix('forum-topics')->name('forum-topics.')->group(function () {
            Route::get('/', [ForumTopicController::class, 'index'])->name('index');
            Route::get('/{forumTopic}/edit', [ForumTopicController::class, 'edit'])->name('edit');
            Route::put('/{forumTopic}', [ForumTopicController::class, 'update'])->name('update');
            Route::delete('/{forumTopic}', [ForumTopicController::class, 'destroy'])->name('destroy');
            Route::post('/{forumTopic}/toggle-sticky', [ForumTopicController::class, 'toggleSticky'])->name('toggle_sticky');
            Route::post('/{forumTopic}/toggle-locked', [ForumTopicController::class, 'toggleLocked'])->name('toggle_locked');
        });

        // Forum Post Management
        Route::prefix('forum-posts')->name('forum-posts.')->group(function () {
            Route::get('/', [ForumPostController::class, 'index'])->name('index');
            Route::get('/{forumPost}/edit', [ForumPostController::class, 'edit'])->name('edit');
            Route::put('/{forumPost}', [ForumPostController::class, 'update'])->name('update');
            Route::delete('/{forumPost}', [ForumPostController::class, 'destroy'])->name('destroy');
        });

        // Library Category Management
        Route::prefix('library-categories')->name('library-categories.')->group(function () {
            Route::get('/', [LibraryCategoryController::class, 'index'])->name('index');
            Route::get('/create', [LibraryCategoryController::class, 'create'])->name('create');
            Route::post('/', [LibraryCategoryController::class, 'store'])->name('store');
            Route::get('/{libraryCategory}/edit', [LibraryCategoryController::class, 'edit'])->name('edit');
            Route::put('/{libraryCategory}', [LibraryCategoryController::class, 'update'])->name('update');
            Route::delete('/{libraryCategory}', [LibraryCategoryController::class, 'destroy'])->name('destroy');
        });

        // Library Comments Management
        Route::prefix('library-comments')->name('library-comments.')->group(function () {
            Route::get('/', [AdminLibraryCommentController::class, 'index'])->name('index');
            Route::get('/{comment}', [AdminLibraryCommentController::class, 'show'])->name('show');
            Route::delete('/{comment}', [AdminLibraryCommentController::class, 'destroy'])->name('destroy');
            Route::post('/{comment}/approve', [AdminLibraryCommentController::class, 'approve'])->name('approve');
            Route::post('/{comment}/reject', [AdminLibraryCommentController::class, 'reject'])->name('reject');
            Route::post('/bulk-approve', [AdminLibraryCommentController::class, 'bulkApprove'])->name('bulk_approve');
            Route::post('/bulk-delete', [AdminLibraryCommentController::class, 'bulkDelete'])->name('bulk_delete');
        });

        // Library Content Management
        Route::prefix('library-contents')->name('library-contents.')->group(function () {
            Route::get('/', [LibraryContentController::class, 'index'])->name('index');
            Route::get('/create', [LibraryContentController::class, 'create'])->name('create');
            Route::post('/', [LibraryContentController::class, 'store'])->name('store');
            Route::get('/{libraryContent}/edit', [LibraryContentController::class, 'edit'])->name('edit');
            Route::put('/{libraryContent}', [LibraryContentController::class, 'update'])->name('update');
            Route::delete('/{libraryContent}', [LibraryContentController::class, 'destroy'])->name('destroy');
        });

        // Interview Category Management
        Route::prefix('interview-categories')->name('interview-categories.')->group(function () {
            Route::get('/', [InterviewCategoryController::class, 'index'])->name('index');
            Route::get('/create', [InterviewCategoryController::class, 'create'])->name('create');
            Route::post('/', [InterviewCategoryController::class, 'store'])->name('store');
            Route::get('/{category}/edit', [InterviewCategoryController::class, 'edit'])->name('edit');
            Route::put('/{category}', [InterviewCategoryController::class, 'update'])->name('update');
            Route::delete('/{category}', [InterviewCategoryController::class, 'destroy'])->name('destroy');
        });

        // Interview Question Management
        Route::prefix('interview-questions')->name('interview-questions.')->group(function () {
            Route::get('/', [InterviewQuestionController::class, 'index'])->name('index');
            Route::get('/create', [InterviewQuestionController::class, 'create'])->name('create');
            Route::post('/', [InterviewQuestionController::class, 'store'])->name('store');
            Route::get('/{question}/edit', [InterviewQuestionController::class, 'edit'])->name('edit');
            Route::put('/{question}', [InterviewQuestionController::class, 'update'])->name('update');
            Route::delete('/{question}', [InterviewQuestionController::class, 'destroy'])->name('destroy');
        });

        // Interview Flashcard Management
        Route::prefix('interview-flashcards')->name('interview-flashcards.')->group(function () {
            Route::get('/', [InterviewFlashcardController::class, 'index'])->name('index');
            Route::get('/create', [InterviewFlashcardController::class, 'create'])->name('create');
            Route::post('/', [InterviewFlashcardController::class, 'store'])->name('store');
            Route::get('/{flashcard}/edit', [InterviewFlashcardController::class, 'edit'])->name('edit');
            Route::put('/{flashcard}', [InterviewFlashcardController::class, 'update'])->name('update');
            Route::delete('/{flashcard}', [InterviewFlashcardController::class, 'destroy'])->name('destroy');
        });

        // Chat Category Management
        Route::prefix('chat-categories')->name('chat-categories.')->group(function () {
            Route::get('/', [AdminChatCategoryController::class, 'index'])->name('index');
            Route::get('/create', [AdminChatCategoryController::class, 'create'])->name('create');
            Route::post('/', [AdminChatCategoryController::class, 'store'])->name('store');
            Route::get('/{category}/edit', [AdminChatCategoryController::class, 'edit'])->name('edit');
            Route::put('/{category}', [AdminChatCategoryController::class, 'update'])->name('update');
            Route::delete('/{category}', [AdminChatCategoryController::class, 'destroy'])->name('destroy');
            Route::post('/{category}/toggle-active', [AdminChatCategoryController::class, 'toggleActive'])->name('toggle_active');
        });

        // Chat Room Management
        Route::prefix('chat-rooms')->name('chat-rooms.')->group(function () {
            Route::get('/', [AdminChatRoomController::class, 'index'])->name('index');
            Route::get('/create', [AdminChatRoomController::class, 'create'])->name('create');
            Route::post('/', [AdminChatRoomController::class, 'store'])->name('store');
            Route::get('/{room}/edit', [AdminChatRoomController::class, 'edit'])->name('edit');
            Route::put('/{room}', [AdminChatRoomController::class, 'update'])->name('update');
            Route::get('/{room}', [AdminChatRoomController::class, 'show'])->name('show');
            Route::post('/{room}/toggle-active', [AdminChatRoomController::class, 'toggleActive'])->name('toggle_active');
            Route::delete('/{room}/clear-messages', [AdminChatRoomController::class, 'clearMessages'])->name('clear_messages');
            Route::delete('/{room}', [AdminChatRoomController::class, 'destroy'])->name('destroy');
            Route::post('/{id}/restore', [AdminChatRoomController::class, 'restore'])->name('restore');
            Route::delete('/{room}/messages/{message}', [AdminChatRoomController::class, 'destroyMessage'])->name('destroy_message');
            Route::post('/{room}/bulk-delete-messages', [AdminChatRoomController::class, 'bulkDeleteMessages'])->name('bulk_delete_messages');
            Route::post('/{room}/cleanup-old-messages', [AdminChatRoomController::class, 'cleanupOldMessages'])->name('cleanup_old_messages');
        });

        // User Management
        Route::prefix('users')->name('users.')->group(function () {
            Route::get('/', [UserController::class, 'index'])->name('index');
            Route::get('/{user}', [UserController::class, 'show'])->name('show');
            Route::get('/{user}/edit', [UserController::class, 'edit'])->name('edit');
            Route::put('/{user}', [UserController::class, 'update'])->name('update');
            Route::delete('/{user}', [UserController::class, 'destroy'])->name('destroy');
            Route::post('/{user}/ban', [UserController::class, 'ban'])->name('ban');
            Route::post('/{user}/unban', [UserController::class, 'unban'])->name('unban');
            Route::post('/{user}/change-role', [UserController::class, 'changeRole'])->name('change_role');
            Route::post('/bulk-action', [UserController::class, 'bulkAction'])->name('bulk_action');
        });

        // Settings Management
        Route::prefix('settings')->name('settings.')->group(function () {
            Route::get('/', [SettingsController::class, 'index'])->name('index');
            Route::put('/', [SettingsController::class, 'update'])->name('update');
            Route::get('/clear-cache', [SettingsController::class, 'clearCache'])->name('clear_cache');
        });

        // Homepage Widgets Management
        Route::prefix('homepage')->name('homepage.')->group(function () {
            Route::get('/', [HomepageWidgetController::class, 'index'])->name('index');
            Route::get('/create', [HomepageWidgetController::class, 'create'])->name('create');
            Route::post('/', [HomepageWidgetController::class, 'store'])->name('store');
            Route::get('/{homepage_widget}/edit', [HomepageWidgetController::class, 'edit'])->name('edit');
            Route::put('/{homepage_widget}', [HomepageWidgetController::class, 'update'])->name('update');
            Route::delete('/{homepage_widget}', [HomepageWidgetController::class, 'destroy'])->name('destroy');
            Route::post('/{homepage_widget}/toggle', [HomepageWidgetController::class, 'toggle'])->name('toggle');
            Route::post('/reorder', [HomepageWidgetController::class, 'reorder'])->name('reorder');
        });

        // Modules Management
        Route::prefix('modules')->name('modules.')->group(function () {
            Route::get('/', [ModuleController::class, 'index'])->name('index');
            Route::put('/{module}', [ModuleController::class, 'update'])->name('update');
            Route::post('/{module}/toggle-active', [ModuleController::class, 'toggleActive'])->name('toggle_active');
            Route::post('/{module}/toggle-maintenance', [ModuleController::class, 'toggleMaintenance'])->name('toggle_maintenance');
            Route::post('/{module}/sort', [ModuleController::class, 'updateSortOrder'])->name('sort');
        });

        // Pages Management
        Route::prefix('pages')->name('pages.')->group(function () {
            Route::get('/', [PageController::class, 'index'])->name('index');
            Route::get('/create', [PageController::class, 'create'])->name('create');
            Route::post('/', [PageController::class, 'store'])->name('store');
            Route::get('/{page}/edit', [PageController::class, 'edit'])->name('edit');
            Route::put('/{page}', [PageController::class, 'update'])->name('update');
            Route::delete('/{page}', [PageController::class, 'destroy'])->name('destroy');
        });

        // Menu Management
        Route::prefix('menus')->name('menus.')->group(function () {
            Route::get('/', [MenuController::class, 'index'])->name('index');
            Route::get('/create', [MenuController::class, 'create'])->name('create');
            Route::post('/', [MenuController::class, 'store'])->name('store');
            Route::get('/{menu}/edit', [MenuController::class, 'edit'])->name('edit');
            Route::put('/{menu}', [MenuController::class, 'update'])->name('update');
            Route::delete('/{menu}', [MenuController::class, 'destroy'])->name('destroy');
            // Menu Items
            Route::post('/{menu}/items', [MenuController::class, 'storeItem'])->name('items.store');
            Route::put('/{menu}/items/{item}', [MenuController::class, 'updateItem'])->name('items.update');
            Route::delete('/{menu}/items/{item}', [MenuController::class, 'destroyItem'])->name('items.destroy');
            Route::post('/{menu}/items/reorder', [MenuController::class, 'reorderItems'])->name('items.reorder');
        });

        // Sidebar Settings
        Route::prefix('sidebars')->name('sidebars.')->group(function () {
            Route::get('/', [SidebarController::class, 'index'])->name('index');
            Route::get('/{sidebar}/edit', [SidebarController::class, 'edit'])->name('edit');
            Route::put('/{sidebar}', [SidebarController::class, 'update'])->name('update');
            Route::post('/{sidebar}/reset', [SidebarController::class, 'reset'])->name('reset');
        });

        // Header/Footer Settings
        Route::prefix('header-footer')->name('header-footer.')->group(function () {
            Route::get('/', [HeaderFooterController::class, 'index'])->name('index');
            Route::get('/{headerFooterSetting}/edit', [HeaderFooterController::class, 'edit'])->name('edit');
            Route::put('/{headerFooterSetting}', [HeaderFooterController::class, 'update'])->name('update');
            Route::post('/{headerFooterSetting}/reset', [HeaderFooterController::class, 'reset'])->name('reset');
        });

        // Theme Management
        Route::prefix('themes')->name('themes.')->group(function () {
            Route::get('/', [ThemeController::class, 'index'])->name('index');
            Route::get('/{theme}/edit', [ThemeController::class, 'edit'])->name('edit');
            Route::put('/{theme}', [ThemeController::class, 'update'])->name('update');
            Route::post('/{theme}/activate', [ThemeController::class, 'activate'])->name('activate');
            Route::get('/{theme}/preview', [ThemeController::class, 'preview'])->name('preview');
        });

        Route::prefix('homepage-layouts')->name('homepage-layouts.')->group(function () {
            Route::get('/', [HomepageLayoutController::class, 'index'])->name('index');
            Route::get('/{homepageLayout}/edit', [HomepageLayoutController::class, 'edit'])->name('edit');
            Route::put('/{homepageLayout}', [HomepageLayoutController::class, 'update'])->name('update');
            Route::post('/{homepageLayout}/activate', [HomepageLayoutController::class, 'activate'])->name('activate');
            Route::get('/{homepageLayout}/preview', [HomepageLayoutController::class, 'preview'])->name('preview');
        });

        // Homepage Themes Management
        Route::prefix('homepage-themes')->name('homepage-themes.')->group(function () {
            Route::get('/', [HomepageThemeController::class, 'index'])->name('index');
            Route::get('/create', [HomepageThemeController::class, 'create'])->name('create');
            Route::post('/', [HomepageThemeController::class, 'store'])->name('store');
            Route::get('/{homepageTheme}/edit', [HomepageThemeController::class, 'edit'])->name('edit');
            Route::put('/{homepageTheme}', [HomepageThemeController::class, 'update'])->name('update');
            Route::post('/{homepageTheme}/activate', [HomepageThemeController::class, 'activate'])->name('activate');
            Route::delete('/{homepageTheme}', [HomepageThemeController::class, 'destroy'])->name('destroy');
            Route::get('/{homepageTheme}/preview', [HomepageThemeController::class, 'preview'])->name('preview');
        });

        // AI Settings Management
        Route::prefix('ai-settings')->name('ai-settings.')->group(function () {
            Route::get('/', [AISettingController::class, 'index'])->name('index');
            Route::get('/create', [AISettingController::class, 'create'])->name('create');
            Route::post('/', [AISettingController::class, 'store'])->name('store');
            Route::get('/{aiSetting}/edit', [AISettingController::class, 'edit'])->name('edit');
            Route::put('/{aiSetting}', [AISettingController::class, 'update'])->name('update');
            Route::delete('/{aiSetting}', [AISettingController::class, 'destroy'])->name('destroy');
            Route::post('/{aiSetting}/toggle', [AISettingController::class, 'toggleActive'])->name('toggle');
            Route::post('/test-connection', [AISettingController::class, 'testConnection'])->name('test');
            Route::get('/provider-defaults', [AISettingController::class, 'getProviderDefaults'])->name('provider_defaults');
        });

        // Simulation Questions Management
        Route::prefix('simulation-questions')->name('simulation-questions.')->group(function () {
            Route::get('/', [SimulationQuestionController::class, 'index'])->name('index');
            Route::get('/create', [SimulationQuestionController::class, 'create'])->name('create');
            Route::post('/', [SimulationQuestionController::class, 'store'])->name('store');
            Route::get('/{simulationQuestion}/edit', [SimulationQuestionController::class, 'edit'])->name('edit');
            Route::put('/{simulationQuestion}', [SimulationQuestionController::class, 'update'])->name('update');
            Route::delete('/{simulationQuestion}', [SimulationQuestionController::class, 'destroy'])->name('destroy');
        });
    });

// ── Public Forum Routes ────────────────────────────────────────────────
Route::prefix('forum')->name('forum.')->group(function () {
    Route::get('/', [ForumController::class, 'index'])->name('index');
    Route::get('/category/{category}', [ForumController::class, 'category'])->name('category');
    Route::get('/category/{category}/{topic}', [ForumController::class, 'topic'])->name('topic');
    Route::get('/search', [ForumController::class, 'search'])->name('search');
    
    // Auth required
    Route::middleware('auth')->group(function () {
        Route::get('/create', [ForumController::class, 'createTopic'])->name('create_topic');
        // Throttled: 10 posts per minute
        Route::post('/topics', [ForumController::class, 'storeTopic'])->name('topics.store')->middleware('throttle:10,1');
        // Throttled: 30 replies per minute
        Route::post('/category/{category}/{topic}/posts', [ForumController::class, 'storePost'])->name('posts.store')->middleware('throttle:30,1');
        Route::get('/category/{category}/{topic}/posts/{post}/edit', [ForumController::class, 'editPost'])->name('posts.edit');
        Route::put('/category/{category}/{topic}/posts/{post}', [ForumController::class, 'updatePost'])->name('posts.update');
        // Throttled: 60 likes per minute
        Route::post('/posts/{post}/like', [ForumController::class, 'likePost'])->name('posts.like')->middleware('throttle:60,1');
        Route::delete('/posts/{post}', [ForumController::class, 'deletePost'])->name('posts.delete');
        // Throttled: 10 uploads per minute
        Route::post('/upload-image', [ForumController::class, 'uploadImage'])->name('upload_image')->middleware('throttle:10,1');
    });
});

// ── AI Routes ──────────────────────────────────────────────────────────
Route::prefix('ai')->name('ai.')->group(function () {
    Route::get('/', [AIController::class, 'index'])->name('index');
    // Throttled: 20 AI chat requests per minute
    Route::post('/chat', [AIController::class, 'chat'])->name('chat')->middleware('throttle:20,1');
    
    // Auth required
    Route::middleware('auth')->group(function () {
        Route::get('/simulasyon', [AIController::class, 'simulationIndex'])->name('simulation');
        Route::get('/simulasyon/{scenario}', [AIController::class, 'startSimulation'])->name('start_simulation');
        Route::post('/simulasyon/submit', [AIController::class, 'submitSimulation'])->name('submit_simulation');
        Route::get('/simulasyon-sonuc/{simulation}', [AIController::class, 'showSimulationResult'])->name('simulation_result');
        
        // Admin only
        Route::middleware('admin')->group(function () {
            Route::get('/content-generator', [AIController::class, 'contentGenerator'])->name('content_generator');
            Route::post('/generate-content', [AIController::class, 'generateContent'])->name('generate_content');
        });
    });
});

// ── Notification Routes ────────────────────────────────────────────────
Route::middleware('auth')->prefix('notifications')->name('notifications.')->group(function () {
    Route::get('/', [NotificationController::class, 'index'])->name('index');
    Route::post('/mark-all-read', [NotificationController::class, 'markAllAsRead'])->name('mark_all_read');
    Route::post('/{notification}/read', [NotificationController::class, 'markAsRead'])->name('read');
    Route::delete('/{notification}', [NotificationController::class, 'destroy'])->name('destroy');
});

// Notification AJAX endpoints
Route::get('/api/notifications/unread-count', [NotificationController::class, 'unreadCount']);
Route::get('/api/notifications/latest', [NotificationController::class, 'latest']);

// ── Forum Notification Routes ──────────────────────────────────────────
Route::middleware('auth')->prefix('forum-notifications')->name('forum.notifications.')->group(function () {
    Route::get('/', [ForumNotificationController::class, 'index'])->name('index');
    Route::post('/mark-all-read', [ForumNotificationController::class, 'markAllAsRead'])->name('mark_all_read');
    Route::post('/{notification}/read', [ForumNotificationController::class, 'markAsRead'])->name('read');
    Route::delete('/{notification}', [ForumNotificationController::class, 'delete'])->name('delete');
    Route::delete('/delete-read', [ForumNotificationController::class, 'deleteRead'])->name('delete_read');
});

// Forum Notification AJAX endpoint
Route::middleware('auth')->get('/api/forum-notifications/unread-count', [ForumNotificationController::class, 'unreadCount'])->name('forum.notifications.unread_count');

// ── Chat Routes ───────────────────────────────────────────────────────
Route::prefix('sohbet')->name('chat.')->group(function () {
    Route::get('/', [ChatController::class, 'index'])->name('index');
    Route::get('/ara', [ChatController::class, 'search'])->name('search');

    // Auth-required routes
    Route::middleware('auth')->group(function () {
        Route::get('/oda-olustur', [ChatController::class, 'create'])->name('create');
        // Throttled: 5 room creations per hour
        Route::post('/oda-olustur', [ChatController::class, 'store'])->name('store')->middleware('throttle:5,60');
        Route::get('/{room}/duzenle', [ChatController::class, 'edit'])->name('edit');
        Route::put('/{room}', [ChatController::class, 'update'])->name('update');
        Route::delete('/{room}', [ChatController::class, 'destroy'])->name('destroy');
        // Throttled: 60 messages per minute
        Route::post('/{room}/mesaj-gonder', [ChatController::class, 'sendMessage'])->name('send')->middleware('throttle:60,1');
        Route::delete('/mesajlar/{message}', [ChatController::class, 'deleteMessage'])->name('delete_message');
    });

    Route::get('/{room}/mesajlar', [ChatController::class, 'fetchMessages'])->name('fetch');
    Route::get('/{room}', [ChatController::class, 'show'])->name('show');
});

// ── Calculators Routes ────────────────────────────────────────────────
Route::prefix('hesaplamalar')->name('calculators.')->group(function () {
    Route::get('/', [CalculatorController::class, 'index'])->name('index');
    Route::get('/bmi', [CalculatorController::class, 'bmiCalculator'])->name('bmi');
    Route::post('/bmi', [CalculatorController::class, 'calculateBmi'])->name('calculate_bmi');
    Route::get('/maas', [CalculatorController::class, 'salaryCalculator'])->name('maas');
    Route::post('/maas', [CalculatorController::class, 'calculateSalary'])->name('calculate_maas');
    Route::get('/kpss', [CalculatorController::class, 'kpssCalculator'])->name('kpss');
    Route::post('/kpss', [CalculatorController::class, 'calculateKpss'])->name('calculate_kpss');
    Route::get('/yas-gorev', [CalculatorController::class, 'agePositionCalculator'])->name('age_position');
    Route::post('/yas-gorev', [CalculatorController::class, 'calculateAgePosition'])->name('calculate_age_position');
    Route::get('/safak', [CalculatorController::class, 'safakCalculator'])->name('safak');
    Route::post('/safak', [CalculatorController::class, 'calculateSafak'])->name('calculate_safak');
    Route::get('/gecmisim', [CalculatorController::class, 'history'])->name('history');
});

// ── Public Interview Routes ────────────────────────────────────────────
Route::prefix('interview')->name('interview.')->group(function () {
    Route::get('/', [InterviewController::class, 'index'])->name('index');
    Route::get('/category/{category}', [InterviewController::class, 'category'])->name('category');
    Route::get('/category/{category}/quiz', [InterviewController::class, 'startQuiz'])->name('start_quiz');
    Route::post('/category/{category}/submit', [InterviewController::class, 'submitQuiz'])->name('submit_quiz');
    Route::get('/category/{category}/guest-result', [InterviewController::class, 'guestResult'])->name('guest_result');
    Route::get('/category/{category}/result/{attempt}', [InterviewController::class, 'result'])->name('result');
    Route::get('/category/{category}/flashcards', [InterviewController::class, 'flashcards'])->name('flashcards');
    Route::get('/leaderboard', [InterviewController::class, 'leaderboard'])->name('leaderboard');
    Route::get('/my-attempts', [InterviewController::class, 'myAttempts'])->name('my_attempts');
});

// ── Public Library Routes ──────────────────────────────────────────────
Route::prefix('library')->name('library.')->group(function () {
    Route::get('/', [LibraryController::class, 'index'])->name('index');
    Route::get('/category/{category}', [LibraryController::class, 'category'])->name('category');
    Route::get('/category/{category}/{content}', [LibraryController::class, 'show'])->name('show');
    Route::get('/search', [LibraryController::class, 'search'])->name('search');
});

// ── Public Blog Routes ─────────────────────────────────────────────────
Route::prefix('blog')->name('blog.')->group(function () {
    Route::get('/', [PublicBlogController::class, 'index'])->name('index');
    Route::get('/category/{category}', [PublicBlogController::class, 'category'])->name('category');
    Route::get('/{post}', [PublicBlogController::class, 'show'])->name('show');
});

// ── Blog Comments (Guests allowed for POST, auth required for DELETE) ────
Route::post('/blog/{post}/comments', [BlogCommentController::class, 'store'])->name('blog.comments.store');
Route::middleware(['auth'])->group(function () {
    Route::delete('/blog/comments/{comment}', [BlogCommentController::class, 'destroy'])->name('blog.comments.destroy');
});

// ── Library Comments ──────────────────────────────────────────────────
Route::post('/library/contents/{content}/comments', [LibraryCommentController::class, 'store'])->name('library.comments.store');
Route::middleware(['auth'])->group(function () {
    Route::delete('/library/comments/{comment}', [LibraryCommentController::class, 'destroy'])->name('library.comments.destroy');
});

require __DIR__.'/auth.php';
