<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\NewsController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\MaintenanceAdminLoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Auth\VerificationController;
use App\Http\Controllers\Admin\ActivityLogController as AdminActivityLogController;
use App\Http\Controllers\Admin\LoginHistoryController as AdminLoginHistoryController;
use App\Http\Controllers\Admin\NewsController as AdminNewsController;
use App\Http\Controllers\Admin\NotificationController as AdminNotificationController;
use App\Http\Controllers\Admin\OperationsController as AdminOperationsController;
use App\Http\Controllers\Admin\SearchController as AdminSearchController;
use App\Http\Controllers\Admin\SystemUpdateController as AdminSystemUpdateController;
use App\Http\Controllers\Admin\TwoFactorSecurityController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\Auth\TwoFactorChallengeController;
use App\Http\Controllers\LicenseActivationController;
use App\Http\Middleware\EnsureValidLicense;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::withoutMiddleware([EnsureValidLicense::class])->group(function () {
    Route::get('/license', [LicenseActivationController::class, 'show'])->name('license.show');
    Route::post('/license', [LicenseActivationController::class, 'store'])
        ->middleware('throttle:license-activate')
        ->name('license.store');
});

Route::get('/', [NewsController::class, 'index'])->name('home');

// Public Team List (nur Admins, nicht gesperrt)
Route::get('/team', function () {
    $users = \App\Models\User::query()
        ->where('is_admin', true)
        ->where('is_banned', false)
        ->orderBy('created_at', 'desc')
        ->get();

    return view('team', compact('users'));
})->name('team');

// Language switcher
Route::get('/language/{locale}', function ($locale) {
    if (in_array($locale, ['de', 'en'])) {
        session(['locale' => $locale]);
        app()->setLocale($locale);
    }
    return redirect()->back();
})->name('language.switch');

// Admin-Login nur sinnvoll während Wartungsmodus (Route bleibt erreichbar; außerhalb → Redirect auf normales Login)
Route::get('/wartung/admin-anmeldung', [MaintenanceAdminLoginController::class, 'create'])->name('maintenance.admin.login');
Route::post('/wartung/admin-anmeldung', [MaintenanceAdminLoginController::class, 'store'])->middleware('throttle:login')->name('maintenance.admin.login.store');

// Authentication Routes
Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login'])->middleware('throttle:login');
Route::get('/two-factor/challenge', [TwoFactorChallengeController::class, 'create'])->name('two-factor.challenge');
Route::post('/two-factor/challenge', [TwoFactorChallengeController::class, 'store'])->middleware('throttle:two-factor')->name('two-factor.verify');
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

// Registration Routes
Route::get('/register', [RegisterController::class, 'showRegistrationForm'])->name('register');
Route::post('/register', [RegisterController::class, 'register']);

// Password Reset Routes
Route::get('/password/reset', [ForgotPasswordController::class, 'showLinkRequestForm'])->name('password.request');
Route::post('/password/email', [ForgotPasswordController::class, 'sendResetLinkEmail'])->name('password.email');
Route::get('/password/reset/{token}', [ResetPasswordController::class, 'showResetForm'])->name('password.reset');
Route::post('/password/reset', [ResetPasswordController::class, 'reset'])->name('password.update');

// Email Verification Routes
Route::get('/email/verify', [VerificationController::class, 'show'])->name('verification.notice');
Route::get('/email/verify/{id}/{hash}', [VerificationController::class, 'verify'])->name('verification.verify');
Route::post('/email/resend', [VerificationController::class, 'resend'])->name('verification.resend');

// News Routes
Route::get('/news/{news}', [NewsController::class, 'show'])->name('news.show');
Route::post('/news/{news}/comments', [NewsController::class, 'storeComment'])->name('news.comments.store')->middleware(['auth', 'throttle:comments']);
Route::delete('/news/{news}/comments/{comment}', [NewsController::class, 'destroyComment'])->name('news.comments.destroy')->middleware('auth');

// Admin Routes
Route::prefix('admin')->name('admin.')->middleware(['auth', 'admin'])->group(function () {
    Route::get('/', function () {
        return view('admin.dashboard');
    })->name('dashboard');

    Route::get('/search/suggestions', [AdminSearchController::class, 'suggestions'])->name('search.suggestions');
    Route::get('/search', [AdminSearchController::class, 'index'])->name('search');

    Route::get('/notifications/feed', [AdminNotificationController::class, 'feed'])->name('notifications.feed');
    Route::post('/notifications/mark-read', [AdminNotificationController::class, 'markRead'])->name('notifications.mark-read');
    Route::post('/notifications/clear-history', [AdminNotificationController::class, 'clearHistory'])->name('notifications.clear-history');

    Route::get('/activity-log', [AdminActivityLogController::class, 'index'])->name('activity-log.index');

    Route::get('/operations', [AdminOperationsController::class, 'index'])->name('operations.index');
    Route::post('/operations/backup', [AdminOperationsController::class, 'runBackup'])
        ->middleware('throttle:6,60')
        ->name('operations.backup');
    Route::post('/operations/maintenance/enable', [AdminOperationsController::class, 'maintenanceEnable'])
        ->middleware('throttle:12,60')
        ->name('operations.maintenance.enable');
    Route::post('/operations/maintenance/disable', [AdminOperationsController::class, 'maintenanceDisable'])
        ->middleware('throttle:12,60')
        ->name('operations.maintenance.disable');

    Route::get('/system-update', [AdminSystemUpdateController::class, 'index'])->name('system-update.index');
    Route::post('/system-update/apply', [AdminSystemUpdateController::class, 'apply'])
        ->middleware('throttle:3,120')
        ->name('system-update.apply');

    Route::get('/login-history', [AdminLoginHistoryController::class, 'index'])->name('login-history.index');

    Route::get('/security/two-factor', [TwoFactorSecurityController::class, 'show'])->name('security.two-factor');
    Route::post('/security/two-factor/begin', [TwoFactorSecurityController::class, 'begin'])->name('security.two-factor.begin');
    Route::post('/security/two-factor/confirm', [TwoFactorSecurityController::class, 'confirm'])->name('security.two-factor.confirm');
    Route::post('/security/two-factor/disable', [TwoFactorSecurityController::class, 'disable'])->name('security.two-factor.disable');

    // News Management
    Route::resource('news', AdminNewsController::class);

    // User Management
    Route::resource('users', AdminUserController::class);
    Route::patch('users/{user}/toggle-admin', [AdminUserController::class, 'toggleAdmin'])->name('users.toggle-admin');
    Route::patch('users/{user}/toggle-ban', [AdminUserController::class, 'toggleBan'])->name('users.toggle-ban');
    
    // Team List
    Route::get('/team', [AdminUserController::class, 'team'])->name('team');
    
    // Settings
    Route::get('/settings', [App\Http\Controllers\Admin\SettingController::class, 'index'])->name('settings.index');
    Route::put('/settings', [App\Http\Controllers\Admin\SettingController::class, 'update'])->name('settings.update');
    Route::get('/settings/general', [App\Http\Controllers\Admin\SettingController::class, 'general'])->name('settings.general');
    Route::put('/settings/general', [App\Http\Controllers\Admin\SettingController::class, 'updateGeneral'])->name('settings.general.update');
    Route::get('/settings/logo', [App\Http\Controllers\Admin\SettingController::class, 'logo'])->name('settings.logo');
    Route::put('/settings/logo', [App\Http\Controllers\Admin\SettingController::class, 'updateLogo'])->name('settings.logo.update');
    Route::get('/settings/registration', [App\Http\Controllers\Admin\SettingController::class, 'registration'])->name('settings.registration');
    Route::put('/settings/registration', [App\Http\Controllers\Admin\SettingController::class, 'updateRegistration'])->name('settings.registration.update');
});
