import type { CollectionConfig } from 'payload';

export const FormSubmissions: CollectionConfig = {
  slug: 'form-submissions',
  admin: {
    useAsTitle: 'name',
    defaultColumns: ['name', 'email', 'type', 'createdAt'],
    description: 'Captured leads from Contact and Product Quote forms.',
  },
  access: {
    // Only Admins can view and delete leads via dashboard.
    read: ({ req: { user } }) => Boolean(user) || process.env.NODE_ENV === 'development',
    update: ({ req: { user } }) => Boolean(user) || process.env.NODE_ENV === 'development',
    delete: ({ req: { user } }) => Boolean(user) || process.env.NODE_ENV === 'development',
    // Next.js server actions handle secure inserts natively. No public client create access.
    create: () => false,
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
      admin: {
        readOnly: true,
      },
    },
    {
      name: 'email',
      type: 'email',
      required: true,
      admin: {
        readOnly: true,
      },
    },
    {
      name: 'type',
      type: 'select',
      options: [
        { label: 'General Contact', value: 'contact' },
        { label: 'Product Quote', value: 'product_quote' },
      ],
      required: true,
      admin: {
        position: 'sidebar',
        readOnly: true,
      },
    },
    {
      name: 'productName',
      type: 'text',
      admin: {
        position: 'sidebar',
        readOnly: true,
        condition: (data) => data.type === 'product_quote',
        description: 'The specific KLZ product the user requested a quote for.',
      },
    },
    {
      name: 'message',
      type: 'textarea',
      required: true,
      admin: {
        readOnly: true,
      },
    },
  ],
};
