import { z } from 'zod';

export type SurveyTable = z.infer<typeof user>;
export type CreateUser = z.infer<typeof createUser>;
export type UpdateUser = z.infer<typeof updateUser>;

export const user = z.object({
  id: z.number().optional(),
  username: z.string(),
  email: z.string(),
  fullname: z.string(),
  phone: z.string().nullable(),
  isActive: z.boolean(),
  role: z.string(),
  state: z.string().optional().nullable(),
  municipality: z.string().optional().nullable(),
  parish: z.string().optional().nullable(),
});

export const createUser = z.object({
  id: z.number().optional(),
  username: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }),
  password: z.string().min(8, { message: "Debe de tener 8 o más caracteres" }),
  email: z.string().email({ message: "Correo no válido" }),
  fullname: z.string(),
  phone: z.string(),
  confirmPassword: z.string(),
  role: z.number()
})
.refine((data) => data.password === data.confirmPassword, {
  message: 'La contraseña no coincide',
  path: ['confirmPassword'],
})

export const updateUser = z.object({
  id: z.number(),
  username: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }).or(z.literal('')),
  password: z.string().min(6, { message: "Debe de tener 6 o más caracteres" }).or(z.literal('')),
  email: z.string().email({ message: "Correo no válido" }).or(z.literal('')),
  fullname: z.string().optional(),
  phone: z.string().optional(),
  role: z.number().optional(),
  isActive: z.boolean().optional(),
  state: z.number().optional().nullable(),
  municipality: z.number().optional().nullable(),
  parish: z.number().optional().nullable(),
})

export const surveysApiResponseSchema = z.object({
  message: z.string(),
  data: z.array(user),
  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 const UsersMutate = z.object({
  message: z.string(),
  data: user,
})