import { fields } from '@keystatic/core';
import { createSEOField } from './seo.ts';
import { createCoverField } from './cover.ts';

export const createBaseArticleFields = (
  parentCollection: string = 'articles',
) => ({
  title: fields.slug({ name: { label: 'Title' } }),
  subtitle: fields.text({ label: 'Subtitle' }),
  summary: fields.text({
    label: 'Summary',
    multiline: true,
    validation: { isRequired: true },
  }),
  cover: createCoverField(),
  publishDate: fields.datetime({
    label: 'Publish Date',
    validation: { isRequired: true },
    defaultValue: { kind: 'now' },
  }),
  updateDate: fields.datetime({
    label: 'Update Date',
  }),
  status: fields.select({
    label: 'Status',
    defaultValue: 'draft',
    options: [
      { label: 'Draft', value: 'draft' },
      { label: 'Published', value: 'published' },
      { label: 'Archived', value: 'archived' },
    ],
  }),
  isFeatured: fields.checkbox({
    label: 'Featured',
    description: 'Show on Homepage',
  }),
  parent: fields.relationship({
    label: 'Parent',
    collection: parentCollection,
  }),
  tags: fields.array(fields.text({ label: 'Tag' }), {
    label: 'Tags',
    itemLabel: (props) => props.value,
  }),
  relatedArticles: fields.array(
    fields.relationship({ label: 'Article', collection: 'articles' }),
    {
      label: 'Related Articles',
      itemLabel: (props) => props.value ?? 'Select article',
    },
  ),
  seo: createSEOField(),
});
