import { AggregateRoot } from './aggregate-root.base'
import { AuthorizationId, UserId, RegionCode, Month } from '@/domain/value-objects'
import { RoleType, AssessmentResult, BenefitActionType } from '@/domain/enums'
import { v4 as uuidv4 } from 'uuid'

export interface BenefitAssessmentRecordProps {
  id: string
  authorizationId: AuthorizationId
  userId: UserId
  roleType: RoleType
  regionCode: RegionCode
  regionName: string
  assessmentMonth: Month
  monthIndex: number
  monthlyTarget: number
  cumulativeTarget: number
  treesCompleted: number
  treesRequired: number
  benefitActionTaken: BenefitActionType
  previousBenefitStatus: boolean
  newBenefitStatus: boolean
  newValidUntil: Date | null
  result: AssessmentResult
  remarks: string | null
  assessedAt: Date
  createdAt: Date
}

/**
 * 权益有效性考核记录
 * 专门记录权益激活/续期/失效的考核历史
 * 与 MonthlyAssessment (火柴人排名) 分离，避免职责混淆
 */
export class BenefitAssessmentRecord extends AggregateRoot {
  private _id: string
  private _authorizationId: AuthorizationId
  private _userId: UserId
  private _roleType: RoleType
  private _regionCode: RegionCode
  private _regionName: string

  // 考核月份
  private _assessmentMonth: Month
  private _monthIndex: number

  // 考核目标
  private _monthlyTarget: number
  private _cumulativeTarget: number

  // 完成情况
  private _treesCompleted: number
  private _treesRequired: number

  // 权益状态变化
  private _benefitActionTaken: BenefitActionType
  private _previousBenefitStatus: boolean
  private _newBenefitStatus: boolean
  private _newValidUntil: Date | null

  // 考核结果
  private _result: AssessmentResult

  // 备注
  private _remarks: string | null

  // 时间戳
  private _assessedAt: Date
  private _createdAt: Date

  // Getters
  get id(): string {
    return this._id
  }
  get authorizationId(): AuthorizationId {
    return this._authorizationId
  }
  get userId(): UserId {
    return this._userId
  }
  get roleType(): RoleType {
    return this._roleType
  }
  get regionCode(): RegionCode {
    return this._regionCode
  }
  get regionName(): string {
    return this._regionName
  }
  get assessmentMonth(): Month {
    return this._assessmentMonth
  }
  get monthIndex(): number {
    return this._monthIndex
  }
  get monthlyTarget(): number {
    return this._monthlyTarget
  }
  get cumulativeTarget(): number {
    return this._cumulativeTarget
  }
  get treesCompleted(): number {
    return this._treesCompleted
  }
  get treesRequired(): number {
    return this._treesRequired
  }
  get benefitActionTaken(): BenefitActionType {
    return this._benefitActionTaken
  }
  get previousBenefitStatus(): boolean {
    return this._previousBenefitStatus
  }
  get newBenefitStatus(): boolean {
    return this._newBenefitStatus
  }
  get newValidUntil(): Date | null {
    return this._newValidUntil
  }
  get result(): AssessmentResult {
    return this._result
  }
  get remarks(): string | null {
    return this._remarks
  }
  get assessedAt(): Date {
    return this._assessedAt
  }
  get createdAt(): Date {
    return this._createdAt
  }

  // 私有构造函数
  private constructor(props: BenefitAssessmentRecordProps) {
    super()
    this._id = props.id
    this._authorizationId = props.authorizationId
    this._userId = props.userId
    this._roleType = props.roleType
    this._regionCode = props.regionCode
    this._regionName = props.regionName
    this._assessmentMonth = props.assessmentMonth
    this._monthIndex = props.monthIndex
    this._monthlyTarget = props.monthlyTarget
    this._cumulativeTarget = props.cumulativeTarget
    this._treesCompleted = props.treesCompleted
    this._treesRequired = props.treesRequired
    this._benefitActionTaken = props.benefitActionTaken
    this._previousBenefitStatus = props.previousBenefitStatus
    this._newBenefitStatus = props.newBenefitStatus
    this._newValidUntil = props.newValidUntil
    this._result = props.result
    this._remarks = props.remarks
    this._assessedAt = props.assessedAt
    this._createdAt = props.createdAt
  }

  // 工厂方法 - 从数据库重建
  static fromPersistence(props: BenefitAssessmentRecordProps): BenefitAssessmentRecord {
    return new BenefitAssessmentRecord(props)
  }

  // 工厂方法 - 创建新记录
  static create(params: {
    authorizationId: AuthorizationId
    userId: UserId
    roleType: RoleType
    regionCode: RegionCode
    regionName: string
    assessmentMonth: Month
    monthIndex: number
    monthlyTarget: number
    cumulativeTarget: number
    treesCompleted: number
    treesRequired: number
    benefitActionTaken: BenefitActionType
    previousBenefitStatus: boolean
    newBenefitStatus: boolean
    newValidUntil: Date | null
    result: AssessmentResult
    remarks?: string | null
  }): BenefitAssessmentRecord {
    return new BenefitAssessmentRecord({
      id: uuidv4(),
      authorizationId: params.authorizationId,
      userId: params.userId,
      roleType: params.roleType,
      regionCode: params.regionCode,
      regionName: params.regionName,
      assessmentMonth: params.assessmentMonth,
      monthIndex: params.monthIndex,
      monthlyTarget: params.monthlyTarget,
      cumulativeTarget: params.cumulativeTarget,
      treesCompleted: params.treesCompleted,
      treesRequired: params.treesRequired,
      benefitActionTaken: params.benefitActionTaken,
      previousBenefitStatus: params.previousBenefitStatus,
      newBenefitStatus: params.newBenefitStatus,
      newValidUntil: params.newValidUntil,
      result: params.result,
      remarks: params.remarks ?? null,
      assessedAt: new Date(),
      createdAt: new Date(),
    })
  }

  /**
   * 转换为持久化数据
   */
  toPersistence(): Record<string, any> {
    return {
      id: this._id,
      authorizationId: this._authorizationId.value,
      userId: this._userId.value,
      accountSequence: this._userId.accountSequence,
      roleType: this._roleType,
      regionCode: this._regionCode.value,
      regionName: this._regionName,
      assessmentMonth: this._assessmentMonth.value,
      monthIndex: this._monthIndex,
      monthlyTarget: this._monthlyTarget,
      cumulativeTarget: this._cumulativeTarget,
      treesCompleted: this._treesCompleted,
      treesRequired: this._treesRequired,
      benefitActionTaken: this._benefitActionTaken,
      previousBenefitStatus: this._previousBenefitStatus,
      newBenefitStatus: this._newBenefitStatus,
      newValidUntil: this._newValidUntil,
      result: this._result,
      remarks: this._remarks,
      assessedAt: this._assessedAt,
      createdAt: this._createdAt,
    }
  }
}
