<?php

use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\BookmarksController;
use App\Http\Controllers\GuestbookController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\MusicController;
use App\Http\Controllers\RoscoLekoController;
use GuzzleHttp\Client as GuzzleClient;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', [HomeController::class, 'show']);
Route::get('/bookmarks', [BookmarksController::class, 'show'])
    ->name('bookmarks');
Route::get('/music', [MusicController::class, 'show']);
Route::get('/pandamonium', [RoscoLekoController::class, 'show']);
Route::get('/guestbook', [GuestbookController::class, 'show']);
Route::post('/guestbook', [GuestbookController::class, 'addEntry'])
    ->name('guestbook')
    ->middleware('validator')
    ->middleware('rate_limit');

Route::middleware('auth')->group(function () {
    Route::get('/bookmarks/bookmark/new', [BookmarksController::class, 'createBookmark'])
        ->name('bookmarks.create-bookmark');
    Route::post('/bookmarks/bookmark/new', [BookmarksController::class, 'addBookmark']);
    Route::delete('/bookmarks/bookmark/{id}', [BookmarksController::class, 'destroyBookmark'])
        ->name('bookmarks.destroy-bookmark');
    Route::get('/bookmarks/category/new', [BookmarksController::class, 'createCategory'])
        ->name('bookmarks.create-category');
    Route::post('/bookmarks/category/new', [BookmarksController::class, 'addCategory']);
    Route::delete('/bookmarks/category/{id}', [BookmarksController::class, 'destroyCategory'])
        ->name('bookmarks.destroy-category');

    Route::delete('/guestbook/{id}', [GuestbookController::class, 'destroy'])
        ->name('guestbook.destroy');
    Route::get('/guestbook/{id}/flag', [GuestbookController::class, 'flag'])
        ->name('guestbook.flag');
});

/* Authentication */
Route::prefix('auth')->group(function() {
    Route::get('login', [LoginController::class, 'showLoginForm']);
    Route::post('login', [LoginController::class, 'login'])->name('login');
    Route::get('logout', [LoginController::class, 'logout'])->name('logout');
});

/* Legacy Proxies */
Route::prefix('proxy')->group(function () {
    Route::get('/wah/{image}', function (string $image) {
        $client = new GuzzleClient();
        $response = $client->request('GET', 'https://api.tinyfox.dev/hourly/wahs/'.$image);

        return response($response->getBody())
            ->header('Content-Type', $response->getHeader('Content-Type'));
    });

    Route::get('/lastfm/{image}', function (string $image) {
        $client = new GuzzleClient();
        $response = $client->request('GET', 'https://lastfm.freetls.fastly.net/i/u/174s/'.$image);

        return response($response->getBody())
            ->header('Content-Type', $response->getHeader('Content-Type'));
    });
});


