/**
 * @ai-summary Request validation schemas for admin user management API
 * @ai-context Uses Zod for runtime validation and type safety
 */

import { z } from 'zod';

// Subscription tier enum
export const subscriptionTierSchema = z.enum(['free', 'pro', 'enterprise']);

// Query params for listing users
export const listUsersQuerySchema = z.object({
  page: z.coerce.number().min(1).default(1),
  pageSize: z.coerce.number().min(1).max(100).default(20),
  search: z.string().optional(),
  tier: subscriptionTierSchema.optional(),
  status: z.enum(['active', 'deactivated', 'all']).default('all'),
  sortBy: z.enum(['email', 'createdAt', 'displayName', 'subscriptionTier']).default('createdAt'),
  sortOrder: z.enum(['asc', 'desc']).default('desc'),
});

// Path param for user auth0Sub
export const userAuth0SubSchema = z.object({
  auth0Sub: z.string().min(1, 'auth0Sub is required'),
});

// Body for updating subscription tier
export const updateTierSchema = z.object({
  subscriptionTier: subscriptionTierSchema,
});

// Body for deactivating a user
export const deactivateUserSchema = z.object({
  reason: z.string().max(500).optional(),
});

// Body for updating user profile (admin edit)
export const updateProfileSchema = z.object({
  email: z.string().email('Invalid email address').optional(),
  displayName: z.string().max(100, 'Display name must be 100 characters or less').optional(),
}).refine(
  (data) => data.email !== undefined || data.displayName !== undefined,
  { message: 'At least one field (email or displayName) must be provided' }
);

// Body for promoting user to admin
export const promoteToAdminSchema = z.object({
  role: z.enum(['admin', 'super_admin']).default('admin'),
});

// Type exports
export type ListUsersQueryInput = z.infer<typeof listUsersQuerySchema>;
export type UserAuth0SubInput = z.infer<typeof userAuth0SubSchema>;
export type UpdateTierInput = z.infer<typeof updateTierSchema>;
export type DeactivateUserInput = z.infer<typeof deactivateUserSchema>;
export type UpdateProfileInput = z.infer<typeof updateProfileSchema>;
export type PromoteToAdminInput = z.infer<typeof promoteToAdminSchema>;
