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'),
  paymentMode: z.enum(['self', 'external']).default('self'),
  externalUrl: z.string().url().max(1000).optional(),
  externalPlatform: z.enum(['launchgood', 'enthuse', 'justgiving', 'gofundme', 'other']).optional(),
  zakatEligible: z.boolean().default(false),
})

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']),
  donorName: z.string().max(200).optional().default(''),
  donorEmail: z.string().max(200).optional().default(''),
  donorPhone: z.string().max(20).optional().default(''),

  // Home address (required for HMRC Gift Aid)
  donorAddressLine1: z.string().max(300).optional().default(''),
  donorPostcode: z.string().max(10).optional().default(''),

  // Gift Aid
  giftAid: z.boolean().default(false),
  isZakat: z.boolean().default(false),

  // Communication consent (GDPR/PECR)
  emailOptIn: z.boolean().default(false),
  whatsappOptIn: z.boolean().default(false),

  // Consent audit trail
  consentMeta: z.object({
    giftAid: z.object({
      declared: z.boolean(),
      declarationText: z.string(),
      declaredAt: z.string(),
    }).optional(),
    email: z.object({
      granted: z.boolean(),
      consentText: z.string(),
      grantedAt: z.string(),
    }).optional(),
    whatsapp: z.object({
      granted: z.boolean(),
      consentText: z.string(),
      grantedAt: z.string(),
    }).optional(),
    ip: z.string().optional(),
    userAgent: z.string().optional(),
    consentVersion: z.string().default('v1'),
  }).optional(),

  eventId: z.string(),
  qrSourceId: z.string().nullable().optional(),
  // Payment scheduling
  scheduleMode: z.enum(['now', 'date', 'installments']).default('now'),
  dueDate: z.string().optional(),
  installmentCount: z.number().int().min(2).max(12).optional(),
  installmentDates: z.array(z.string()).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,
  donorAddressLine1: data.donorAddressLine1 || undefined,
  donorPostcode: data.donorPostcode || 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(),
})
