/**
 * 系统维护公告实体
 * 用于系统升级/维护期间阻断用户操作
 */
export class SystemMaintenanceEntity {
  constructor(
    public readonly id: string,
    public readonly title: string,
    public readonly message: string,
    public readonly startTime: Date,
    public readonly endTime: Date,
    public readonly isActive: boolean,
    public readonly createdAt: Date,
    public readonly updatedAt: Date,
    public readonly createdBy: string,
    public readonly updatedBy: string | null,
  ) {}

  /**
   * 检查当前是否在维护期间
   */
  isInMaintenanceWindow(): boolean {
    if (!this.isActive) return false;
    const now = new Date();
    return now >= this.startTime && now <= this.endTime;
  }

  /**
   * 检查维护是否已结束
   */
  isExpired(): boolean {
    return new Date() > this.endTime;
  }

  /**
   * 创建新的维护公告
   */
  static create(params: {
    id: string;
    title: string;
    message: string;
    startTime: Date;
    endTime: Date;
    createdBy: string;
  }): SystemMaintenanceEntity {
    return new SystemMaintenanceEntity(
      params.id,
      params.title,
      params.message,
      params.startTime,
      params.endTime,
      false, // 默认不激活，需手动激活
      new Date(),
      new Date(),
      params.createdBy,
      null,
    );
  }

  /**
   * 激活维护公告
   */
  activate(updatedBy: string): SystemMaintenanceEntity {
    return new SystemMaintenanceEntity(
      this.id,
      this.title,
      this.message,
      this.startTime,
      this.endTime,
      true,
      this.createdAt,
      new Date(),
      this.createdBy,
      updatedBy,
    );
  }

  /**
   * 停用维护公告
   */
  deactivate(updatedBy: string): SystemMaintenanceEntity {
    return new SystemMaintenanceEntity(
      this.id,
      this.title,
      this.message,
      this.startTime,
      this.endTime,
      false,
      this.createdAt,
      new Date(),
      this.createdBy,
      updatedBy,
    );
  }

  /**
   * 更新维护公告
   */
  update(params: {
    title?: string;
    message?: string;
    startTime?: Date;
    endTime?: Date;
    updatedBy: string;
  }): SystemMaintenanceEntity {
    return new SystemMaintenanceEntity(
      this.id,
      params.title ?? this.title,
      params.message ?? this.message,
      params.startTime ?? this.startTime,
      params.endTime ?? this.endTime,
      this.isActive,
      this.createdAt,
      new Date(),
      this.createdBy,
      params.updatedBy,
    );
  }
}

/**
 * 维护状态响应（给移动端）
 */
export interface MaintenanceStatusResponse {
  /** 是否在维护中 */
  inMaintenance: boolean;
  /** 维护标题 */
  title?: string;
  /** 维护说明 */
  message?: string;
  /** 预计结束时间 */
  endTime?: Date;
}
