import type { ReactNode } from 'react';
import { TranslatedField } from '@/components/admin/translated-field';
import type { FormErrors, LocaleOption, Translations } from '@/types';

/** The Markdown the editors need for a text page. */
const MARKDOWN_HINTS: { syntax: string; meaning: string }[] = [
    { syntax: '## Başlık', meaning: 'bölüm başlığı' },
    { syntax: '### Alt başlık', meaning: 'alt başlık' },
    { syntax: '**kalın**', meaning: 'kalın yazı' },
    { syntax: '*italik*', meaning: 'italik yazı' },
    { syntax: '- madde', meaning: 'madde işaretli liste' },
    { syntax: '1. madde', meaning: 'numaralı liste' },
    { syntax: '[metin](https://adres)', meaning: 'bağlantı' },
];

type Props = {
    /** Field path without the locale: "body" reads errors["body.tr"]. */
    name: string;
    label: string;
    /** The language being edited (see useEditingLocale). */
    locale: LocaleOption;
    value: Translations;
    onChange: (value: Translations) => void;
    errors: FormErrors;
    /** Required in the default language (other languages stay optional). */
    required?: boolean;
    /** Height of the textarea, in lines. */
    rows?: number;
    /** Said after the general help, e.g. what the headings are used for. */
    hint?: ReactNode;
};

/**
 * A translatable text written in Markdown (legal pages, blog posts): a
 * tall monospaced textarea, followed by a short guide to the syntax.
 * Meant to sit directly in a FormSection, which spaces the two apart.
 */
export function MarkdownField({
    name,
    label,
    locale,
    value,
    onChange,
    errors,
    required = false,
    rows = 22,
    hint,
}: Props) {
    return (
        <>
            <TranslatedField
                name={name}
                label={label}
                locale={locale}
                value={value}
                onChange={onChange}
                errors={errors}
                rows={rows}
                required={required}
                className="[&_textarea]:min-h-96 [&_textarea]:font-mono [&_textarea]:leading-relaxed"
                hint={
                    <>
                        Metin Markdown ile yazılır; paragrafları boş bir satırla
                        ayırın. HTML etiketleri güvenlik nedeniyle yok sayılır.
                        {hint && <> {hint}</>}
                    </>
                }
            />

            <div className="rounded-lg border bg-muted/40 p-4">
                <p className="text-sm font-medium">Kısa Markdown rehberi</p>
                <dl className="mt-3 grid grid-cols-1 gap-x-6 gap-y-2 text-sm sm:grid-cols-2">
                    {MARKDOWN_HINTS.map((item) => (
                        <div
                            key={item.syntax}
                            className="flex flex-wrap items-baseline gap-2"
                        >
                            <dt>
                                <code className="rounded bg-background px-1.5 py-0.5 font-mono text-xs">
                                    {item.syntax}
                                </code>
                            </dt>
                            <dd className="text-muted-foreground">
                                {item.meaning}
                            </dd>
                        </div>
                    ))}
                </dl>
            </div>
        </>
    );
}
