import { any, z } from 'zod'; // Question types export enum QuestionType { TITLE = 'title', SIMPLE = 'simple', MULTIPLE_CHOICE = 'multiple_choice', SINGLE_CHOICE = 'single_choice', SELECT = 'select' } // Base question schema const baseQuestionSchema = z.object({ id: z.string(), type: z.nativeEnum(QuestionType), required: z.boolean().default(false), position: z.number() }); // Title question export const titleQuestionSchema = baseQuestionSchema.extend({ type: z.literal(QuestionType.TITLE), content: z.string() }); // Simple question (text input) export const simpleQuestionSchema = baseQuestionSchema.extend({ type: z.literal(QuestionType.SIMPLE), question: z.string() }); // Option-based questions const optionSchema = z.object({ id: z.string(), text: z.string() }); // Multiple choice question export const multipleChoiceQuestionSchema = baseQuestionSchema.extend({ type: z.literal(QuestionType.MULTIPLE_CHOICE), question: z.string(), options: z.array(optionSchema) }); // Single choice question export const singleChoiceQuestionSchema = baseQuestionSchema.extend({ type: z.literal(QuestionType.SINGLE_CHOICE), question: z.string(), options: z.array(optionSchema) }); // Select question export const selectQuestionSchema = baseQuestionSchema.extend({ type: z.literal(QuestionType.SELECT), question: z.string(), options: z.array(optionSchema) }); // Union of all question types export const questionSchema = z.discriminatedUnion('type', [ titleQuestionSchema, simpleQuestionSchema, multipleChoiceQuestionSchema, singleChoiceQuestionSchema, selectQuestionSchema ]); // Survey schema export const surveySchema = z.object({ id: z.number().optional(), title: z.string(), description: z.string(), targetAudience: z.string(), closingDate: z.date().optional(), published: z.boolean(), questions: z.array(questionSchema), created_at: z.string().optional(), updated_at: z.string().optional(), }); // Survey Answer request schema export const surveyAnswerMutateSchema = z.object({ surveyId: z.string(), answers: z.array( z.object({ questionId: z.string(), value: z.union([z.string(), z.array(z.string())]) }) ), }); // Types based on schemas export type Question = z.infer; export type TitleQuestion = z.infer; export type SimpleQuestion = z.infer; export type MultipleChoiceQuestion = z.infer; export type SingleChoiceQuestion = z.infer; export type SelectQuestion = z.infer; export type Survey = z.infer; export type SurveyResponse = z.infer; export const surveyApiSchema = z.object({ id: z.number().optional(), title: z.string(), description: z.string(), targetAudience: z.string(), closingDate: z.string().transform((str) => str ? new Date(str) : null), published: z.boolean(), questions: z.array(questionSchema), created_at: z.string().optional(), updated_at: z.string().optional(), }); export type SurveyTable = z.infer; // Api response schemas export const surveysApiResponseSchema = z.object({ message: z.string(), data: z.array(surveyApiSchema), meta: z.object({ page: z.number(), limit: z.number(), totalCount: z.number(), totalPages: z.number(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), nextPage: z.number().nullable(), previousPage: z.number().nullable(), }), }); // Survey response schema export const surveyAnswerApiResponseSchema = z.object({ id: z.number().optional(), surveyId: z.number(), userId: z.number(), answers: z.array( z.object({ questionId: z.string(), value: z.union([z.string(), z.array(z.string())]) }) ), created_at: z.string().optional(), updated_at: z.string().optional(), }); // Response schemas for the API create, update export const SurveyAnswerMutate = z.object({ message: z.string(), data: surveyAnswerApiResponseSchema, }); // Response schemas for the API create, update export const suveryApiMutationResponseSchema = z.object({ message: z.string(), data: surveyApiSchema, }); // Response schemas for the API create, update export const suveryResponseDeleteSchema = z.object({ message: z.string(), }); // Schema For User Survey Answer export const surveyAnswerQuerySchema = z.object({ surverId: z.number(), title: z.string(), description: z.string(), created_at: z.string().transform((str) => str ? new Date(str) : null), closingDate: z.string().transform((str) => str ? new Date(str) : null), targetAudience: z.string(), user_id: z.number().nullable(), }); export const surveyAnswerQuerySchema2 = z.object({ surveys: z.any(), answers_surveys: z.any().optional() // surverId: z.number(), // title: z.string(), // description: z.string(), // created_at: z.string().transform((str) => str ? new Date(str) : null), // closingDate: z.string().transform((str) => str ? new Date(str) : null), // targetAudience: z.string(), // user_id: z.number().nullable(), }); // Api response schemas export const surveysApiResponseForUserSchema = z.object({ message: z.string(), data: z.array(surveyAnswerQuerySchema2), meta: z.object({ page: z.number(), limit: z.number(), totalCount: z.number(), totalPages: z.number(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), nextPage: z.number().nullable(), previousPage: z.number().nullable(), }), }); export type SurveyAnswerForUser = z.infer;