import { z } from 'zod';

const emailSchema = z.string().email().max(320);
const passwordSchema = z.string().min(12).max(128);

export const registerRequestSchema = z.object({
  email: emailSchema,
  password: passwordSchema,
  username: z.string().min(3).max(32).optional(),
  displayName: z.string().min(1).max(64).optional(),
});

export const loginRequestSchema = z.object({
  email: emailSchema,
  password: z.string().min(1).max(128),
});

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

export const forgotPasswordRequestSchema = z.object({
  email: emailSchema,
});

export const resetPasswordRequestSchema = z.object({
  token: z.string().min(1),
  newPassword: passwordSchema,
});

export const twoFactorConfirmRequestSchema = z.object({
  code: z.string().min(6).max(16),
});

export const twoFactorVerifyRequestSchema = z.object({
  challengeId: z.string().uuid(),
  code: z.string().min(6).max(16),
});

export const twoFactorDisableRequestSchema = z.object({
  password: z.string().min(1).max(128),
  code: z.string().min(6).max(16),
});

export const authUserSchema = z.object({
  id: z.string().uuid(),
  email: z.string().email(),
  username: z.string(),
  displayName: z.string().nullable(),
  emailVerified: z.boolean(),
  roles: z.array(z.string()),
});

export const loginSuccessResponseSchema = z.object({
  user: authUserSchema,
});

export const loginTwoFactorRequiredResponseSchema = z.object({
  twoFactorRequired: z.literal(true),
  challengeId: z.string().uuid(),
});

export const loginResponseSchema = z.union([
  loginSuccessResponseSchema,
  loginTwoFactorRequiredResponseSchema,
]);

export const twoFactorSetupResponseSchema = z.object({
  secret: z.string(),
  qrUrl: z.string(),
});

export const sessionSummarySchema = z.object({
  id: z.string().uuid(),
  ipAddress: z.string().nullable(),
  userAgent: z.string().nullable(),
  createdAt: z.string().datetime(),
  expiresAt: z.string().datetime(),
  current: z.boolean(),
});

export const sessionListResponseSchema = z.object({
  sessions: z.array(sessionSummarySchema),
});

export const messageResponseSchema = z.object({
  message: z.string(),
});

export type RegisterRequest = z.infer<typeof registerRequestSchema>;
export type LoginRequest = z.infer<typeof loginRequestSchema>;
export type VerifyEmailRequest = z.infer<typeof verifyEmailRequestSchema>;
export type ForgotPasswordRequest = z.infer<typeof forgotPasswordRequestSchema>;
export type ResetPasswordRequest = z.infer<typeof resetPasswordRequestSchema>;
export type TwoFactorConfirmRequest = z.infer<typeof twoFactorConfirmRequestSchema>;
export type TwoFactorVerifyRequest = z.infer<typeof twoFactorVerifyRequestSchema>;
export type TwoFactorDisableRequest = z.infer<typeof twoFactorDisableRequestSchema>;
export type AuthUser = z.infer<typeof authUserSchema>;
export type LoginResponse = z.infer<typeof loginResponseSchema>;
export type TwoFactorSetupResponse = z.infer<typeof twoFactorSetupResponseSchema>;
export type SessionSummary = z.infer<typeof sessionSummarySchema>;
export type SessionListResponse = z.infer<typeof sessionListResponseSchema>;
export type MessageResponse = z.infer<typeof messageResponseSchema>;
