<?php

namespace App\Services\Docs;

use App\Models\User;
use App\Services\ApplicationUrlService;

class ApiDocsService
{
    public function __construct(
        private ApplicationUrlService $urls,
    ) {
    }

    /**
     * Bootstrap payload for the documentation SPA (replaces legacy swagger_ui / swagger_ui_public views).
     *
     * @return array<string, mixed>
     */
    public function bootstrap(?User $user, bool $public): array
    {
        return [
            'variant' => $public ? 'public' : 'full',
            'title' => $public
                ? 'Al Rahma API — Public Docs'
                : 'Al Rahma Sunday School — API Docs',
            'openapi_url' => $this->urls->docsSwaggerMergedJsonUrl(false),
            'try_it_out_enabled' => ! $public,
            'persist_authorization' => ! $public,
            'show_authorize_button' => ! $public,
            'welcome_name' => $public ? null : $this->welcomeName($user),
        ];
    }

    private function welcomeName(?User $user): ?string
    {
        if (! $user) {
            return null;
        }

        $full = trim(($user->firstname ?? '').' '.($user->lastname ?? ''));

        return $full !== '' ? $full : ($user->email ?: 'Admin');
    }
}
