import { z } from 'zod';
import { ActionStepKind, ActionStepCompletionSource } from '@prisma/client';

export const createActionCampaignSchema = z.object({
  slug: z.string().min(1).max(80).regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/).optional(),
  title: z.string().min(1).max(200),
  description: z.string().max(5000).nullable().optional(),
  rewardText: z.string().max(500).nullable().optional(),
  isActive: z.boolean().optional().default(false),
  startsAt: z.coerce.date().nullable().optional(),
  endsAt: z.coerce.date().nullable().optional(),
  minStepsForReward: z.number().int().positive().nullable().optional(),
});

export const updateActionCampaignSchema = z.object({
  slug: z.string().min(1).max(80).regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/).optional(),
  title: z.string().min(1).max(200).optional(),
  description: z.string().max(5000).nullable().optional(),
  rewardText: z.string().max(500).nullable().optional(),
  isActive: z.boolean().optional(),
  startsAt: z.coerce.date().nullable().optional(),
  endsAt: z.coerce.date().nullable().optional(),
  minStepsForReward: z.number().int().positive().nullable().optional(),
});

export const createActionStepSchema = z.object({
  kind: z.nativeEnum(ActionStepKind),
  label: z.string().min(1).max(200),
  description: z.string().max(2000).nullable().optional(),
  targetId: z.string().max(200).nullable().optional(),
  targetUrl: z.string().url().max(500).nullable().optional(),
  autoComplete: z.boolean().optional().default(true),
});

export const updateActionStepSchema = z.object({
  kind: z.nativeEnum(ActionStepKind).optional(),
  label: z.string().min(1).max(200).optional(),
  description: z.string().max(2000).nullable().optional(),
  targetId: z.string().max(200).nullable().optional(),
  targetUrl: z.string().url().max(500).nullable().optional(),
  autoComplete: z.boolean().optional(),
});

export const reorderStepsSchema = z.object({
  stepIds: z.array(z.string().min(1)).min(1),
});

export const markCompleteSchema = z.object({
  source: z.nativeEnum(ActionStepCompletionSource).optional().default(ActionStepCompletionSource.SELF_REPORTED),
});

export type CreateActionCampaignInput = z.infer<typeof createActionCampaignSchema>;
export type UpdateActionCampaignInput = z.infer<typeof updateActionCampaignSchema>;
export type CreateActionStepInput = z.infer<typeof createActionStepSchema>;
export type UpdateActionStepInput = z.infer<typeof updateActionStepSchema>;
export type ReorderStepsInput = z.infer<typeof reorderStepsSchema>;
export type MarkCompleteInput = z.infer<typeof markCompleteSchema>;
