import { z } from "zod";

export const envSchema = z.object({
  NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
  APP_URL: z.string().url().default("http://localhost:3000"),
  DATABASE_URL: z.string().min(1),
  REDIS_URL: z.string().url().default("redis://localhost:6379"),
  NEXTAUTH_SECRET: z.string().min(32),
  NEXTAUTH_URL: z.string().url(),
  EVOLUTION_API_URL: z.string().url(),
  EVOLUTION_API_KEY: z.string().min(1),
  EVOLUTION_INSTANCE_ID: z.string().min(1),
  GOOGLE_CALENDAR_ID: z.string().min(1),
  GOOGLE_CLIENT_ID: z.string().min(1),
  GOOGLE_CLIENT_SECRET: z.string().min(1),
  GOOGLE_REDIRECT_URI: z.string().url(),
  WHATSAPP_PHONE_NUMBER: z.string().min(10),
  SMTP_HOST: z.string().min(1),
  SMTP_PORT: z.coerce.number().min(1),
  SMTP_USER: z.string().min(1),
  SMTP_PASS: z.string().min(1),
  SMTP_FROM_NAME: z.string().min(1),
  SMTP_FROM_EMAIL: z.string().email(),
  ADMIN_EMAIL: z.string().email(),
});

export type Env = z.infer<typeof envSchema>;

export const patientSchema = z.object({
  phone: z.string().min(10, "Teléfono inválido"),
  name: z.string().min(2, "El nombre debe tener al menos 2 caracteres"),
  email: z.string().email("Email inválido").optional().or(z.literal("")),
  birthdate: z.coerce.date({
    errorMap: () => ({ message: "Fecha de nacimiento inválida" }),
  }),
});

export type PatientInput = z.infer<typeof patientSchema>;

export const appointmentSchema = z.object({
  patientPhone: z.string().min(10, "Teléfono inválido"),
  date: z.coerce.date({
    errorMap: () => ({ message: "Fecha inválida" }),
  }),
  isCrisis: z.boolean().default(false),
});

export type AppointmentInput = z.infer<typeof appointmentSchema>;

export const clinicalNoteSchema = z.object({
  patientId: z.string().min(1, "ID de paciente requerido"),
  content: z.string().min(10, "La nota debe tener al menos 10 caracteres"),
});

export type ClinicalNoteInput = z.infer<typeof clinicalNoteSchema>;

export const contactSchema = z.object({
  name: z.string().min(2, "El nombre debe tener al menos 2 caracteres"),
  phone: z.string().min(10, "Teléfono inválido"),
  message: z.string().min(10, "El mensaje debe tener al menos 10 caracteres"),
});

export type ContactInput = z.infer<typeof contactSchema>;
