<?php

use App\Http\Controllers\ActivityLogController;
use App\Http\Controllers\AgentController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\BackupsController;
use App\Http\Controllers\DatabaseManagerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SystemHealthController;
use App\Http\Controllers\WebsiteController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return auth()->check()
        ? redirect()->route('dashboard')
        : redirect()->route('login');
});

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthenticatedSessionController::class, 'create'])->name('login');
    Route::post('/login', [AuthenticatedSessionController::class, 'store'])->name('login.store');
});

Route::middleware(['auth', 'super.admin', 'track.activity'])->group(function () {
    Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');

    Route::get('/dashboard', DashboardController::class)->name('dashboard');
    Route::get('/search', SearchController::class)->name('search');

    Route::resource('websites', WebsiteController::class)->except(['destroy']);
    Route::post('/websites/{website}/sync', [WebsiteController::class, 'sync'])->name('websites.sync');
    Route::post('/websites/{website}/toggle-status', [WebsiteController::class, 'toggleStatus'])->name('websites.toggle-status');
    Route::delete('/websites/{website}', [WebsiteController::class, 'destroy'])->name('websites.destroy');

    Route::get('/database-manager', DatabaseManagerController::class)->name('database-manager.index');

    Route::get('/backups', [BackupsController::class, 'index'])->name('backups.index');
    Route::post('/websites/{website}/backups', [BackupsController::class, 'store'])->name('backups.store');
    Route::post('/backups/{backup}/restore', [BackupsController::class, 'restore'])->name('backups.restore');
    Route::get('/backups/{backup}/download', [BackupsController::class, 'download'])->name('backups.download');
    Route::delete('/backups/{backup}', [BackupsController::class, 'destroy'])->name('backups.destroy');

    Route::get('/activity-logs', ActivityLogController::class)->name('activity-logs.index');
    Route::get('/agents', AgentController::class)->name('agents.index');
    Route::get('/system-health', SystemHealthController::class)->name('system-health.index');

    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
    Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');

    Route::get('/profile', ProfileController::class)->name('profile.show');
});
