'use client';

import { useEffect, useMemo, useState } from 'react';
import { buildOAuthExamples } from '@/lib/oauth-examples';
import { fetchPublicSettingsClient } from '@/lib/api';
import { resolveOAuthApiBase } from '@/lib/oauth-url';
import { CodeExampleTabs } from '@/components/code-example-tabs';

export function OAuthCodeTabs() {
  const [apiBase, setApiBase] = useState('http://localhost:3000');

  useEffect(() => {
    void fetchPublicSettingsClient()
      .then((settings) => setApiBase(resolveOAuthApiBase(settings)))
      .catch(() => undefined);
  }, []);

  const examples = useMemo(() => buildOAuthExamples(apiBase), [apiBase]);

  return (
    <div className="space-y-3">
      <p className="text-sm text-zinc-500 dark:text-zinc-400">
        Базовый URL API (issuer): <code className="rounded bg-zinc-100 px-1.5 py-0.5 dark:bg-zinc-800">{apiBase}</code>
        {' — '}
        берётся из настройки <strong>PUBLIC_API_URL</strong> или <strong>Домен IdP</strong> в админ-панели.
      </p>
      <CodeExampleTabs examples={examples} />
    </div>
  );
}
