/**
 * Register DTO
 *
 * Validation schema for user registration
 */

import { z } from 'zod';

/**
 * Password requirements:
 * - Minimum 8 characters
 * - At least one uppercase letter
 * - At least one lowercase letter
 * - At least one number
 * - At least one special character
 */
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}$/;

/**
 * Username requirements:
 * - 3-20 characters
 * - Alphanumeric and underscores only
 * - Must start with a letter
 */
const usernameRegex = /^[a-zA-Z][a-zA-Z0-9_]{2,19}$/;

export const registerSchema = z.object({
  email: z
    .string()
    .min(1, 'Email is required')
    .email('Invalid email format')
    .toLowerCase()
    .trim(),
  username: z
    .string()
    .min(3, 'Username must be at least 3 characters')
    .max(20, 'Username must not exceed 20 characters')
    .regex(usernameRegex, 'Username must start with a letter and contain only letters, numbers, and underscores')
    .trim(),
  password: z
    .string()
    .min(8, 'Password must be at least 8 characters')
    .max(128, 'Password must not exceed 128 characters')
    .regex(passwordRegex, 'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character'),
});

export type RegisterDto = z.infer<typeof registerSchema>;
