import { z } from 'zod'

export const createEventSchema = z.object({
  name: z.string().min(1).max(200),
  description: z.string().max(2000).optional(),
  eventDate: z.string().datetime().optional(),
  location: z.string().max(500).optional(),
  goalAmount: z.number().int().positive().optional(), // pence
  currency: z.string().default('GBP'),
})

export const createQrSourceSchema = z.object({
  label: z.string().min(1).max(100),
  volunteerName: z.string().max(100).optional(),
  tableName: z.string().max(100).optional(),
})

export const createPledgeSchema = z.object({
  amountPence: z.number().int().min(100).max(100000000), // £1 to £1M
  rail: z.enum(['bank', 'gocardless', 'card', 'fpx']),
  donorName: z.string().max(200).optional().default(''),
  donorEmail: z.string().max(200).optional().default(''),
  donorPhone: z.string().max(20).optional().default(''),
  giftAid: z.boolean().default(false),
  eventId: z.string(),
  qrSourceId: z.string().nullable().optional(),
}).transform((data) => ({
  ...data,
  donorEmail: data.donorEmail && data.donorEmail.includes('@') ? data.donorEmail : undefined,
  donorPhone: data.donorPhone && data.donorPhone.length >= 10 ? data.donorPhone : undefined,
  donorName: data.donorName || undefined,
  qrSourceId: data.qrSourceId || undefined,
}))

export const importBankStatementSchema = z.object({
  columnMapping: z.object({
    dateCol: z.string(),
    descriptionCol: z.string(),
    amountCol: z.string().optional(),
    creditCol: z.string().optional(),
    referenceCol: z.string().optional(),
  }),
})

export const updatePledgeStatusSchema = z.object({
  status: z.enum(['new', 'initiated', 'paid', 'overdue', 'cancelled']),
  notes: z.string().max(1000).optional(),
})
