export class ProvinceCitySelection {
  private constructor(
    public readonly provinceCode: string,
    public readonly provinceName: string,
    public readonly cityCode: string,
    public readonly cityName: string,
    public readonly selectedAt: Date,
    public readonly confirmedAt: Date | null,
  ) {}

  get isConfirmed(): boolean {
    return this.confirmedAt !== null;
  }

  static create(
    provinceCode: string,
    provinceName: string,
    cityCode: string,
    cityName: string,
  ): ProvinceCitySelection {
    if (!provinceCode || !cityCode) {
      throw new Error('省市代码不能为空');
    }

    return new ProvinceCitySelection(
      provinceCode,
      provinceName,
      cityCode,
      cityName,
      new Date(),
      null,
    );
  }

  static reconstitute(
    provinceCode: string,
    provinceName: string,
    cityCode: string,
    cityName: string,
    selectedAt: Date,
    confirmedAt: Date | null,
  ): ProvinceCitySelection {
    return new ProvinceCitySelection(
      provinceCode,
      provinceName,
      cityCode,
      cityName,
      selectedAt,
      confirmedAt,
    );
  }

  confirm(): ProvinceCitySelection {
    if (this.isConfirmed) {
      throw new Error('省市已确认，不可重复确认');
    }

    return new ProvinceCitySelection(
      this.provinceCode,
      this.provinceName,
      this.cityCode,
      this.cityName,
      this.selectedAt,
      new Date(),
    );
  }

  /**
   * 检查是否已过5秒确认时间
   */
  canConfirm(): boolean {
    const elapsed = Date.now() - this.selectedAt.getTime();
    return elapsed >= 5000; // 5秒
  }

  /**
   * 获取剩余等待时间（毫秒）
   */
  getRemainingWaitTime(): number {
    const elapsed = Date.now() - this.selectedAt.getTime();
    return Math.max(0, 5000 - elapsed);
  }
}
