<?php

use Illuminate\Support\Facades\Route;

if (config('swagger.enabled')) {
    $docsPath = trim((string) config('swagger.path', 'api/documentation'), '/');

    Route::get($docsPath, function () {
        return response()
            ->view('swagger', [
                'title' => config('swagger.title'),
                'specUrl' => url(trim(config('swagger.path'), '/').'/openapi.yaml'),
            ])
            ->header('Cache-Control', 'no-store, private');
    })->name('swagger.ui');

    Route::get($docsPath.'/openapi.yaml', function () {
        $path = config('swagger.spec_path');
        abort_unless(is_string($path) && is_file($path), 404);

        return response()->file($path, [
            'Content-Type' => 'application/yaml; charset=UTF-8',
            'Cache-Control' => 'no-store, private',
            'X-Content-Type-Options' => 'nosniff',
        ]);
    })->name('swagger.spec');
}
