<?php

/**
 * Legacy routes — old site URL compatibility layer.
 *
 * These routes exist purely to keep old bookmarks / external links working.
 * Most are 301 redirects to their canonical replacements, or thin wrappers
 * around controllers that were never updated to use new URL patterns.
 *
 * Do NOT add new features here. When a legacy route is no longer needed,
 * remove it from this file.
 */

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Legacy\AvatarController;
use App\Http\Controllers\Legacy\CategoryRedirectController;
use App\Http\Controllers\Community\LatestCommentsController;
use App\Http\Controllers\User\FavouritesController;
use App\Http\Controllers\User\ProfileController;
use App\Http\Controllers\Web\GalleryController;

// ── AVATARS ───────────────────────────────────────────────────────────────────
Route::get('/avatar/{id}/{name?}', [AvatarController::class, 'show'])->where('id', '\d+')->name('legacy.avatar');

// ── ARTWORK (legacy comment URL) ──────────────────────────────────────────────
//Route::match(['get','post'], '/art/{id}/comment', [ArtController::class, 'show'])->where('id', '\d+');

// ── CATEGORIES / SECTIONS ─────────────────────────────────────────────────────
Route::redirect('/sections', '/categories', 301)->name('sections');
Route::redirect('/browse-categories', '/categories', 301)->name('browse.categories');

// Legacy mixed-case category URL patterns:
//   /Skins/BrowserBob/210
//   /Skins/BrowserBob/sdsdsdsd/210
Route::get('/{group}/{slug}/{id}', CategoryRedirectController::class)
    ->where('group', '(?i:skins|wallpapers|photography|other|digital-art|members)')
    ->where('slug', '[^/]+(?:/[^/]+)*')
    ->whereNumber('id')
    ->name('legacy.category.short');

// Legacy category URL pattern: /category/group/slug/id
Route::get('/category/{group}/{slug?}/{id?}', CategoryRedirectController::class)->name('legacy.category');

// ── BROWSE / FEATURED / DAILY ─────────────────────────────────────────────────
//Route::get('/browse', [BrowseGalleryController::class, 'browse'])->name('legacy.browse');
Route::get('/browse',           fn () => redirect('/explore', 301))->name('legacy.browse');
Route::get('/featured-artworks', fn () => redirect('/featured', 301))->name('legacy.featured_artworks');
Route::get('/daily-uploads',    fn () => redirect()->route('uploads.daily', request()->query(), 301))->name('legacy.daily_uploads');

// ── CHAT ──────────────────────────────────────────────────────────────────────
Route::match(['get', 'post'], '/chat', fn () => redirect('/messages', 301));
Route::match(['get', 'post'], '/community/chat', fn () => redirect('/messages', 301))->name('community.chat');

// ── UPLOADS / COMMENTS / DOWNLOADS (SEO alias pages) ─────────────────────────
Route::get('/latest',           fn () => redirect('/uploads/latest', 301))->name('legacy.latest');

Route::get('/authors/top',      fn () => redirect('/creators/top', 301))->name('authors.top');
Route::get('/latest-artworks',  fn () => redirect()->route('discover.fresh', request()->query(), 301))->name('legacy.latest_artworks');

Route::get('/latest-comments',  [LatestCommentsController::class, 'index'])->name('legacy.latest_comments');
Route::get('/comments/latest',  [LatestCommentsController::class, 'index'])->name('comments.latest');

Route::get('/today-in-history',  fn () => redirect()->route('discover.on-this-day', request()->query(), 301))->name('legacy.today_in_history');

Route::get('/today-downloads',  fn () => redirect()->route('downloads.today', request()->query(), 301))->name('legacy.today_downloads');

Route::get('/monthly-commentators', fn () => redirect()->route('comments.monthly', request()->query(), 301))->name('legacy.monthly_commentators');

Route::get('/members',           fn () => redirect()->route('creators.top', request()->query(), 301))->name('legacy.members');
Route::get('/top-favourites',    fn () => redirect()->route('discover.top-rated', request()->query(), 301))->name('legacy.top_favourites');

// ── REDIRECTS: top-authors, interviews, apply, bug-report ────────────────────
Route::get('/top-authors', fn () => redirect('/creators/top', 301))->name('legacy.top_authors');
Route::get('/interviews',  fn () => redirect('/stories', 301))->name('legacy.interviews');
Route::get('/apply',       fn () => redirect('/contact', 301))->name('legacy.apply.redirect');

// ── BUDDIES / MYBUDDIES ───────────────────────────────────────────────────────
Route::middleware('auth')->get('/mybuddies.php', fn () => redirect()->route('dashboard.following', [], 301))->name('legacy.mybuddies.php');
Route::middleware('auth')->get('/mybuddies',     fn () => redirect()->route('dashboard.following', [], 301))->name('legacy.mybuddies');
Route::middleware('auth')->delete('/mybuddies/{id}', fn () => redirect()->route('dashboard.following'))->name('legacy.mybuddies.delete');

Route::middleware('auth')->get('/buddies.php', fn () => redirect()->route('dashboard.followers', [], 301))->name('legacy.buddies.php');
Route::middleware('auth')->get('/buddies',     fn () => redirect()->route('dashboard.followers', [], 301))->name('legacy.buddies');

// ── FAVOURITES / GALLERY ──────────────────────────────────────────────────────
Route::get('/favourites/{id?}/{username?}', [FavouritesController::class, 'index'])->name('legacy.favourites');
Route::post('/favourites/{userId}/delete/{artworkId}', [FavouritesController::class, 'destroy'])->name('legacy.favourites.delete');

Route::middleware('ensure.onboarding.complete')
    ->get('/gallery/{id}/{username?}', [GalleryController::class, 'show'])
    ->name('legacy.gallery'); // We need to fix to a new gallery

// ── PROFILE (legacy URL patterns) ────────────────────────────────────────────
Route::get('/user/{username}', [ProfileController::class, 'legacyByUsername'])->where('username', '[A-Za-z0-9_-]{3,20}')->name('legacy.user.profile');
Route::get('/profile/{id}/{username?}', [ProfileController::class, 'legacyById'])->where('id', '\d+')->name('legacy.profile.id');
Route::get('/profile/{username}', [ProfileController::class, 'legacyByUsername'])->where('username', '[A-Za-z0-9_-]{3,20}')->name('legacy.profile');

// Keep legacy `/user` as a permanent redirect to the canonical dashboard path.
Route::middleware(['auth'])->match(['get','post'], '/user', function () {return redirect()->route('dashboard.profile', [], 301);})->name('legacy.user.redirect');

// ── COMMENTS / STATISTICS ─────────────────────────────────────────────────────
Route::middleware('auth')->get('/recieved-comments', fn () => redirect()->route('dashboard.comments.received', request()->query(), 301))->name('legacy.received_comments');
Route::middleware('auth')->get('/received-comments', fn () => redirect()->route('dashboard.comments.received', request()->query(), 301))->name('legacy.received_comments.corrected');

Route::middleware(['auth'])->group(function () {
    Route::get('/statistics', fn () => redirect()->route('leaderboard', [], 301))->name('legacy.statistics');
});
