import { z } from 'zod'
import { pikkuFunc } from '#pikku/pikku-types.gen.js'
import { listTemplates } from '../../templates/index.js'

export const ListResolutionTemplatesOutput = z.array(
  z.object({
    key: z.string(),
    titleDe: z.string(),
    titleEn: z.string(),
    descriptionDe: z.string(),
    fields: z.array(
      z.object({
        key: z.string(),
        labelDe: z.string(),
        labelEn: z.string(),
        type: z.enum(['text', 'textarea', 'number', 'date', 'currency']),
        required: z.boolean().optional(),
      }),
    ),
  }),
)

export const listResolutionTemplates = pikkuFunc({
  expose: true,
  description: 'List all available resolution templates.',
  output: ListResolutionTemplatesOutput,
  func: async () => {
    return listTemplates().map((t) => ({
      key: t.key,
      titleDe: t.titleDe,
      titleEn: t.titleEn,
      descriptionDe: t.descriptionDe,
      fields: t.fields,
    })) as any
  },
})
