import { DomainEvent } from './domain-event.base'
import { RoleType } from '@/domain/enums'

// 社区授权事件（管理员直接授权或自助申请）
export class CommunityAuthorizedEvent extends DomainEvent {
  readonly eventType = 'authorization.community.authorized'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    communityName: string
    authorizedBy: string | null // null 表示自助申请
  }

  constructor(data: {
    authorizationId: string
    userId: string
    communityName: string
    authorizedBy: string | null
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 社区授权申请事件
export class CommunityAuthRequestedEvent extends DomainEvent {
  readonly eventType = 'authorization.community.requested'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    communityName: string
  }

  constructor(data: { authorizationId: string; userId: string; communityName: string }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 授权省公司申请事件
export class AuthProvinceCompanyRequestedEvent extends DomainEvent {
  readonly eventType = 'authorization.auth_province.requested'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    provinceCode: string
    provinceName: string
  }

  constructor(data: {
    authorizationId: string
    userId: string
    provinceCode: string
    provinceName: string
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 授权市公司申请事件
export class AuthCityCompanyRequestedEvent extends DomainEvent {
  readonly eventType = 'authorization.auth_city.requested'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    cityCode: string
    cityName: string
  }

  constructor(data: {
    authorizationId: string
    userId: string
    cityCode: string
    cityName: string
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 正式省公司授权事件
export class ProvinceCompanyAuthorizedEvent extends DomainEvent {
  readonly eventType = 'authorization.province.authorized'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    provinceCode: string
    provinceName: string
    authorizedBy: string | null // null 表示自动升级
  }

  constructor(data: {
    authorizationId: string
    userId: string
    provinceCode: string
    provinceName: string
    authorizedBy: string | null
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 正式市公司授权事件
export class CityCompanyAuthorizedEvent extends DomainEvent {
  readonly eventType = 'authorization.city.authorized'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    cityCode: string
    cityName: string
    authorizedBy: string | null // null 表示自动升级
  }

  constructor(data: {
    authorizationId: string
    userId: string
    cityCode: string
    cityName: string
    authorizedBy: string | null
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// Admin授权省团队事件
export class AuthProvinceCompanyGrantedEvent extends DomainEvent {
  readonly eventType = 'authorization.auth_province.granted'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    provinceCode: string
    provinceName: string
    authorizedBy: string | null // null 表示自助申请
  }

  constructor(data: {
    authorizationId: string
    userId: string
    provinceCode: string
    provinceName: string
    authorizedBy: string | null
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// Admin授权市团队事件
export class AuthCityCompanyGrantedEvent extends DomainEvent {
  readonly eventType = 'authorization.auth_city.granted'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    cityCode: string
    cityName: string
    authorizedBy: string | null // null 表示自助申请
  }

  constructor(data: {
    authorizationId: string
    userId: string
    cityCode: string
    cityName: string
    authorizedBy: string | null
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 权益激活事件
export class BenefitActivatedEvent extends DomainEvent {
  readonly eventType = 'authorization.benefit.activated'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    roleType: RoleType
    regionCode: string
  }

  constructor(data: {
    authorizationId: string
    userId: string
    roleType: RoleType
    regionCode: string
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 权益失效事件
export class BenefitDeactivatedEvent extends DomainEvent {
  readonly eventType = 'authorization.benefit.deactivated'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    roleType: RoleType
    reason: string
  }

  constructor(data: {
    authorizationId: string
    userId: string
    roleType: RoleType
    reason: string
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 角色授权事件
export class RoleAuthorizedEvent extends DomainEvent {
  readonly eventType = 'authorization.role.authorized'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    roleType: RoleType
    regionCode: string
    authorizedBy: string
  }

  constructor(data: {
    authorizationId: string
    userId: string
    roleType: RoleType
    regionCode: string
    authorizedBy: string
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 角色撤销事件
export class RoleRevokedEvent extends DomainEvent {
  readonly eventType = 'authorization.role.revoked'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    roleType: RoleType
    regionCode: string
    reason: string
    revokedBy: string
  }

  constructor(data: {
    authorizationId: string
    userId: string
    roleType: RoleType
    regionCode: string
    reason: string
    revokedBy: string
  }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}

// 占比考核豁免事件
export class PercentageCheckExemptedEvent extends DomainEvent {
  readonly eventType = 'authorization.percentage_check.exempted'
  readonly aggregateId: string
  readonly payload: {
    authorizationId: string
    userId: string
    exemptedBy: string
  }

  constructor(data: { authorizationId: string; userId: string; exemptedBy: string }) {
    super()
    this.aggregateId = data.authorizationId
    this.payload = data
  }
}
