<?php

use App\Http\Controllers\HomeController; 
use App\Http\Controllers\Admin\LanguageController;
use App\Http\Controllers\Admin\GlobalizationController;
use App\Http\Controllers\MediaController; 
use App\Http\Controllers\PublicPageController;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
*/

// 1. Public Home Page (Guests can view this now!)
Route::get('/', [HomeController::class, 'index'])->name('home');


// 2. Protected Core Routes (Dashboard & Home)
Route::middleware(['auth', 'verified'])->group(function () {
    
    // --- CLEANED UP: Pointing directly to HomeController ---
    Route::get('/home', [HomeController::class, 'index'])->name('home');

    // Dynamic Dashboard Redirect
    Route::get('/dashboard', function () {
        $role = auth()->user()->role ?? 'user';
        if (in_array($role, ['seller', 'admin', 'manager'])) {
            return redirect()->route('dashboard.seller');
        }
        return redirect()->route('dashboard.buyer');
    })->name('dashboard');

    Route::post('/upload-media', [MediaController::class, 'upload'])->name('media.upload');

    // Modular Authenticated Routes
    require __DIR__ . '/api/buyer.php';
    require __DIR__ . '/api/seller.php';
    require __DIR__ . '/api/setting.php';
    require __DIR__ . '/api/chat.php'; 

    // --- Notification Routes ---
    Route::get('/api/notifications', [\App\Http\Controllers\NotificationController::class, 'index'])->name('notifications.index');
    Route::get('/api/notifications/unread-count', [\App\Http\Controllers\NotificationController::class, 'getUnreadCount'])->name('notifications.unread');
    Route::post('/api/notifications/{id}/read', [\App\Http\Controllers\NotificationController::class, 'markAsRead'])->name('notifications.read');
    Route::post('/api/notifications/read-all', [\App\Http\Controllers\NotificationController::class, 'markAllAsRead'])->name('notifications.read-all');
    Route::delete('/api/notifications/clear-all', [\App\Http\Controllers\NotificationController::class, 'clearAll'])->name('notifications.clear-all'); 
});


// 3. Admin Control Panel
Route::middleware(['auth', 'admin'])->prefix('cp')->group(function () {
    Route::get('/dashboard', function () {
        return Inertia::render('cp/Dashboard');
    })->name('cp.dashboard');
});


/*
|--------------------------------------------------------------------------
| Independent Modular Routes
|--------------------------------------------------------------------------
*/

// Finance & Payments
require __DIR__ . '/api/finance.php';

// Marketplace Routes (Public & Private)
require __DIR__ . '/api/marketplace.php';

// Language Switcher
Route::get('/language/{code}', [LanguageController::class, 'switchLanguage'])->name('language.switch');

// Currency Switcher
Route::get('/currency/{code}', function ($code) {
    if (\App\Models\Currency::where('code', $code)->where('is_active', true)->exists()) {
        session(['currency' => $code]);
    }
    return back();
})->name('currency.switch');

// Locations API
Route::get('/api/locations/countries', [GlobalizationController::class, 'getCountries']);
Route::get('/api/locations/countries/{id}/states', [GlobalizationController::class, 'getStates']);
Route::get('/api/locations/states/{id}/cities', [GlobalizationController::class, 'getCities']);

// Custom Public Pages (Fallback Route)
Route::get('/p/{slug}', [PublicPageController::class, 'show'])->name('custom.page');

// PWA Dynamic Routes
Route::get('/manifest.json', [\App\Http\Controllers\PwaController::class, 'manifest'])->name('pwa.manifest');
Route::get('/sw.js', [\App\Http\Controllers\PwaController::class, 'serviceWorker'])->name('pwa.sw');

/*
|--------------------------------------------------------------------------
| SEO & Sitemaps (Public)
|--------------------------------------------------------------------------
*/
Route::get('/sitemap.xml', [\App\Http\Controllers\SitemapController::class, 'index'])->name('sitemap.index');
Route::get('/sitemaps/products.xml', [\App\Http\Controllers\SitemapController::class, 'products'])->name('sitemap.products');
Route::get('/sitemaps/categories.xml', [\App\Http\Controllers\SitemapController::class, 'categories'])->name('sitemap.categories');
Route::get('/sitemaps/stores.xml', [\App\Http\Controllers\SitemapController::class, 'stores'])->name('sitemap.stores');
Route::get('/sitemaps/pages.xml', [\App\Http\Controllers\SitemapController::class, 'pages'])->name('sitemap.pages');

require __DIR__.'/auth.php';