<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class WebserviceDocsController extends Controller
{
    public function index()
    {
        return view('admin.webservice-docs.index');
    }

    public function swaggerYaml(): BinaryFileResponse
    {
        $path = base_path('docs/swagger.yaml');

        if (!file_exists($path)) {
            $fallbackPath = dirname(base_path()) . DIRECTORY_SEPARATOR . 'docs' . DIRECTORY_SEPARATOR . 'swagger.yaml';
            if (file_exists($fallbackPath)) {
                $path = $fallbackPath;
            }
        }

        abort_unless(file_exists($path), 404, 'Swagger file not found');

        return response()->file($path, [
            'Content-Type' => 'text/yaml; charset=utf-8',
            'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
        ]);
    }
}
