<?php

namespace App\Services\Docs;

use App\Services\ApplicationUrlService;

/**
 * legacy parity: {@see \App\Controllers\DocsController::index} and
 * {@see \App\Controllers\View\DocsController::swagger} (multi-spec Swagger UI payload).
 */
class DocsCatalogService
{
    public function __construct(
        private ApplicationUrlService $urls,
    ) {
    }

    /**
     * Hub metadata for the docs landing route (replaces Blade `docs/index`).
     *
     * @return array<string, mixed>
     */
    public function indexPayload(): array
    {
        return [
            'title' => (string) config('docs.index.title', 'API documentation'),
            'description' => (string) config('docs.index.description', ''),
            'links' => [
                'swagger_catalog' => $this->urls->docsSwaggerCatalogUrl(),
                'merged_openapi_json' => $this->urls->docsSwaggerMergedJsonUrl(),
                'docs_spa' => config('docs.client_url'),
                'public_bootstrap' => $this->urls->apiDocsPublicBootstrapUrl(),
                'admin_bootstrap' => $this->urls->apiDocsAdminBootstrapUrl(),
            ],
        ];
    }

    /**
     * Multi-spec dropdown payload (legacy `View\DocsController::swagger` / `swagger_ui` view).
     *
     * @return array{specs: list<array{name: string, url: string}>, merged_openapi_url: string}
     */
    public function swaggerSpecsPayload(): array
    {
        $specs = [];
        foreach ((array) config('docs.swagger_specs', []) as $row) {
            if (! is_array($row)) {
                continue;
            }
            $name = trim((string) ($row['name'] ?? ''));
            $path = trim((string) ($row['path'] ?? ''));
            if ($name === '' || $path === '') {
                continue;
            }
            $specs[] = [
                'name' => $name,
                'url' => $this->urls->absolutePublicPathUrl($path),
            ];
        }

        return [
            'specs' => $specs,
            'merged_openapi_url' => $this->urls->docsSwaggerMergedJsonUrl(),
        ];
    }
}
