import { z } from 'zod';
import { EmailTemplateCategory } from '@prisma/client';

// Variable type enum
export const EmailTemplateVariableTypeSchema = z.enum(['TEXT', 'VIDEO']);
export type EmailTemplateVariableTypeType = z.infer<typeof EmailTemplateVariableTypeSchema>;

// Variable schema
export const emailTemplateVariableSchema = z.object({
  key: z.string().regex(/^[A-Z_]+$/, 'Variable key must be uppercase letters and underscores'),
  label: z.string().min(1).max(100),
  description: z.string().max(500).optional(),
  type: EmailTemplateVariableTypeSchema.default('TEXT'),
  videoId: z.number().int().positive().optional(),
  isRequired: z.boolean().default(true),
  isConditional: z.boolean().default(false),
  sampleValue: z.string().max(1000).optional(),
  sortOrder: z.number().int().min(0).default(0),
}).refine(
  (data) => {
    // VIDEO type must have videoId
    if (data.type === 'VIDEO' && !data.videoId) {
      return false;
    }
    return true;
  },
  { message: 'VIDEO variables must have a videoId', path: ['videoId'] }
);

// List templates
export const listEmailTemplatesSchema = z.object({
  page: z.coerce.number().int().min(1).default(1),
  limit: z.coerce.number().int().min(1).max(100).default(20),
  search: z.string().max(200).optional(),
  category: z.nativeEnum(EmailTemplateCategory).optional(),
  isActive: z.coerce.boolean().optional(),
});

export type ListEmailTemplatesDto = z.infer<typeof listEmailTemplatesSchema>;

// Create template
export const createEmailTemplateSchema = z.object({
  key: z.string().regex(/^[a-z0-9-]+$/, 'Template key must be lowercase alphanumeric with hyphens').min(1).max(100),
  name: z.string().min(1).max(200),
  description: z.string().max(1000).optional(),
  category: z.nativeEnum(EmailTemplateCategory),
  subjectLine: z.string().min(1).max(500),
  htmlContent: z.string().min(1).max(100000, 'HTML content exceeds 100KB limit'),
  textContent: z.string().min(1).max(50000, 'Text content exceeds 50KB limit'),
  isActive: z.boolean().default(true),
  variables: z.array(emailTemplateVariableSchema).optional(),
});

export type CreateEmailTemplateDto = z.infer<typeof createEmailTemplateSchema>;

// Update template
export const updateEmailTemplateSchema = z.object({
  name: z.string().min(1).max(200).optional(),
  description: z.string().max(1000).optional().nullable(),
  category: z.nativeEnum(EmailTemplateCategory).optional(),
  subjectLine: z.string().min(1).max(500).optional(),
  htmlContent: z.string().min(1).max(100000, 'HTML content exceeds 100KB limit').optional(),
  textContent: z.string().min(1).max(50000, 'Text content exceeds 50KB limit').optional(),
  isActive: z.boolean().optional(),
  variables: z.array(emailTemplateVariableSchema).optional(),
});

export type UpdateEmailTemplateDto = z.infer<typeof updateEmailTemplateSchema>;

// Rollback to version
export const rollbackToVersionSchema = z.object({
  versionNumber: z.number().int().min(1),
  changeNotes: z.string().max(500).optional(),
});

export type RollbackToVersionDto = z.infer<typeof rollbackToVersionSchema>;

// Validate template
export const validateTemplateSchema = z.object({
  htmlContent: z.string().min(1).max(100000),
  textContent: z.string().min(1).max(50000),
  subjectLine: z.string().min(1).max(500).optional(),
});

export type ValidateTemplateDto = z.infer<typeof validateTemplateSchema>;

// Send test email
export const sendTestEmailSchema = z.object({
  recipientEmail: z.string().email('Invalid email address').max(255),
  testData: z.record(z.string(), z.string()),
});

export type SendTestEmailDto = z.infer<typeof sendTestEmailSchema>;
