<?php

use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\DatabaseBackupController;
use App\Http\Controllers\Admin\BlogPostController;
use App\Http\Controllers\Admin\ContactController as AdminContactController;
use App\Http\Controllers\Admin\MediaController;
use App\Http\Controllers\Admin\PageController as AdminPageController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\ServiceController as AdminServiceController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\UserController as AdminUserController;
use App\Http\Controllers\Admin\AuditController as AdminAuditController;
use App\Http\Middleware\TrackVisit;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\InstallController;
use App\Http\Controllers\PageController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SitemapController;
use App\Http\Controllers\ServiceController;
use Illuminate\Support\Facades\Route;

Route::get('/install', [InstallController::class, 'index'])->name('install.index');
Route::post('/install', [InstallController::class, 'store'])->name('install.run');
Route::get('/sitemap.xml', [SitemapController::class, 'index'])->name('sitemap');
Route::get('/robots.txt', [SitemapController::class, 'robots'])->name('robots');

Route::middleware(TrackVisit::class)->group(function () {
    Route::get('/', [HomeController::class, 'index'])->name('home');
    Route::get('/about', [PageController::class, 'about'])->name('about');
    Route::get('/blog', [PageController::class, 'blog'])->name('blog');
    Route::get('/blog/{slug}', [PageController::class, 'blogPost'])->name('blog.show');
    Route::get('/contact', [ContactController::class, 'index'])->name('contact');
    Route::post('/contact', [ContactController::class, 'store'])->name('contact.store');
    Route::get('/services', [ServiceController::class, 'index'])->name('services.index');
    Route::get('/service/{slug}', [ServiceController::class, 'show'])->name('services.show');
});

// Legacy/legacy-auth support: some auth scaffolding expects a route named "dashboard".
// Redirect it to the admin dashboard route that exists in this app.
Route::middleware(['auth'])->get('/dashboard', function () {
    return redirect()->route('admin.dashboard');
})->name('dashboard');

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');
});

Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])
        ->middleware('permission:dashboard.view')
        ->name('dashboard');

    Route::get('pages', [AdminPageController::class, 'index'])
        ->middleware('permission:pages.view,pages.create,pages.update,pages.delete')
        ->name('pages.index');
    Route::get('pages/create', [AdminPageController::class, 'create'])
        ->middleware('permission:pages.create')
        ->name('pages.create');
    Route::post('pages', [AdminPageController::class, 'store'])
        ->middleware('permission:pages.create')
        ->name('pages.store');
    Route::get('pages/{page}/edit', [AdminPageController::class, 'edit'])
        ->middleware('permission:pages.update')
        ->name('pages.edit');
    Route::put('pages/{page}', [AdminPageController::class, 'update'])
        ->middleware('permission:pages.update')
        ->name('pages.update');
    Route::delete('pages/{page}', [AdminPageController::class, 'destroy'])
        ->middleware('permission:pages.delete')
        ->name('pages.destroy');

    Route::get('blog-posts', [BlogPostController::class, 'index'])
        ->middleware('permission:blog_posts.view,blog_posts.create,blog_posts.update,blog_posts.delete')
        ->name('blog-posts.index');
    Route::get('blog-posts/create', [BlogPostController::class, 'create'])
        ->middleware('permission:blog_posts.create')
        ->name('blog-posts.create');
    Route::post('blog-posts', [BlogPostController::class, 'store'])
        ->middleware('permission:blog_posts.create')
        ->name('blog-posts.store');
    Route::get('blog-posts/{blogPost}/edit', [BlogPostController::class, 'edit'])
        ->middleware('permission:blog_posts.update')
        ->name('blog-posts.edit');
    Route::put('blog-posts/{blogPost}', [BlogPostController::class, 'update'])
        ->middleware('permission:blog_posts.update')
        ->name('blog-posts.update');
    Route::delete('blog-posts/{blogPost}', [BlogPostController::class, 'destroy'])
        ->middleware('permission:blog_posts.delete')
        ->name('blog-posts.destroy');

    Route::get('services', [AdminServiceController::class, 'index'])
        ->middleware('permission:services.view,services.create,services.update,services.delete')
        ->name('services.index');
    Route::get('services/create', [AdminServiceController::class, 'create'])
        ->middleware('permission:services.create')
        ->name('services.create');
    Route::post('services', [AdminServiceController::class, 'store'])
        ->middleware('permission:services.create')
        ->name('services.store');
    Route::get('services/{service}/edit', [AdminServiceController::class, 'edit'])
        ->middleware('permission:services.update')
        ->name('services.edit');
    Route::put('services/{service}', [AdminServiceController::class, 'update'])
        ->middleware('permission:services.update')
        ->name('services.update');
    Route::delete('services/{service}', [AdminServiceController::class, 'destroy'])
        ->middleware('permission:services.delete')
        ->name('services.destroy');

    Route::get('contacts', [AdminContactController::class, 'index'])
        ->middleware('permission:contacts.view,contacts.update,contacts.delete')
        ->name('contacts.index');
    Route::get('contacts/{contact}', [AdminContactController::class, 'show'])
        ->middleware('permission:contacts.view,contacts.update,contacts.delete')
        ->name('contacts.show');
    Route::put('contacts/{contact}/notes', [AdminContactController::class, 'updateNotes'])
        ->middleware('permission:contacts.update')
        ->name('contacts.notes');
    Route::put('contacts/{contact}/mark-read', [AdminContactController::class, 'markRead'])
        ->middleware('permission:contacts.update')
        ->name('contacts.mark-read');
    Route::put('contacts/{contact}/mark-unread', [AdminContactController::class, 'markUnread'])
        ->middleware('permission:contacts.update')
        ->name('contacts.mark-unread');
    Route::put('contacts/{contact}/mark-replied', [AdminContactController::class, 'markReplied'])
        ->middleware('permission:contacts.update')
        ->name('contacts.mark-replied');
    Route::put('contacts/{contact}/archive', [AdminContactController::class, 'archive'])
        ->middleware('permission:contacts.update')
        ->name('contacts.archive');
    Route::post('contacts/bulk-status', [AdminContactController::class, 'bulkUpdateStatus'])
        ->middleware('permission:contacts.update')
        ->name('contacts.bulk-status');
    Route::delete('contacts/{contact}', [AdminContactController::class, 'destroy'])
        ->middleware('permission:contacts.delete')
        ->name('contacts.destroy');
    Route::post('contacts/bulk-delete', [AdminContactController::class, 'bulkDestroy'])
        ->middleware('permission:contacts.delete')
        ->name('contacts.bulk-delete');

    Route::get('settings', [SettingController::class, 'edit'])
        ->middleware('permission:settings.view,settings.update,settings.refresh')
        ->name('settings.edit');
    Route::put('settings', [SettingController::class, 'update'])
        ->middleware('permission:settings.update')
        ->name('settings.update');
    Route::post('settings/refresh-dashboard-data', [SettingController::class, 'refreshDashboardData'])
        ->middleware('permission:settings.refresh')
        ->name('settings.refresh-dashboard-data');
    Route::post('settings/delete-database', [SettingController::class, 'deleteDatabase'])
        ->middleware('role:admin')
        ->name('settings.delete-database');

    Route::get('media', [MediaController::class, 'index'])
        ->middleware('permission:media.view,media.upload,media.replace,media.delete,media.reorder')
        ->name('media.index');
    Route::get('media/browser', [MediaController::class, 'browser'])
        ->middleware('permission:media.view')
        ->name('media.browser');
    Route::get('media/{media}/details', [MediaController::class, 'details'])
        ->middleware('permission:media.view')
        ->name('media.details');
    Route::post('media', [MediaController::class, 'store'])
        ->middleware('permission:media.upload')
        ->name('media.store');
    Route::post('media/{media}/replace', [MediaController::class, 'replace'])
        ->middleware('permission:media.replace')
        ->name('media.replace');
    Route::delete('media/{media}', [MediaController::class, 'destroy'])
        ->middleware('permission:media.delete')
        ->name('media.destroy');
    Route::post('media/bulk-delete', [MediaController::class, 'bulkDestroy'])
        ->middleware('permission:media.delete')
        ->name('media.bulk-delete');
    Route::post('media/reorder', [MediaController::class, 'reorder'])
        ->middleware('permission:media.reorder')
        ->name('media.reorder');

    Route::get('roles', [RoleController::class, 'index'])
        ->middleware('permission:roles.view,roles.create,roles.update,roles.delete')
        ->name('roles.index');
    Route::get('roles/create', [RoleController::class, 'create'])
        ->middleware('permission:roles.create')
        ->name('roles.create');
    Route::post('roles', [RoleController::class, 'store'])
        ->middleware('permission:roles.create')
        ->name('roles.store');
    Route::get('roles/{role}/edit', [RoleController::class, 'edit'])
        ->middleware('permission:roles.update')
        ->name('roles.edit');
    Route::put('roles/{role}', [RoleController::class, 'update'])
        ->middleware('permission:roles.update')
        ->name('roles.update');
    Route::delete('roles/{role}', [RoleController::class, 'destroy'])
        ->middleware('permission:roles.delete')
        ->name('roles.destroy');

    Route::get('users', [AdminUserController::class, 'index'])
        ->middleware('permission:users.view,users.create,users.update,users.delete')
        ->name('users.index');
    Route::get('users/create', [AdminUserController::class, 'create'])
        ->middleware('permission:users.create')
        ->name('users.create');
    Route::post('users', [AdminUserController::class, 'store'])
        ->middleware('permission:users.create')
        ->name('users.store');
    Route::get('users/{user}/edit', [AdminUserController::class, 'edit'])
        ->middleware('permission:users.update')
        ->name('users.edit');
    Route::put('users/{user}', [AdminUserController::class, 'update'])
        ->middleware('permission:users.update')
        ->name('users.update');
    Route::delete('users/{user}', [AdminUserController::class, 'destroy'])
        ->middleware('permission:users.delete')
        ->name('users.destroy');

    Route::get('audits', [AdminAuditController::class, 'index'])
        ->middleware('permission:audits.view')
        ->name('audits.index');
    Route::get('backups/database', [DatabaseBackupController::class, 'download'])
        ->middleware('permission:backups.download')
        ->name('backups.database');
});

require __DIR__.'/auth.php';
