<?php

use App\Http\Controllers\ContactController;
use App\Http\Controllers\DownloadsController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\PlaylistController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\StreamController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\Api\AdminApiController;
use App\Http\Controllers\Api\DownloadApiController;
use App\Http\Controllers\Api\HentaiApiController;
use App\Http\Controllers\Api\StreamApiController;
use App\Http\Controllers\Api\UserApiController;
use Illuminate\Support\Facades\Route;

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

Route::get('/', [HomeController::class, 'index'])->name('home.index');
Route::get('/stats', [HomeController::class, 'stats'])->name('home.stats');
Route::get('/banned', [HomeController::class, 'banned'])->name('home.banned');

// API Endpoint
Route::get('/v1/hentai-list', [HentaiApiController::class, 'index'])->name('api.hentai.index');

// Stream Page
Route::get('/hentai/{title}', [StreamController::class, 'index'])->name('hentai.index');
Route::post('/player/api', [StreamApiController::class, 'getStream'])->name('hentai.player');

// Search
Route::get('/search', [HomeController::class, 'search'])->name('hentai.search');
Route::post('/search', [HomeController::class, 'searchRedirect'])->name('hentai.searchredirect');

// Contact Form
Route::get('/contact', [ContactController::class, 'index'])->name('contact.index');
Route::post('/contact', [ContactController::class, 'store'])->name('contact.store');

// Public Playlistts
Route::get('/playlists', [PlaylistController::class, 'index'])->name('playlist.index');
Route::get('/playlist/{playlist_id}', [PlaylistController::class, 'show'])->name('playlist.show');

// Captcha Reload
Route::get('/reload-captcha', [ContactController::class, 'reloadCaptcha']);

// Download
Route::post('/get-download', [DownloadApiController::class, 'getDownload']);

Route::post('/update-language', [HomeController::class, 'updateLanguage'])->name('update.language');

// User Routes
Route::middleware('auth')->group(function () {
    Route::get('/user/profile', [ProfileController::class, 'index'])->name('profile.show');
    Route::get('/user/comments', [ProfileController::class, 'comments'])->name('profile.comments');
    Route::get('/user/likes', [ProfileController::class, 'likes'])->name('profile.likes');
    Route::get('/user/watched', [ProfileController::class, 'watched'])->name('user.watched');

    // Notifications
    Route::get('/user/notifications', [App\Http\Controllers\NotificationController::class, 'index'])->name('profile.notifications');
    Route::delete('/user/notifications', [App\Http\Controllers\NotificationController::class, 'delete'])->name('profile.notifications.delete');

    Route::get('/user/settings', [ProfileController::class, 'settings'])->name('profile.settings');
    Route::post('/user/settings', [ProfileController::class, 'saveSettings'])->name('profile.settings.save');
    Route::get('/user/blacklist', [UserApiController::class, 'getBlacklist'])->name('profile.blacklist');
    Route::post('/user/blacklist', [ProfileController::class, 'saveBlacklist'])->name('profile.blacklist.save');

    Route::post('/user/delete', [UserController::class, 'delete'])->name('profile.delete');

    // Playlist Routes for User Page
    Route::get('/user/playlists', [PlaylistController::class, 'playlists'])->name('profile.playlists');
    Route::get('/user/playlist/{playlist_id}', [PlaylistController::class, 'showPlaylist'])->name('profile.playlist.show');
    Route::post('/create-playlist', [PlaylistController::class, 'createPlaylist'])->name('profile.playlists.create');
    Route::delete('/user/playlist/{playlist_id}', [PlaylistController::class, 'deletePlaylist'])->name('profile.playlist.delete');
    Route::post('/user/playlist-episode', [PlaylistController::class, 'deleteEpisodeFromPlaylist'])->name('playlist.delete.episode');

    // Playlist Routes for Modals on Stream Page
    Route::post('/hentai/add-to-playlist', [PlaylistController::class, 'addPlaylistApi'])->name('hentai.playlists.add');
    Route::post('/hentai/create-playlist', [PlaylistController::class, 'createPlaylistApi'])->name('hentai.playlists.create');

    // Download Page
    Route::get('/download-search', [HomeController::class, 'downloadSearch'])->name('download.search');
});

Route::get('/user/{username}', [UserController::class, 'index'])->name('user.index');

/*
|---------------------------------------------------------------------------------
| Admin Pages
|---------------------------------------------------------------------------------
*/
Route::group(['middleware' => ['auth', 'auth.admin']], function () {
    // Site alerts
    Route::get('/admin/alert', [App\Http\Controllers\Admin\AlertController::class, 'index'])->name('admin.alert.index');
    Route::post('/admin/alert', [App\Http\Controllers\Admin\AlertController::class, 'store'])->name('admin.alert.create');
    Route::delete('/admin/alert/{alert_id}', [App\Http\Controllers\Admin\AlertController::class, 'delete'])->name('admin.alert.delete');

    // Users
    Route::get('/admin/users', [App\Http\Controllers\Admin\UserController::class, 'index'])->name('admin.user.index');
    Route::post('/admin/users', [App\Http\Controllers\Admin\UserController::class, 'update'])->name('admin.user.update');

    // Contact page overview
    Route::get('/admin/contact', [App\Http\Controllers\Admin\ContactController::class, 'index'])->name('admin.contact.index');
    Route::delete('/admin/contact/{contact_id}', [App\Http\Controllers\Admin\ContactController::class, 'delete'])->name('admin.contact.delete');

    // Site background settings
    Route::get('/admin/background', [App\Http\Controllers\Admin\SiteBackgroundController::class, 'index'])->name('admin.background.index');
    Route::post('/admin/background', [App\Http\Controllers\Admin\SiteBackgroundController::class, 'create'])->name('admin.background.create');
    Route::put('/admin/background', [App\Http\Controllers\Admin\SiteBackgroundController::class, 'update'])->name('admin.background.update');
    Route::delete('/admin/background', [App\Http\Controllers\Admin\SiteBackgroundController::class, 'delete'])->name('admin.background.delete');

    // Release
    Route::get('/admin/release', [App\Http\Controllers\Admin\ReleaseController::class, 'index'])->name('admin.upload.index');
    Route::post('/admin/release/upload', [App\Http\Controllers\Admin\ReleaseController::class, 'store'])->name('admin.upload');

    // Episode
    Route::post('/admin/episode/upload', [App\Http\Controllers\Admin\EpisodeController::class, 'store'])->name('admin.upload.episode');
    Route::post('/admin/episode/edit', [App\Http\Controllers\Admin\EpisodeController::class, 'update'])->name('admin.edit');

    // Get Tags used for Upload Form
    Route::get('/admin/tags', [AdminApiController::class, 'getTags'])->name('admin.tags');
    Route::get('/admin/studios', [AdminApiController::class, 'getStudios'])->name('admin.studios');

    // Get Tags for editing Episode
    Route::get('/admin/tags/{episode_id}', [AdminApiController::class, 'getEpisodeTags'])->name('admin.tags.episode');
    Route::get('/admin/studio/{episode_id}', [AdminApiController::class, 'getEpisodeStudio'])->name('admin.studio.episode');

    // Subtitles
    Route::get('/admin/subtitles/{episode_id}', [AdminApiController::class, 'getSubtitles'])->name('admin.subtitles');
    Route::post('/admin/add-new-subtitle', [App\Http\Controllers\Admin\SubtitleController::class, 'store'])->name('admin.add.new.subtitle');
    Route::post('/admin/update-subtitles', [App\Http\Controllers\Admin\SubtitleController::class, 'update'])->name('admin.update.subtitles');
});
