import { z } from 'zod';

export const registerSchema = z.object({
  name: z.string().min(2).max(80),
  email: z.string().email(),
  password: z.string().min(8).max(100),
});

export const loginSchema = z.object({
  email: z.string().email(),
  password: z.string().min(1),
});

export const googleAuthSchema = z.object({
  credential: z.string().min(1),
});

const otpCode = z.string().regex(/^\d{6}$/, 'Enter the 6-digit code');

export const verifyEmailSchema = z.object({
  email: z.string().email(),
  code: otpCode,
});

export const resendOtpSchema = z.object({
  email: z.string().email(),
  purpose: z.enum(['EMAIL_VERIFY', 'PASSWORD_RESET']).default('EMAIL_VERIFY'),
});

export const forgotPasswordSchema = z.object({
  email: z.string().email(),
});

export const resetPasswordSchema = z.object({
  email: z.string().email(),
  code: otpCode,
  password: z.string().min(8).max(100),
});

// ----- Self-service account management (logged-in user editing their own profile) -----
export const updateProfileSchema = z.object({
  name: z.string().min(2).max(80),
});

export const changePasswordSchema = z.object({
  currentPassword: z.string().min(1),
  newPassword: z.string().min(8).max(100),
});

export const changeEmailRequestSchema = z.object({
  newEmail: z.string().email(),
});

export const changeEmailVerifySchema = z.object({
  newEmail: z.string().email(),
  code: otpCode,
});

export const adminCreateUserSchema = z.object({
  name: z.string().min(2).max(80),
  email: z.string().email(),
  password: z.string().min(8).max(100),
  role: z.enum(['USER', 'ADMIN']).default('USER'),
});

export const adminUpdateUserSchema = z.object({
  name: z.string().min(2).max(80).optional(),
  password: z.string().min(8).max(100).optional(),
  role: z.enum(['USER', 'ADMIN']).optional(),
  isActive: z.boolean().optional(),
});

export const vendorSchema = z.object({
  name: z.string().min(2).max(80),
  description: z.string().max(2000).optional().nullable(),
  logoUrl: z.string().url().optional().nullable().or(z.literal('')),
});

export const courseSchema = z.object({
  vendorId: z.string().uuid(),
  title: z.string().min(2).max(150),
  code: z.string().max(60).optional().nullable(),
  description: z.string().max(4000).optional().nullable(),
  priceCents: z.number().int().min(0).default(0),
  currency: z.string().length(3).default('usd'),
  isFree: z.boolean().default(false),
  published: z.boolean().default(true),
  freeQuestionLimit: z.number().int().min(0).max(100).default(5),
  examMinutes: z.number().int().min(0).max(600).default(0),
});

const optionSchema = z.object({
  text: z.string().min(1).max(1000),
  isCorrect: z.boolean(),
});

export const questionSchema = z.object({
  courseId: z.string().uuid(),
  text: z.string().min(3).max(4000),
  explanation: z.string().max(4000).optional().nullable(),
  type: z.enum(['SINGLE', 'MULTIPLE']).default('SINGLE'),
  category: z.enum(['PRACTICE', 'EXAM']).default('PRACTICE'),
  options: z.array(optionSchema).min(2).max(8),
});

export const userQuestionSchema = questionSchema; // same shape, status forced to PENDING server-side

export const submitAnswerSchema = z.object({
  questionId: z.string().uuid(),
  selectedOptionIds: z.array(z.string().uuid()).min(0),
});

export const startAttemptSchema = z.object({
  courseId: z.string().uuid(),
  mode: z.enum(['PRACTICE', 'EXAM']),
});
