<?php

use App\Http\Controllers\HomeController;
use App\Http\Controllers\NewsController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\PublicProfileController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\TagController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\PublicPageController; 
use App\Http\Controllers\Api\Admin\SchedulerController;
use App\Http\Controllers\SitemapController; 
use App\Http\Controllers\FeedController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Cache; 
use App\Models\Setting;
use Inertia\Inertia;
use App\Models\Post; 

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

// --- 1. Public Routes ---

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

// Search Route
Route::get('/search', [NewsController::class, 'search'])->name('news.search');

// Single News Page
Route::get('/news/{slug}', [NewsController::class, 'show'])->name('news.show');

Route::get('/post/{id}',[NewsController::class, 'showById'])->name('post.show.id');

// Author Profile
Route::get('/author/{username}', [PublicProfileController::class, 'show'])->name('author.show');

// Category Page
Route::get('/category/{slug}', [CategoryController::class, 'show'])->name('category.show');

// Source Page (RSS Feed)
Route::get('/source/{rssFeed}', [App\Http\Controllers\SourceController::class, 'show'])->name('source.show');

// Tag Search Page
Route::get('/tag/{tag}', [TagController::class, 'show'])->name('tag.show');

// --- STATIC PAGES (About, Privacy, etc.) ---
// Using /p/ prefix to avoid conflict with news or categories
Route::get('/p/{slug}', [PublicPageController::class, 'show'])->name('page.show');

Route::get('/run-scheduler/{token}', [SchedulerController::class, 'runScheduler'])->name('scheduler.run');

// PWA MANIFEST ROUTE
Route::get('/manifest.json', function () {
    $pwa = Cache::rememberForever('pwa_settings', function () {
        return Setting::where('group', 'pwa')->pluck('value', 'key')->toArray();
    });

    if (($pwa['pwa_enabled'] ?? 'false') !== 'true') {
        return response()->json(['error' => 'PWA is disabled'], 404);
    }

    return response()->json([
        'name' => $pwa['pwa_name'] ?? config('app.name'),
        'short_name' => $pwa['pwa_short_name'] ?? config('app.name'),
        'start_url' => '/',
        'display' => 'standalone',
        'background_color' => $pwa['pwa_background_color'] ?? '#ffffff',
        'theme_color' => $pwa['pwa_theme_color'] ?? '#ffffff',
        'icons' => [
            [
                'src' => $pwa['pwa_icon'] ? asset($pwa['pwa_icon']) : '/placeholder.jpg',
                'sizes' => '512x512',
                'type' => 'image/png',
                'purpose' => 'any maskable'
            ]
        ]
    ]);
})->name('pwa.manifest');

Route::get('/sitemap.xml', [SitemapController::class, 'index'])->name('sitemap');
// Sub-Sitemaps
Route::get('/posts-sitemap.xml', [SitemapController::class, 'posts'])->name('sitemap.posts');
Route::get('/categories-sitemap.xml', [SitemapController::class, 'categories'])->name('sitemap.categories');
Route::get('/users-sitemap.xml', [SitemapController::class, 'users'])->name('sitemap.users');
Route::get('/pages-sitemap.xml', [SitemapController::class, 'pages'])->name('sitemap.pages');
Route::get('/tags-sitemap.xml', [SitemapController::class, 'tags'])->name('sitemap.tags');

Route::get('/feed', [FeedController::class, 'index'])->name('rss.feed');

// --- 2. Authenticated User Routes ---
Route::middleware(['auth', 'verified'])->group(function () {
    
    // Dashboard
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Submit News
    Route::get('/submit-news', function () {
        return Inertia::render('News/SubmitNews');
    })->name('news.submit');

    // My Posts List
    Route::get('/my-posts', function () {
        return Inertia::render('User/MyPosts');
    })->name('my-posts.index');

    // Edit Post
    Route::get('/my-posts/{post}/edit', function (Post $post) {
        if (request()->user()->cannot('update', $post)) {
            abort(403);
        }
        $post->load('images'); 
        return Inertia::render('User/EditPost', ['post' => $post]);
    })->name('my-posts.edit');
});


// --- 3. Settings / Profile Management ---
Route::middleware('auth')->group(function () {
    Route::get('/settings', [ProfileController::class, 'edit'])->name('settings.edit');
    Route::patch('/settings', [ProfileController::class, 'update'])->name('settings.update');
    Route::post('/settings/images', [ProfileController::class, 'updateImages'])->name('settings.images');
    Route::delete('/settings', [ProfileController::class, 'destroy'])->name('settings.destroy');
});

Route::post('/locale', function (Illuminate\Http\Request $request) {
    $request->validate(['locale' => 'required|exists:languages,code']);
    session(['locale' => $request->locale]);
    return back();
})->name('locale.switch');

Route::fallback(function () {
    return Inertia::render('NotFound')
        ->toResponse(request())
        ->setStatusCode(404);
});
// --- 4. Authentication Routes ---
require __DIR__.'/auth.php';