<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\BlogController;
use App\Settings\GeneralSettings;
use App\Domain\Seo\SeoPresenter;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;

require __DIR__.'/legacy.php';

Route::get('/docs/api/openapi.yaml', function () {
    $file = base_path('docs/api/openapi.yaml');
    abort_unless(is_file($file), 404);

    return response()->file($file, ['Content-Type' => 'application/yaml; charset=UTF-8']);
});

Route::get('/posts/{slug}', [BlogController::class, 'bySlug'])->where('slug', '[A-Za-z0-9\\-]+');

// Dev/local attachments disk public URL (ATTACHMENTS_DRIVER=local)
Route::get('/attachments-local/{path}', function (string $path) {
    $disk = Storage::disk(config('larablog.attachments_disk', 'attachments'));
    abort_unless($disk->exists($path), 404);

    return $disk->response($path);
})->where('path', '.*');

// Fallback when public/themes/{theme} symlink is missing.
Route::get('/themes/{theme}/{path}', function (string $theme, string $path) {
    $base = base_path('themes/'.$theme.'/assets');
    $baseReal = realpath($base);
    $file = realpath($base.'/'.$path);
    if (! $baseReal || ! $file || ! str_starts_with($file, $baseReal) || ! is_file($file)) {
        abort(404);
    }

    $mime = match (strtolower(pathinfo($file, PATHINFO_EXTENSION))) {
        'css' => 'text/css; charset=UTF-8',
        'js' => 'application/javascript; charset=UTF-8',
        'mjs' => 'application/javascript; charset=UTF-8',
        'png' => 'image/png',
        'jpg', 'jpeg' => 'image/jpeg',
        'gif' => 'image/gif',
        'svg' => 'image/svg+xml',
        'webp' => 'image/webp',
        'woff' => 'font/woff',
        'woff2' => 'font/woff2',
        'ttf' => 'font/ttf',
        'map' => 'application/json',
        default => mime_content_type($file) ?: 'application/octet-stream',
    };

    return response()->file($file, [
        'Content-Type' => $mime,
        'Cache-Control' => 'public, max-age=3600',
    ]);
})->where('path', '.*');

// /index.php is often rewritten to /; handle legacy query actions here and on /
Route::get('/index.php', function () {
    $action = request()->query('action');

    return match ($action) {
        'tags' => request()->filled('item')
            ? redirect('/tag/'.rawurlencode((string) request()->query('item')), 301)
            : app(BlogController::class)->tagsList(),
        'login' => app(AuthController::class)->showLogin(app(GeneralSettings::class)),
        'reg' => app(AuthController::class)->showRegister(app(GeneralSettings::class)),
        'profile' => app(AuthController::class)->showProfile(app(GeneralSettings::class)),
        'links' => app(BlogController::class)->links(),
        'comments' => app(BlogController::class)->comments(),
        'tagslist' => app(BlogController::class)->tagsList(),
        'search' => app(BlogController::class)->search(request()),
        'show' => app(BlogController::class)->show(request(), (int) request()->query('id'), app(GeneralSettings::class), app(SeoPresenter::class)),
        default => app(BlogController::class)->index(request(), app(GeneralSettings::class), app(SeoPresenter::class)),
    };
});
