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

export const createCampaignSchema = z.object({
  title: z.string().min(1, 'Title is required').max(200),
  description: z.string().max(2000).optional(),
  emailSubject: z.string().min(1, 'Email subject is required').max(200),
  emailBody: z.string().min(1, 'Email body is required').max(10000),
  callToAction: z.string().max(500).optional(),
  status: z.nativeEnum(CampaignStatus).optional().default(CampaignStatus.DRAFT),
  targetGovernmentLevels: z.array(z.nativeEnum(GovernmentLevel)).optional().default([]),
  allowSmtpEmail: z.boolean().optional().default(true),
  allowMailtoLink: z.boolean().optional().default(true),
  collectUserInfo: z.boolean().optional().default(true),
  showEmailCount: z.boolean().optional().default(true),
  showCallCount: z.boolean().optional().default(true),
  allowEmailEditing: z.boolean().optional().default(false),
  allowCustomRecipients: z.boolean().optional().default(false),
  showResponseWall: z.boolean().optional().default(false),
  highlightCampaign: z.boolean().optional().default(false),
  coverPhoto: z.string().url().max(500).optional(),
  coverVideoId: z.number().int().positive().nullable().optional(),
});

export const updateCampaignSchema = z.object({
  title: z.string().min(1).max(200).optional(),
  description: z.string().max(2000).nullable().optional(),
  emailSubject: z.string().min(1).max(200).optional(),
  emailBody: z.string().min(1).max(10000).optional(),
  callToAction: z.string().max(500).nullable().optional(),
  status: z.nativeEnum(CampaignStatus).optional(),
  targetGovernmentLevels: z.array(z.nativeEnum(GovernmentLevel)).optional(),
  allowSmtpEmail: z.boolean().optional(),
  allowMailtoLink: z.boolean().optional(),
  collectUserInfo: z.boolean().optional(),
  showEmailCount: z.boolean().optional(),
  showCallCount: z.boolean().optional(),
  allowEmailEditing: z.boolean().optional(),
  allowCustomRecipients: z.boolean().optional(),
  showResponseWall: z.boolean().optional(),
  highlightCampaign: z.boolean().optional(),
  coverPhoto: z.string().url().max(500).nullable().optional(),
  coverVideoId: z.number().int().positive().nullable().optional(),
});

export const listCampaignsSchema = 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(CampaignStatus).optional(),
});

export const campaignIdSchema = z.object({
  id: z.string().min(1),
});

// User-submitted campaign (restricted fields)
export const createUserCampaignSchema = z.object({
  title: z.string().min(3, 'Title must be at least 3 characters').max(200),
  description: z.string().max(2000).optional(),
  emailSubject: z.string().min(3, 'Email subject is required').max(200),
  emailBody: z.string().min(10, 'Email body must be at least 10 characters').max(5000),
  callToAction: z.string().max(500).optional(),
  targetGovernmentLevels: z.array(z.nativeEnum(GovernmentLevel)).min(1, 'Select at least one government level'),
});

// Update own user campaign (same restricted fields)
export const updateUserCampaignSchema = createUserCampaignSchema.partial();

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

// Moderation queue filters
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 CreateCampaignInput = z.infer<typeof createCampaignSchema>;
export type UpdateCampaignInput = z.infer<typeof updateCampaignSchema>;
export type ListCampaignsInput = z.infer<typeof listCampaignsSchema>;
export type CreateUserCampaignInput = z.infer<typeof createUserCampaignSchema>;
export type UpdateUserCampaignInput = z.infer<typeof updateUserCampaignSchema>;
export type ModerateCampaignInput = z.infer<typeof moderateCampaignSchema>;
export type ListModerationQueueInput = z.infer<typeof listModerationQueueSchema>;
