import { z } from 'zod';

const listFormsQuerySchema = z.object({
  status: z.enum(["active", "inactive", "archived"]).optional(),
  limit: z.coerce.number().min(1).max(100).optional().default(20),
  offset: z.coerce.number().min(0).optional().default(0)
});
const createFormSchema = z.object({
  name: z.string().min(1, "Name is required"),
  slug: z.string().min(1, "Slug is required"),
  description: z.string().optional(),
  schema: z.string().min(1, "Schema is required"),
  successMessage: z.string().optional(),
  redirectUrl: z.string().url().optional().or(z.literal("")),
  status: z.enum(["active", "inactive", "archived"]).optional().default("active")
});
const updateFormSchema = z.object({
  name: z.string().min(1, "Name is required").optional(),
  slug: z.string().min(1, "Slug is required").optional(),
  description: z.string().optional(),
  schema: z.string().min(1, "Schema is required").optional(),
  successMessage: z.string().optional(),
  redirectUrl: z.string().url().optional().or(z.literal("")),
  status: z.enum(["active", "inactive", "archived"]).optional()
});
const formResponseSchema = z.object({
  id: z.string(),
  name: z.string(),
  slug: z.string(),
  description: z.string().nullable().optional(),
  schema: z.string(),
  successMessage: z.string().nullable().optional(),
  redirectUrl: z.string().nullable().optional(),
  status: z.string(),
  createdBy: z.string().nullable().optional(),
  createdAt: z.string(),
  updatedAt: z.string()
});
z.object({
  items: z.array(formResponseSchema),
  total: z.number(),
  limit: z.number(),
  offset: z.number()
});
const listSubmissionsQuerySchema = z.object({
  limit: z.coerce.number().min(1).max(100).optional().default(20),
  offset: z.coerce.number().min(0).optional().default(0)
});
z.object({
  // Use passthrough object for dynamic form data validation
  data: z.object({}).passthrough()
});
const formSubmissionResponseSchema = z.object({
  id: z.string(),
  formId: z.string(),
  data: z.string(),
  submittedAt: z.string(),
  submittedBy: z.string().nullable().optional(),
  ipAddress: z.string().nullable().optional(),
  userAgent: z.string().nullable().optional()
});
const formSubmissionWithDataResponseSchema = formSubmissionResponseSchema.extend({
  // Use passthrough object for dynamic parsed data
  parsedData: z.object({}).passthrough(),
  form: formResponseSchema.optional()
});
z.object({
  items: z.array(formSubmissionWithDataResponseSchema),
  total: z.number(),
  limit: z.number(),
  offset: z.number()
});

export { createFormSchema, formResponseSchema, formSubmissionResponseSchema, formSubmissionWithDataResponseSchema, listFormsQuerySchema, listSubmissionsQuerySchema, updateFormSchema };
