import {
  SettingsFormCard,
  type SettingFieldDef,
} from '@/components/admin/shared/settings-form-card';
import { DocumensoTestButton } from '@/components/admin/documenso/documenso-test-button';
import { PageHeader } from '@/components/shared/page-header';

const API_FIELDS: SettingFieldDef[] = [
  {
    key: 'documenso_api_url_override',
    label: 'API URL override',
    description: 'Optional. Falls back to DOCUMENSO_API_URL env when blank.',
    type: 'string',
    placeholder: 'https://documenso.example.com',
    defaultValue: '',
  },
  {
    key: 'documenso_api_key_override',
    label: 'API key override',
    description: 'Optional. Falls back to DOCUMENSO_API_KEY env when blank. Stored in plain text.',
    type: 'password',
    defaultValue: '',
  },
];

const EOI_FIELDS: SettingFieldDef[] = [
  {
    key: 'documenso_eoi_template_id',
    label: 'EOI Documenso template ID',
    description: 'Numeric template ID used by the Documenso EOI pathway.',
    type: 'string',
    placeholder: '12345',
    defaultValue: '',
  },
  {
    key: 'eoi_default_pathway',
    label: 'Default EOI pathway',
    description:
      'Which pathway is used when an EOI is generated without an explicit choice. Documenso = signed via Documenso, In-app = filled locally with pdf-lib.',
    type: 'select',
    options: [
      { value: 'documenso-template', label: 'Documenso template' },
      { value: 'inapp', label: 'In-app (pdf-lib)' },
    ],
    defaultValue: 'documenso-template',
  },
];

export default function DocumensoSettingsPage() {
  return (
    <div className="space-y-6">
      <PageHeader
        title="Documenso & EOI"
        description="API credentials and default EOI generation pathway. Use the test-connection button to verify a saved configuration before relying on it."
      />

      <SettingsFormCard
        title="Documenso API"
        description="Per-port API credentials. Leave blank to use the global env defaults."
        fields={API_FIELDS}
        extra={<DocumensoTestButton />}
      />

      <SettingsFormCard
        title="EOI generation"
        description="Default pathway and template used when an interest's EOI is generated."
        fields={EOI_FIELDS}
      />
    </div>
  );
}
