/**
 * 合同模板聚合根
 */
export class ContractTemplate {
  private _id?: number;
  private _version: string;
  private _title: string;
  private _content: string;
  private _effectiveFrom: Date;
  private _effectiveTo?: Date;
  private _isActive: boolean;
  private _createdAt: Date;
  private _updatedAt: Date;

  private constructor() {
    this._createdAt = new Date();
    this._updatedAt = new Date();
  }

  // ============================================
  // 工厂方法
  // ============================================

  /**
   * 创建新的合同模板
   */
  static create(params: {
    version: string;
    title: string;
    content: string;
    effectiveFrom: Date;
    effectiveTo?: Date;
  }): ContractTemplate {
    const template = new ContractTemplate();
    template._version = params.version;
    template._title = params.title;
    template._content = params.content;
    template._effectiveFrom = params.effectiveFrom;
    template._effectiveTo = params.effectiveTo;
    template._isActive = true;
    return template;
  }

  /**
   * 从持久化数据重建
   */
  static reconstitute(data: {
    id: number;
    version: string;
    title: string;
    content: string;
    effectiveFrom: Date;
    effectiveTo?: Date;
    isActive: boolean;
    createdAt: Date;
    updatedAt: Date;
  }): ContractTemplate {
    const template = new ContractTemplate();
    template._id = data.id;
    template._version = data.version;
    template._title = data.title;
    template._content = data.content;
    template._effectiveFrom = data.effectiveFrom;
    template._effectiveTo = data.effectiveTo;
    template._isActive = data.isActive;
    template._createdAt = data.createdAt;
    template._updatedAt = data.updatedAt;
    return template;
  }

  // ============================================
  // Getters
  // ============================================

  get id(): number | undefined {
    return this._id;
  }

  get version(): string {
    return this._version;
  }

  get title(): string {
    return this._title;
  }

  get content(): string {
    return this._content;
  }

  get effectiveFrom(): Date {
    return this._effectiveFrom;
  }

  get effectiveTo(): Date | undefined {
    return this._effectiveTo;
  }

  get isActive(): boolean {
    return this._isActive;
  }

  get createdAt(): Date {
    return this._createdAt;
  }

  get updatedAt(): Date {
    return this._updatedAt;
  }

  // ============================================
  // 业务方法
  // ============================================

  /**
   * 检查模板在指定时间是否有效
   */
  isEffectiveAt(date: Date): boolean {
    if (!this._isActive) {
      return false;
    }

    if (date < this._effectiveFrom) {
      return false;
    }

    if (this._effectiveTo && date > this._effectiveTo) {
      return false;
    }

    return true;
  }

  /**
   * 生成填充用户信息后的合同内容
   *
   * 占位符:
   * - {{USER_PHONE}}: 用户手机号
   * - {{USER_REAL_NAME}}: 用户真实姓名
   * - {{USER_ID_CARD}}: 用户身份证号 (脱敏)
   * - {{TREE_COUNT}}: 认种数量
   * - {{TOTAL_AMOUNT}}: 总金额
   * - {{PROVINCE_NAME}}: 省份名称
   * - {{CITY_NAME}}: 城市名称
   * - {{CURRENT_DATE}}: 当前日期
   * - {{ORDER_NO}}: 订单号
   */
  generateContractContent(params: {
    userPhoneNumber?: string;
    userRealName?: string;
    userIdCardNumber?: string;
    treeCount: number;
    totalAmount: number;
    provinceName: string;
    cityName: string;
    orderNo: string;
  }): string {
    const now = new Date();
    const dateStr = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日`;

    let content = this._content;
    content = content.replace(/\{\{USER_PHONE\}\}/g, params.userPhoneNumber || '未认证');
    content = content.replace(/\{\{USER_REAL_NAME\}\}/g, params.userRealName || '未认证');
    content = content.replace(/\{\{USER_ID_CARD\}\}/g, params.userIdCardNumber || '未认证');
    content = content.replace(/\{\{TREE_COUNT\}\}/g, params.treeCount.toString());
    content = content.replace(/\{\{TOTAL_AMOUNT\}\}/g, params.totalAmount.toFixed(2));
    content = content.replace(/\{\{PROVINCE_NAME\}\}/g, params.provinceName);
    content = content.replace(/\{\{CITY_NAME\}\}/g, params.cityName);
    content = content.replace(/\{\{CURRENT_DATE\}\}/g, dateStr);
    content = content.replace(/\{\{ORDER_NO\}\}/g, params.orderNo);

    return content;
  }

  /**
   * 停用模板
   */
  deactivate(): void {
    this._isActive = false;
    this._updatedAt = new Date();
  }

  /**
   * 设置失效时间
   */
  setEffectiveTo(date: Date): void {
    this._effectiveTo = date;
    this._updatedAt = new Date();
  }
}
