/**
 * Authentication API Types
 */

// Base API response with optional error code
export interface BaseApiResponse {
  success: boolean
  message?: string
  code?: string // Error code at root level like E001, E011, etc.
  error?: {
    code?: string // Error code in nested error object
    message?: string
  }
}

// User profile
export interface UserProfile {
  nickname: string
  avatar: string
}

// User information
export interface User {
  _id: string
  userId: number
  username: string
  email: string
  emailVerified: boolean
  loginMethods: string[]
  roles: string[]
  profile: UserProfile
  status: string
  referralCode: string
  referredBy?: string
  referredByCode?: string
  lastLogin: string
  createdAt: string
  updatedAt: string
}

// Tokens
export interface Tokens {
  accessToken: string
  refreshToken: string
  accessTokenExpiry: string
  refreshTokenExpiry: string
}

// Pre-login request
export interface PreLoginRequest {
  email: string
  password: string
  language?: string
}

// Pre-login response
export interface PreLoginResponse extends BaseApiResponse {}

// Resend code request
export interface ResendCodeRequest {
  email: string
  language?: string
}

// Resend code response
export interface ResendCodeResponse extends BaseApiResponse {}

// Verify login request
export interface VerifyLoginRequest {
  email: string
  code: string
}

// Verify login response
export interface VerifyLoginResponse extends BaseApiResponse {
  data?: {
    user: User
    tokens: Tokens
  }
}

// Google login request
export interface GoogleLoginRequest {
  idToken: string
  referralCode?: string
}

// Google login response
export interface GoogleLoginResponse extends BaseApiResponse {
  data?: {
    user: User
    tokens: Tokens
  }
}

// Login step type
export type LoginStep = "credentials" | "verification"

// Register step type
export type RegisterStep = "credentials" | "verification"

// Pre-register request
export interface PreRegisterRequest {
  username: string
  password: string
  email: string
  nickname?: string
  referralCode?: string
  language?: string
}

// Pre-register response
export interface PreRegisterResponse extends BaseApiResponse {
  data?: {
    email: string
    expiresIn: string
  }
}

// Resend register code request
export interface ResendRegisterCodeRequest {
  email: string
  language?: string
}

// Resend register code response
export interface ResendRegisterCodeResponse extends BaseApiResponse {
  data?: {
    email: string
    expiresIn: string
  }
}

// Verify register request
export interface VerifyRegisterRequest {
  email: string
  code: string
}

// Verify register response
export interface VerifyRegisterResponse extends BaseApiResponse {
  data?: {
    user: User
    tokens: Tokens
  }
}

// Forgot password step type
export type ForgotPasswordStep = "email" | "reset"

// Forgot password request
export interface ForgotPasswordRequest {
  email: string
  language?: string
}

// Forgot password response
export interface ForgotPasswordResponse extends BaseApiResponse {
  data?: {
    email: string
    expiresIn: number
  }
}

// Resend forgot password code request
export interface ResendForgotPasswordRequest {
  email: string
  language?: string
}

// Resend forgot password code response
export interface ResendForgotPasswordResponse extends BaseApiResponse {
  data?: {
    email: string
    expiresIn: number
  }
}

// Reset password request
export interface ResetPasswordRequest {
  email: string
  code: string
  newPassword: string
}

// Reset password response
export interface ResetPasswordResponse extends BaseApiResponse {
  data?: {
    message: string
  }
}

// Logout response
export interface LogoutResponse extends BaseApiResponse {}

// ============================================
// Profile API Types
// ============================================

// Get profile response
export interface GetProfileResponse extends BaseApiResponse {
  data?: {
    userID: number
    username: string
    email: string
    profile: UserProfile
    roles: string[]
    referralCode: string
    hasReferrer: boolean
    referredByCode?: string
    status: string
    lastLogin: string
    createdAt: string
  }
}

// Update profile request
export interface UpdateProfileRequest {
  nickname?: string
  avatar?: string
}

// Update profile response
export interface UpdateProfileResponse extends BaseApiResponse {
  data?: {
    user: User
  }
}

// ============================================
// Change Password API Types
// ============================================

// Change password request
export interface ChangePasswordRequest {
  oldPassword: string
  newPassword: string
  logoutOtherDevices?: boolean
}

// Change password response
export interface ChangePasswordResponse extends BaseApiResponse {
  data?: {
    loggedOutOtherDevices: boolean
  }
}

// ============================================
// Refresh Token API Types
// ============================================

// Refresh token request
export interface RefreshTokenRequest {
  refreshToken: string
}

// Refresh token response
export interface RefreshTokenResponse extends BaseApiResponse {
  data?: {
    accessToken: string
    accessTokenExpiry: string
  }
}

// ============================================
// Email Change API Types
// ============================================

// Send email verification code request
export interface SendEmailCodeRequest {
  email: string
  language?: string
}

// Send email verification code response
export interface SendEmailCodeResponse extends BaseApiResponse {
  data?: {
    email: string
    expiresIn: string
  }
}

// Verify current email request (Step 1)
export interface VerifyCurrentEmailRequest {
  action: "verify-current"
  currentCode: string
}

// Verify current email response
export interface VerifyCurrentEmailResponse extends BaseApiResponse {
  data?: {
    currentEmailVerified: boolean
    expiresIn: string
  }
}

// Bind new email request (Step 2)
export interface BindNewEmailRequest {
  action: "bind-new"
  newEmail: string
  newCode: string
}

// Bind new email response
export interface BindNewEmailResponse extends BaseApiResponse {
  data?: {
    user: User
  }
}

// Union type for email change requests
export type EmailChangeRequest = VerifyCurrentEmailRequest | BindNewEmailRequest

// Union type for email change responses
export type EmailChangeResponse = VerifyCurrentEmailResponse | BindNewEmailResponse

// ============================================
// Session Management API Types
// ============================================

// Device info for session
export interface DeviceInfo {
  deviceId?: string
  deviceName?: string
  deviceType: string
  os: string
  osVersion?: string
  browser: string
  browserVersion?: string
  userAgent?: string
}

// Location info for session
export interface LocationInfo {
  country?: string
  city?: string
}

// Session item
export interface Session {
  id: string
  userId: string
  tokenJti: string
  deviceInfo: DeviceInfo
  ipAddress: string
  location?: LocationInfo
  loginMethod: string
  isActive: boolean
  isCurrent: boolean
  lastActiveAt: string
  createdAt: string
  expiresAt: string
}

// Get sessions request params
export interface GetSessionsParams {
  page?: number
  pageSize?: number
  active?: boolean
}

// Get sessions response
export interface GetSessionsResponse extends BaseApiResponse {
  data?: {
    sessions: Session[]
    total: number
    page: number
    pageSize: number
    totalPages: number
  }
}

// Get session count response
export interface GetSessionCountResponse extends BaseApiResponse {
  data?: {
    activeCount: number
  }
}

// Login history record
export interface LoginHistoryRecord {
  id: string
  userId: string
  sessionId?: string
  deviceInfo: DeviceInfo
  ipAddress: string
  location?: LocationInfo
  loginMethod: string
  status: "success" | "failed"
  createdAt: string
}

// Get login history params
export interface GetLoginHistoryParams {
  page?: number
  pageSize?: number
  status?: "success" | "failed"
  startDate?: string
  endDate?: string
}

// Get login history response
export interface GetLoginHistoryResponse extends BaseApiResponse {
  data?: {
    records: LoginHistoryRecord[]
    total: number
    page: number
    pageSize: number
    totalPages: number
  }
}

// Revoke session response
export interface RevokeSessionResponse extends BaseApiResponse {}

// Revoke other sessions response
export interface RevokeOtherSessionsResponse extends BaseApiResponse {
  data?: {
    revokedCount: number
  }
}
