/**
 * 联系方式类型
 */
export enum ContactType {
  EMAIL = 'EMAIL',
  WECHAT = 'WECHAT',
  WHATSAPP = 'WHATSAPP',
  TELEGRAM = 'TELEGRAM',
  LINE = 'LINE',
}

/**
 * 通知类型
 */
export enum NotificationType {
  POLICY_UPDATE = 'POLICY_UPDATE',       // 政策变动
  DEADLINE_REMINDER = 'DEADLINE_REMINDER', // 截止日期提醒
  APPOINTMENT = 'APPOINTMENT',           // 预约提醒
  CASE_UPDATE = 'CASE_UPDATE',           // 案件进度
  PROMOTION = 'PROMOTION',               // 优惠活动
}

/**
 * User Contact Domain Entity
 * 用户联系方式 (用于通知推送)
 */
export class UserContactEntity {
  readonly id: string;
  readonly userId: string;
  type: ContactType;
  value: string;                         // 邮箱地址/微信openid/手机号等
  displayName: string | null;            // 显示名称 (如微信昵称)
  isVerified: boolean;
  verifiedAt: Date | null;

  // 通知设置 (付费功能)
  notificationEnabled: boolean;
  enabledNotificationTypes: NotificationType[];

  // 验证相关
  verificationCode: string | null;
  verificationExpiresAt: Date | null;

  readonly createdAt: Date;
  updatedAt: Date;

  private constructor(props: {
    id: string;
    userId: string;
    type: ContactType;
    value: string;
    displayName?: string | null;
    isVerified?: boolean;
    verifiedAt?: Date | null;
    notificationEnabled?: boolean;
    enabledNotificationTypes?: NotificationType[];
    verificationCode?: string | null;
    verificationExpiresAt?: Date | null;
    createdAt?: Date;
    updatedAt?: Date;
  }) {
    this.id = props.id;
    this.userId = props.userId;
    this.type = props.type;
    this.value = props.value;
    this.displayName = props.displayName ?? null;
    this.isVerified = props.isVerified ?? false;
    this.verifiedAt = props.verifiedAt ?? null;
    this.notificationEnabled = props.notificationEnabled ?? false;
    this.enabledNotificationTypes = props.enabledNotificationTypes ?? [];
    this.verificationCode = props.verificationCode ?? null;
    this.verificationExpiresAt = props.verificationExpiresAt ?? null;
    this.createdAt = props.createdAt ?? new Date();
    this.updatedAt = props.updatedAt ?? new Date();
  }

  /**
   * Create a new contact
   */
  static create(userId: string, type: ContactType, value: string): UserContactEntity {
    return new UserContactEntity({
      id: crypto.randomUUID(),
      userId,
      type,
      value,
      createdAt: new Date(),
      updatedAt: new Date(),
    });
  }

  /**
   * Reconstruct from persistence
   */
  static fromPersistence(props: {
    id: string;
    userId: string;
    type: string;
    value: string;
    displayName: string | null;
    isVerified: boolean;
    verifiedAt: Date | null;
    notificationEnabled: boolean;
    enabledNotificationTypes: string[];
    verificationCode: string | null;
    verificationExpiresAt: Date | null;
    createdAt: Date;
    updatedAt: Date;
  }): UserContactEntity {
    return new UserContactEntity({
      ...props,
      type: props.type as ContactType,
      enabledNotificationTypes: props.enabledNotificationTypes as NotificationType[],
    });
  }

  /**
   * Generate verification code
   */
  generateVerificationCode(): string {
    const code = Math.random().toString(36).substring(2, 8).toUpperCase();
    this.verificationCode = code;
    this.verificationExpiresAt = new Date(Date.now() + 10 * 60 * 1000); // 10 minutes
    this.updatedAt = new Date();
    return code;
  }

  /**
   * Verify with code
   */
  verify(code: string): boolean {
    if (!this.verificationCode || !this.verificationExpiresAt) {
      return false;
    }

    if (new Date() > this.verificationExpiresAt) {
      return false;
    }

    if (this.verificationCode !== code) {
      return false;
    }

    this.isVerified = true;
    this.verifiedAt = new Date();
    this.verificationCode = null;
    this.verificationExpiresAt = null;
    this.updatedAt = new Date();
    return true;
  }

  /**
   * Mark as verified (for OAuth bindings)
   */
  markAsVerified(displayName?: string): void {
    this.isVerified = true;
    this.verifiedAt = new Date();
    if (displayName) {
      this.displayName = displayName;
    }
    this.updatedAt = new Date();
  }

  /**
   * Enable notifications
   */
  enableNotifications(types: NotificationType[]): void {
    this.notificationEnabled = true;
    this.enabledNotificationTypes = types;
    this.updatedAt = new Date();
  }

  /**
   * Disable notifications
   */
  disableNotifications(): void {
    this.notificationEnabled = false;
    this.enabledNotificationTypes = [];
    this.updatedAt = new Date();
  }

  /**
   * Update notification types
   */
  updateNotificationTypes(types: NotificationType[]): void {
    this.enabledNotificationTypes = types;
    this.updatedAt = new Date();
  }

  /**
   * Check if notification type is enabled
   */
  isNotificationTypeEnabled(type: NotificationType): boolean {
    return this.notificationEnabled && this.enabledNotificationTypes.includes(type);
  }

  /**
   * Update contact value
   */
  updateValue(value: string): void {
    this.value = value;
    this.isVerified = false;
    this.verifiedAt = null;
    this.updatedAt = new Date();
  }
}
