import { z } from 'zod';
import { PetitionStatus, CampaignModerationStatus } from '@prisma/client';

export const createPetitionSchema = z.object({
  title: z.string().min(1, 'Title is required').max(200),
  description: z.string().max(5000).optional(),
  signatureGoal: z.number().int().positive().nullable().optional(),
  showProgress: z.boolean().optional().default(true),
  showSignatureCount: z.boolean().optional().default(true),
  showSignerNames: z.boolean().optional().default(true),
  signatureCountOffset: z.number().int().min(0).optional().default(0),
  requireName: z.boolean().optional().default(true),
  requireEmail: z.boolean().optional().default(true),
  requirePostalCode: z.boolean().optional().default(false),
  requirePhone: z.boolean().optional().default(false),
  allowComment: z.boolean().optional().default(true),
  commentLabel: z.string().max(200).nullable().optional(),
  requireEmailConfirmation: z.boolean().optional().default(false),
  confirmationEmailSubject: z.string().max(200).nullable().optional(),
  confirmationEmailBody: z.string().max(5000).nullable().optional(),
  coverPhoto: z.string().url().max(500).nullable().optional(),
  coverVideoId: z.number().int().positive().nullable().optional(),
  callToAction: z.string().max(2000).nullable().optional(),
  thankYouMessage: z.string().max(2000).nullable().optional(),
  highlightPetition: z.boolean().optional().default(false),
  linkedCampaignId: z.string().nullable().optional(),
  status: z.nativeEnum(PetitionStatus).optional().default(PetitionStatus.DRAFT),
});

export const updatePetitionSchema = z.object({
  title: z.string().min(1).max(200).optional(),
  description: z.string().max(5000).nullable().optional(),
  signatureGoal: z.number().int().positive().nullable().optional(),
  showProgress: z.boolean().optional(),
  showSignatureCount: z.boolean().optional(),
  showSignerNames: z.boolean().optional(),
  signatureCountOffset: z.number().int().min(0).optional(),
  requireName: z.boolean().optional(),
  requireEmail: z.boolean().optional(),
  requirePostalCode: z.boolean().optional(),
  requirePhone: z.boolean().optional(),
  allowComment: z.boolean().optional(),
  commentLabel: z.string().max(200).nullable().optional(),
  requireEmailConfirmation: z.boolean().optional(),
  confirmationEmailSubject: z.string().max(200).nullable().optional(),
  confirmationEmailBody: z.string().max(5000).nullable().optional(),
  coverPhoto: z.string().url().max(500).nullable().optional(),
  coverVideoId: z.number().int().positive().nullable().optional(),
  callToAction: z.string().max(2000).nullable().optional(),
  thankYouMessage: z.string().max(2000).nullable().optional(),
  highlightPetition: z.boolean().optional(),
  linkedCampaignId: z.string().nullable().optional(),
  status: z.nativeEnum(PetitionStatus).optional(),
});

export const listPetitionsSchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().positive().max(100).default(20),
  search: z.string().optional(),
  status: z.nativeEnum(PetitionStatus).optional(),
});

export const signPetitionSchema = z.object({
  signerName: z.string().max(200).optional(),
  signerEmail: z.string().email().max(255).optional(),
  signerPostalCode: z.string().max(20).optional(),
  signerPhone: z.string().max(30).optional(),
  signerComment: z.string().max(2000).optional(),
  isAnonymous: z.boolean().optional().default(false),
});

export const listSignaturesSchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().positive().max(100).default(20),
  search: z.string().optional(),
  status: z.string().optional(),
});

export const updateSignatureStatusSchema = z.object({
  status: z.enum(['VERIFIED', 'REJECTED']),
});

export const moderatePetitionSchema = z.object({
  action: z.enum(['approve', 'reject', 'request_changes']),
  reason: z.string().max(2000).optional(),
  notes: z.string().max(2000).optional(),
});

export const listModerationQueueSchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().positive().max(100).default(20),
  search: z.string().optional(),
  moderationStatus: z.nativeEnum(CampaignModerationStatus).optional(),
});

export type CreatePetitionInput = z.infer<typeof createPetitionSchema>;
export type UpdatePetitionInput = z.infer<typeof updatePetitionSchema>;
export type ListPetitionsInput = z.infer<typeof listPetitionsSchema>;
export type SignPetitionInput = z.infer<typeof signPetitionSchema>;
export type ListSignaturesInput = z.infer<typeof listSignaturesSchema>;
export type UpdateSignatureStatusInput = z.infer<typeof updateSignatureStatusSchema>;
export type ModeratePetitionInput = z.infer<typeof moderatePetitionSchema>;
export type ListModerationQueueInput = z.infer<typeof listModerationQueueSchema>;
