import { z } from 'zod';

export const phoneRegex = /^[6-9]\d{9}$/; // Indian mobile, 10 digits

export const genderSchema = z.enum(['male', 'female']);
export type Gender = z.infer<typeof genderSchema>;

export const createUserSchema = z.object({
  username: z.string().min(3).max(50).trim().regex(/^[a-zA-Z0-9._-]+$/),
  fullName: z.string().min(2).max(150).trim(),
  phone: z.string().regex(phoneRegex, 'Invalid Indian mobile number'),
  gender: genderSchema,
  homeLocalityId: z.number().int().positive().nullable().optional(),
  isCaptain: z.boolean().default(false),
  isVolunteer: z.boolean().default(true),
  // Captain provides initial password; volunteer can change after first login
  initialPassword: z.string().min(8).max(200),
});
export type CreateUserInput = z.infer<typeof createUserSchema>;

export const updateUserSchema = z.object({
  fullName: z.string().min(2).max(150).optional(),
  phone: z.string().regex(phoneRegex).optional(),
  gender: genderSchema.optional(),
  homeLocalityId: z.number().int().positive().nullable().optional(),
  isCaptain: z.boolean().optional(),
  isVolunteer: z.boolean().optional(),
  isActive: z.boolean().optional(),
});

export const listUsersQuerySchema = z.object({
  q: z.string().max(100).optional(),
  localityId: z.coerce.number().int().positive().optional(),
  role: z.enum(['captain', 'volunteer', 'all']).default('all'),
  isActive: z.coerce.boolean().optional(),
  page: z.coerce.number().int().min(1).default(1),
  pageSize: z.coerce.number().int().min(1).max(500).default(50),
});
