<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\Admin\AdminThemeController;
use App\Http\Controllers\Api\Admin\SettingsController;
use App\Http\Controllers\Api\Admin\UserManagementController;
use App\Http\Controllers\Api\Admin\PageController;
use App\Http\Controllers\Api\Admin\CategoryController;
use App\Http\Controllers\Api\Admin\AdminPostController;
use App\Http\Controllers\Api\Admin\SeoSettingsController;
use App\Http\Controllers\Api\Admin\EditorUploadController;
use App\Http\Controllers\Api\Admin\PostSettingsController;
use App\Http\Controllers\Api\Admin\DashboardStatsController;
use App\Http\Controllers\Api\Admin\AdPlacementController;
use App\Http\Controllers\Api\Admin\AuthSettingsController; 
use App\Http\Controllers\Api\Admin\RssFeedController;
use App\Http\Controllers\Api\Admin\AiProviderController;
use App\Http\Controllers\Api\Admin\AiSettingsController;
use App\Http\Controllers\Api\Admin\SchedulerController;
use App\Http\Controllers\Api\Admin\LanguageController;
use App\Http\Controllers\Api\Admin\SocialSettingsController; 
use App\Http\Controllers\Api\Admin\AppearanceSettingsController;
use App\Http\Controllers\Api\Admin\CodeInjectionController;
use App\Http\Controllers\Api\Admin\PwaSettingsController;
use App\Http\Controllers\Api\Admin\SystemController;
use App\Http\Controllers\Api\Admin\PermalinkSettingsController;

// Apply 'auth:sanctum' (Must be logged in) AND 'admin' (Must be role:admin)
// These routes are prefixed with /api/admin/ automatically by routes/api.php
Route::middleware(['auth:sanctum', 'admin'])->group(function () {
    
    // Dashboard Stats
    Route::get('/dashboard-stats', [DashboardStatsController::class, 'index']);
    
    // 1. Theme Management
    Route::patch('/theme', [AdminThemeController::class, 'update']);
    
    // 2. SMTP / Mail Settings
    Route::get('/settings/smtp', [SettingsController::class, 'getSmtp']);
    Route::post('/settings/smtp', [SettingsController::class, 'updateSmtp']);
    Route::post('/settings/smtp/test', [SettingsController::class, 'sendTestEmail']);

    // 3. User Management
    Route::get('/users', [UserManagementController::class, 'index']);
    Route::put('/users/{user}', [UserManagementController::class, 'update']);
    Route::delete('/users/{user}', [UserManagementController::class, 'destroy']);

    // 4. Custom Pages (CRUD)
    Route::apiResource('pages', PageController::class);

    // 5. Categories (CRUD)
    Route::post('/categories/reorder', [CategoryController::class, 'reorder']);
    Route::apiResource('categories', CategoryController::class);

    // 6. News / Posts Management (CRUD)
    Route::patch('/posts/{post}/flags', [AdminPostController::class, 'updateFlags']);
    Route::apiResource('posts', AdminPostController::class);

    // 7. SEO Settings
    Route::get('/settings/seo', [SeoSettingsController::class, 'index']);
    Route::post('/settings/seo', [SeoSettingsController::class, 'update']);

    // 8. Editor Image Upload (Used by Tiptap Editor in Admin Panel)
    Route::post('/editor/image-upload', [EditorUploadController::class, 'uploadImage'])->name('editor.image.upload');

    // 9. Post Configuration (Enable/Disable Submissions, Auto-Approval)
    Route::get('/settings/post', [PostSettingsController::class, 'index']);
    Route::post('/settings/post', [PostSettingsController::class, 'update']);

    // 10. Ad Placements
    Route::apiResource('ads', AdPlacementController::class)->except(['show']);

    // 11. Auth Settings
    Route::get('/settings/auth', [AuthSettingsController::class, 'index']);
    Route::post('/settings/auth', [AuthSettingsController::class, 'update']);

    // 12. Social Settings
    Route::get('/settings/social', [SocialSettingsController::class, 'index']);
    Route::post('/settings/social', [SocialSettingsController::class, 'update']);
    
    // 13. RSS Feeds
    Route::get('/rss-feeds/form-data', [RssFeedController::class, 'getFormData']);
    Route::apiResource('rss-feeds', RssFeedController::class);
    Route::post('/rss-feeds/{rssFeed}/crawl', [RssFeedController::class, 'crawl']);

    // 14. AI Provider Routes
    Route::post('/ai-providers/{aiProvider}/set-active', [AiProviderController::class, 'setActive']);
    Route::apiResource('ai-providers', AiProviderController::class);
    Route::get('/settings/ai', [AiSettingsController::class, 'index']);
    Route::post('/settings/ai', [AiSettingsController::class, 'update']);

    // 15. Scheduler / Crawler Settings
    Route::get('/system/scheduler', [SchedulerController::class, 'index']);
    Route::post('/system/scheduler/regenerate-token', [SchedulerController::class, 'regenerateToken']);
    Route::post('/system/scheduler/run-crawl', [SchedulerController::class, 'runGlobalCrawl']);
    Route::post('/system/scheduler/settings', [SchedulerController::class, 'updateSettings']);

    // 16. Language
    Route::get('/languages', [LanguageController::class, 'index']);
    Route::post('/languages', [LanguageController::class, 'store']);
    Route::post('/languages/{language}/default', [LanguageController::class, 'setDefault']);
    Route::delete('/languages/{language}', [LanguageController::class, 'destroy']);

    // Translation File Editing
    Route::get('/languages/{code}/translations', [LanguageController::class, 'getTranslations']);
    Route::put('/languages/{code}/translations', [LanguageController::class, 'updateTranslations']);

    // 17. Appearance
    Route::get('/settings/appearance', [AppearanceSettingsController::class, 'index']);
    Route::post('/settings/appearance', [AppearanceSettingsController::class, 'update']);

    // 18. Code Injection
    Route::get('/settings/code-injection', [CodeInjectionController::class, 'index']);
    Route::post('/settings/code-injection', [CodeInjectionController::class, 'update']);

    // 19. System Maintenance
    Route::post('/system/cache-clear', [SystemController::class, 'clearCache']);
    Route::post('/system/migrate', [SystemController::class, 'runMigration']);
    Route::post('/system/storage-link', [SystemController::class, 'linkStorage']);

    // PWA ROUTES
    Route::get('/settings/pwa', [PwaSettingsController::class, 'index']);
    Route::post('/settings/pwa', [PwaSettingsController::class, 'update']);

    Route::post('/system/cache-clear', [SystemController::class, 'clearCache']);
    Route::post('/system/migrate', [SystemController::class, 'runMigration']);
    Route::post('/system/storage-link', [SystemController::class, 'linkStorage']);

    // Permalink Settings
    Route::get('/settings/permalinks',[PermalinkSettingsController::class, 'index']);
    Route::post('/settings/permalinks',[PermalinkSettingsController::class, 'update']);

});