import type { CollectionConfig } from 'payload'
import { anyone, isAdmin } from '../access/roles'
import { loc } from '../fields/localized'

const tier = (label: string) => ({
  name: label, type: 'group' as const, fields: [
    { name: 'rate', type: 'number', required: true },
    { name: 'per', type: 'text', ...loc, admin: { description: 'e.g. / dan, / nedelja, / mesec' } },
    { name: 'popular', type: 'checkbox', defaultValue: false },
    { name: 'save', type: 'text', admin: { description: 'Optional badge, e.g. −18%' } },
  ],
})

// Pricing is money => ADMIN-owned. One doc per market (keyed by `market` code).
export const Pricing: CollectionConfig = {
  slug: 'pricing',
  admin: { useAsTitle: 'market', group: 'Markets & Pricing' },
  access: { read: anyone, create: isAdmin, update: isAdmin, delete: isAdmin },
  versions: { drafts: true },
  fields: [
    {
      name: 'market', type: 'select', required: true, unique: true,
      options: [{ label: 'RS', value: 'RS' }, { label: 'BA', value: 'BA' }],
    },
    tier('daily'),
    tier('weekly'),
    tier('monthly'),
  ],
}
