<?php
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\SitemapController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\PostsController;
use App\Http\Controllers\Admin\CommentsController;
use App\Http\Controllers\Admin\SourcesController;
use Illuminate\Support\Facades\Route;

// ── Admin auth ────────────────────────────────────────────────────────────────
Route::get ('/admin/login',  [AuthController::class, 'showLogin'])->name('admin.login');
Route::post('/admin/login',  [AuthController::class, 'login']);
Route::post('/admin/logout', [AuthController::class, 'logout'])->name('admin.logout');

// ── Admin panel (requires auth + admin/editor role) ───────────────────────────
Route::prefix('admin')->name('admin.')->middleware(['auth', 'admin.role'])->group(function () {

    Route::get('/',          [DashboardController::class, 'index'])->name('dashboard');

    // Posts
    Route::get   ('posts',              [PostsController::class, 'index'])->name('posts.index');
    Route::get   ('posts/{post}/edit',  [PostsController::class, 'edit'])->name('posts.edit');
    Route::put   ('posts/{post}',       [PostsController::class, 'update'])->name('posts.update');
    Route::delete('posts/{post}',       [PostsController::class, 'destroy'])->name('posts.destroy');
    Route::post  ('posts/{post}/featured', [PostsController::class, 'toggleFeatured'])->name('posts.featured');
    Route::post  ('posts/{post}/breaking', [PostsController::class, 'toggleBreaking'])->name('posts.breaking');

    // Comments
    Route::get   ('comments',                    [CommentsController::class, 'index'])->name('comments.index');
    Route::post  ('comments/{comment}/approve',  [CommentsController::class, 'approve'])->name('comments.approve');
    Route::post  ('comments/{comment}/spam',     [CommentsController::class, 'spam'])->name('comments.spam');
    Route::delete('comments/{comment}',          [CommentsController::class, 'destroy'])->name('comments.destroy');
    Route::post  ('comments/bulk',               [CommentsController::class, 'bulkAction'])->name('comments.bulk');

    // Sources
    Route::get  ('sources',                 [SourcesController::class, 'index'])->name('sources.index');
    Route::post ('sources/{source}/toggle', [SourcesController::class, 'toggle'])->name('sources.toggle');
    Route::delete('sources/{source}',       [SourcesController::class, 'destroy'])->name('sources.destroy');
    Route::get  ('crawl-logs',              [SourcesController::class, 'logs'])->name('crawl.logs');
});

Route::get('/', fn() => redirect('/admin'));

// Sitemaps (public)
Route::get('sitemap.xml',      [SitemapController::class, 'index']);
Route::get('sitemap-news.xml', [SitemapController::class, 'news']);
