// ============ Types ============
// 设备信息输入 - 100% 保持前端传递的原样存储
export interface DeviceNameInput {
  model?: string; // iPhone 15 Pro, Pixel 8
  platform?: string; // ios, android, web
  osVersion?: string; // iOS 17.2, Android 14
  [key: string]: unknown; // 允许任意其他字段
}

// ============ Commands ============
export class AutoCreateAccountCommand {
  constructor(
    public readonly deviceId: string,
    public readonly deviceName?: DeviceNameInput,
    public readonly inviterReferralCode?: string,
  ) {}
}

export class RegisterByPhoneCommand {
  constructor(
    public readonly phoneNumber: string,
    public readonly smsCode: string,
    public readonly password: string,
    public readonly deviceId: string,
    public readonly deviceName?: DeviceNameInput,
    public readonly inviterReferralCode?: string,
  ) {}
}

export class RegisterWithoutSmsVerifyCommand {
  constructor(
    public readonly phoneNumber: string,
    public readonly password: string,
    public readonly deviceId: string,
    public readonly deviceName?: DeviceNameInput,
    public readonly inviterReferralCode?: string,
  ) {}
}

export class RecoverByMnemonicCommand {
  constructor(
    public readonly accountSequence: string, // 格式: D + YYMMDD + 5位序号
    public readonly mnemonic: string,
    public readonly newDeviceId: string,
    public readonly deviceName?: string,
  ) {}
}

export class RecoverByPhoneCommand {
  constructor(
    public readonly accountSequence: string, // 格式: D + YYMMDD + 5位序号
    public readonly phoneNumber: string,
    public readonly smsCode: string,
    public readonly newDeviceId: string,
    public readonly deviceName?: string,
  ) {}
}

export class AutoLoginCommand {
  constructor(
    public readonly refreshToken: string,
    public readonly deviceId: string,
  ) {}
}

export class RegisterCommand {
  constructor(
    public readonly phoneNumber: string,
    public readonly smsCode: string,
    public readonly deviceId: string,
    public readonly deviceName?: string,
    public readonly inviterReferralCode?: string,
  ) {}
}

export class LoginCommand {
  constructor(
    public readonly phoneNumber: string,
    public readonly smsCode: string,
    public readonly deviceId: string,
  ) {}
}

export class BindPhoneNumberCommand {
  constructor(
    public readonly userId: string,
    public readonly phoneNumber: string,
    public readonly smsCode: string,
  ) {}
}

export class UpdateProfileCommand {
  constructor(
    public readonly userId: string,
    public readonly nickname?: string,
    public readonly avatarUrl?: string,
  ) {}
}

export class BindWalletAddressCommand {
  constructor(
    public readonly userId: string,
    public readonly chainType: string,
    public readonly address: string,
  ) {}
}

export class SubmitKYCCommand {
  constructor(
    public readonly userId: string,
    public readonly realName: string,
    public readonly idCardNumber: string,
    public readonly idCardFrontUrl: string,
    public readonly idCardBackUrl: string,
  ) {}
}

export class ReviewKYCCommand {
  constructor(
    public readonly userId: string,
    public readonly approved: boolean,
    public readonly reason?: string,
  ) {}
}

export class RemoveDeviceCommand {
  constructor(
    public readonly userId: string,
    public readonly currentDeviceId: string,
    public readonly deviceIdToRemove: string,
  ) {}
}

export class SendSmsCodeCommand {
  constructor(
    public readonly phoneNumber: string,
    public readonly type: 'REGISTER' | 'LOGIN' | 'BIND' | 'RECOVER' | 'RESET_PASSWORD',
  ) {}
}

// ============ Queries ============
export class GetMyProfileQuery {
  constructor(public readonly userId: string) {}
}

export class GetMyDevicesQuery {
  constructor(
    public readonly userId: string,
    public readonly currentDeviceId: string,
  ) {}
}

export class GetUserByReferralCodeQuery {
  constructor(public readonly referralCode: string) {}
}

export class ValidateReferralCodeQuery {
  constructor(public readonly referralCode: string) {}
}

export class GetReferralStatsQuery {
  constructor(public readonly userId: string) {}
}

export class GenerateReferralLinkCommand {
  constructor(
    public readonly userId: string,
    public readonly channel?: string, // 渠道标识: wechat, telegram, twitter 等
    public readonly campaignId?: string, // 活动ID
  ) {}
}

export class GetWalletStatusQuery {
  constructor(public readonly userSerialNum: string) {} // 格式: D + YYMMDD + 5位序号
}

export class MarkMnemonicBackedUpCommand {
  constructor(public readonly userId: string) {}
}

export class VerifySmsCodeCommand {
  constructor(
    public readonly phoneNumber: string,
    public readonly smsCode: string,
    public readonly type: 'REGISTER' | 'LOGIN' | 'BIND' | 'RECOVER' | 'RESET_PASSWORD',
  ) {}
}

export class SetPasswordCommand {
  constructor(
    public readonly userId: string,
    public readonly password: string,
  ) {}
}

export class ChangePasswordCommand {
  constructor(
    public readonly userId: string,
    public readonly oldPassword: string,
    public readonly newPassword: string,
  ) {}
}

export class SendEmailCodeCommand {
  constructor(
    public readonly userId: string,
    public readonly email: string,
    public readonly purpose: 'BIND_EMAIL' | 'UNBIND_EMAIL',
  ) {}
}

export class BindEmailCommand {
  constructor(
    public readonly userId: string,
    public readonly email: string,
    public readonly code: string,
  ) {}
}

export class UnbindEmailCommand {
  constructor(
    public readonly userId: string,
    public readonly code: string,
  ) {}
}

// ============ Results ============

// 钱包状态
export type WalletStatus = 'generating' | 'ready' | 'failed';

export interface WalletStatusResult {
  status: WalletStatus;
  walletAddresses?: {
    kava: string;
    dst: string;
    bsc: string;
  };
  mnemonic?: string; // 助记词 (ready 状态时返回)
  errorMessage?: string; // 失败原因 (failed 状态时返回)
}
export interface AutoCreateAccountResult {
  userSerialNum: string; // 用户序列号 (格式: D + YYMMDD + 5位序号)
  referralCode: string; // 推荐码
  username: string; // 随机用户名
  avatarSvg: string; // 随机SVG头像
  accessToken: string;
  refreshToken: string;
}

export interface RecoverAccountResult {
  userId: string;
  accountSequence: string; // 格式: D + YYMMDD + 5位序号
  nickname: string;
  avatarUrl: string | null;
  referralCode: string;
  accessToken: string;
  refreshToken: string;
}

export interface AutoLoginResult {
  userId: string;
  accountSequence: string; // 格式: D + YYMMDD + 5位序号
  accessToken: string;
  refreshToken: string;
}

export interface RegisterResult {
  userId: string;
  accountSequence: string; // 格式: D + YYMMDD + 5位序号
  referralCode: string;
  accessToken: string;
  refreshToken: string;
}

export interface LoginResult {
  userId: string;
  accountSequence: string; // 格式: D + YYMMDD + 5位序号
  accessToken: string;
  refreshToken: string;
}

export interface UserProfileDTO {
  userId: string;
  accountSequence: string; // 格式: D + YYMMDD + 5位序号
  phoneNumber: string | null;
  nickname: string;
  avatarUrl: string | null;
  referralCode: string;
  walletAddresses: Array<{ chainType: string; address: string }>;
  kycStatus: string;
  kycInfo: { realName: string; idCardNumber: string } | null;
  status: string;
  registeredAt: Date;
  lastLoginAt: Date | null;
}

export interface DeviceDTO {
  deviceId: string;
  deviceName: string;
  addedAt: Date;
  lastActiveAt: Date;
  isCurrent: boolean;
}

export interface UserBriefDTO {
  userId: string;
  accountSequence: string; // 格式: D + YYMMDD + 5位序号
  nickname: string;
  avatarUrl: string | null;
}

export interface ReferralCodeValidationResult {
  valid: boolean;
  referralCode?: string;
  inviterInfo?: {
    accountSequence: string; // 格式: D + YYMMDD + 5位序号
    nickname: string;
    avatarUrl: string | null;
  };
  message?: string;
}

export interface ReferralLinkResult {
  linkId: string;
  referralCode: string;
  shortUrl: string;
  fullUrl: string;
  channel: string | null;
  campaignId: string | null;
  createdAt: Date;
}

export interface ReferralStatsResult {
  referralCode: string;
  totalInvites: number; // 总邀请人数
  directInvites: number; // 直接邀请人数
  indirectInvites: number; // 间接邀请人数 (二级)
  todayInvites: number; // 今日邀请
  thisWeekInvites: number; // 本周邀请
  thisMonthInvites: number; // 本月邀请
  recentInvites: Array<{
    // 最近邀请记录
    accountSequence: string; // 格式: D + YYMMDD + 5位序号
    nickname: string;
    avatarUrl: string | null;
    registeredAt: Date;
    level: number; // 1=直接, 2=间接
  }>;
}

export interface MeResult {
  userId: string;
  accountSequence: string; // 格式: D + YYMMDD + 5位序号
  phoneNumber: string | null;
  nickname: string;
  avatarUrl: string | null;
  referralCode: string;
  referralLink: string; // 完整推荐链接
  inviterSequence: string | null; // 推荐人序列号 (格式: D + YYMMDD + 5位序号)
  walletAddresses: Array<{ chainType: string; address: string }>;
  kycStatus: string;
  status: string;
  registeredAt: Date;
}
