<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FrontendController;

Route::get('/', [FrontendController::class, 'home'])->name('home');
Route::get('/about', [FrontendController::class, 'about'])->name('about');
Route::get('/services', [FrontendController::class, 'services'])->name('services');
Route::get('/contact', [FrontendController::class, 'contact'])->name('contact');
Route::post('/contact', [FrontendController::class, 'submitContact'])->name('contact.submit');

Route::get('/news', [FrontendController::class, 'news'])->name('news');
Route::get('/news/{slug}', [FrontendController::class, 'newsSingle'])->name('news.show');

Route::get('/division/{id}', [FrontendController::class, 'division'])->name('division.show');
Route::get('/portfolio/{id}', [FrontendController::class, 'portfolio'])->name('portfolio.show');

// Admin Panel Routes
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
    // Guest Admin Routes
    Route::middleware('guest:admin')->group(function () {
        Route::get('/login', [\App\Http\Controllers\AdminAuthenticatedSessionController::class, 'create'])->name('login');
        Route::post('/login', [\App\Http\Controllers\AdminAuthenticatedSessionController::class, 'store'])->name('login.submit');
    });

    // Authenticated Admin Routes
    Route::middleware('auth:admin')->group(function () {
        Route::post('/logout', [\App\Http\Controllers\AdminAuthenticatedSessionController::class, 'destroy'])->name('logout');
        
        Route::get('/dashboard', [\App\Http\Controllers\Admin\DashboardController::class, 'index'])->name('dashboard');
        Route::get('/activities', [\App\Http\Controllers\Admin\DashboardController::class, 'activities'])->name('activities');

        // Profile
        Route::get('/profile', [\App\Http\Controllers\Admin\ProfileController::class, 'edit'])->name('profile.edit');
        Route::put('/profile', [\App\Http\Controllers\Admin\ProfileController::class, 'update'])->name('profile.update');

        // Settings (Superadmin only)
        Route::middleware('role:superadmin')->group(function () {
            Route::get('/settings', [\App\Http\Controllers\Admin\SettingController::class, 'index'])->name('settings.index');
            Route::post('/settings', [\App\Http\Controllers\Admin\SettingController::class, 'update'])->name('settings.update');
            
            // Admin User Management
            Route::resource('users', \App\Http\Controllers\Admin\AdminUserController::class)->except(['show']);
        });

        // Content Management (Superadmin & Editor)
        Route::middleware('role:superadmin,editor,manager')->group(function () {
            // Blog / News
            Route::resource('posts', \App\Http\Controllers\Admin\PostController::class)->except(['show']);

            // FAQs
            Route::patch('faqs/{faq}/toggle', [\App\Http\Controllers\Admin\FaqController::class, 'toggle'])->name('faqs.toggle');
            Route::resource('faqs', \App\Http\Controllers\Admin\FaqController::class)->except(['show']);
        });

        // Content Management (Superadmin & Editor only - higher sensitivity)
        Route::middleware('role:superadmin,editor')->group(function () {
            // Divisions
            Route::resource('divisions', \App\Http\Controllers\Admin\DivisionController::class)->except(['show']);
            
            // Portfolio
            Route::resource('portfolio-categories', \App\Http\Controllers\Admin\PortfolioCategoryController::class)->except(['show']);
            Route::resource('portfolio', \App\Http\Controllers\Admin\PortfolioItemController::class)->except(['show'])->parameters(['portfolio' => 'item']);



            // Testimonials
            Route::resource('testimonials', \App\Http\Controllers\Admin\TestimonialController::class)->except(['show']);



            // Hero Slides
            Route::patch('hero-slides/{slide}/toggle', [\App\Http\Controllers\Admin\HeroSlideController::class, 'toggle'])->name('hero-slides.toggle');
            Route::resource('hero-slides', \App\Http\Controllers\Admin\HeroSlideController::class)->except(['show'])->parameters(['hero-slides' => 'slide']);

            // SEO Management
            Route::get('seo', [\App\Http\Controllers\Admin\SeoController::class, 'index'])->name('seo.index');
            Route::get('seo/{seo}/edit', [\App\Http\Controllers\Admin\SeoController::class, 'edit'])->name('seo.edit');
            Route::put('seo/{seo}', [\App\Http\Controllers\Admin\SeoController::class, 'update'])->name('seo.update');

            Route::post('media/{media}/crop', [\App\Http\Controllers\Admin\MediaController::class, 'crop'])->name('media.crop');
            Route::delete('media/{media}', [\App\Http\Controllers\Admin\MediaController::class, 'destroy'])->name('media.destroy');
            Route::post('media', [\App\Http\Controllers\Admin\MediaController::class, 'store'])->name('media.store');
        });

        // Viewer (Read-only sections)
        Route::get('media', [\App\Http\Controllers\Admin\MediaController::class, 'index'])->name('media.index');
        Route::resource('messages', \App\Http\Controllers\Admin\ContactMessageController::class)->only(['index']);
        
        // Content Management & HR (Superadmin, Editor, HR)
        Route::middleware('role:superadmin,editor,hr')->group(function () {
            // Team
            Route::patch('team/{team}/toggle', [\App\Http\Controllers\Admin\TeamMemberController::class, 'toggle'])->name('team.toggle');
            Route::post('team/reorder', [\App\Http\Controllers\Admin\TeamMemberController::class, 'reorder'])->name('team.reorder');
            Route::resource('team', \App\Http\Controllers\Admin\TeamMemberController::class)->except(['show']);

            // Partners / Clients
            Route::post('partners/reorder', [\App\Http\Controllers\Admin\PartnerController::class, 'reorder'])->name('partners.reorder');
            Route::resource('partners', \App\Http\Controllers\Admin\PartnerController::class)->except(['show']);
        });

        // Restricted messages (Delete)
        Route::middleware('role:superadmin,editor,manager')->group(function () {
            Route::delete('messages/{message}', [\App\Http\Controllers\Admin\ContactMessageController::class, 'destroy'])->name('messages.destroy');
        });


    });
});
