/**
 * 用户详情页面类型定义
 */

// ============================================================================
// 用户基本信息
// ============================================================================

export interface UserFullDetail {
  // 基本信息
  accountId: string;
  accountSequence: string;
  avatar: string | null;
  nickname: string | null;
  phoneNumberMasked: string | null;
  status: 'active' | 'frozen' | 'deactivated';
  kycStatus: string;
  isOnline: boolean;
  registeredAt: string;
  lastActiveAt: string | null;

  // 认种统计
  personalAdoptions: number;
  teamAddresses: number;
  teamAdoptions: number;
  provincialAdoptions: {
    count: number;
    percentage: number;
  };
  cityAdoptions: {
    count: number;
    percentage: number;
  };

  // 预种统计
  selfPrePlantingPortions: number;
  teamPrePlantingPortions: number;

  // 排名
  ranking: number | null;

  // 推荐信息
  referralInfo: {
    myReferralCode: string;
    usedReferralCode: string | null;
    referrerId: string | null;
    referrerSequence: string | null;
    referrerNickname: string | null;
    ancestorPath: string | null;
    depth: number;
    directReferralCount: number;
    activeDirectCount: number;
  };
}

// ============================================================================
// 推荐关系树
// ============================================================================

export interface ReferralNode {
  accountSequence: string;
  userId: string;
  nickname: string | null;
  avatar: string | null;
  personalAdoptions: number;
  teamAdoptions: number; // 团队认种量
  selfPrePlantingPortions: number; // 个人预种份数
  teamPrePlantingPortions: number; // 团队预种份数
  depth: number;
  directReferralCount: number;
  isCurrentUser?: boolean;
}

export interface ReferralTreeData {
  currentUser: ReferralNode;
  ancestors: ReferralNode[]; // 向上的推荐人链（从直接推荐人到最顶层）
  directReferrals: ReferralNode[]; // 直推用户列表
}

// ============================================================================
// 认种信息
// ============================================================================

export interface PlantingSummary {
  totalOrders: number;
  totalTreeCount: number;
  totalAmount: string;
  effectiveTreeCount: number;
  pendingTreeCount: number;
  firstPlantingAt: string | null;
  lastPlantingAt: string | null;
}

export interface PlantingLedgerItem {
  orderId: string;
  orderNo: string;
  treeCount: number;
  totalAmount: string;
  status: string;
  selectedProvince: string | null;
  selectedCity: string | null;
  createdAt: string;
  paidAt: string | null;
  fundAllocatedAt: string | null;
  miningEnabledAt: string | null;
}

export interface PlantingLedgerResponse {
  summary: PlantingSummary;
  items: PlantingLedgerItem[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

// 资金分配明细
export interface FundAllocationItem {
  allocationId: string;
  orderId: string;
  orderNo: string;
  targetType: string;
  amount: string;
  targetAccountId: string | null;
  createdAt: string;
}

// ============================================================================
// 钱包信息
// ============================================================================

export interface WalletSummary {
  // USDT
  usdtAvailable: string;
  usdtFrozen: string;
  // DST
  dstAvailable: string;
  dstFrozen: string;
  // BNB
  bnbAvailable: string;
  bnbFrozen: string;
  // OG
  ogAvailable: string;
  ogFrozen: string;
  // RWAD
  rwadAvailable: string;
  rwadFrozen: string;
  // 算力
  hashpower: string;
  // 收益
  pendingUsdt: string;
  pendingHashpower: string;
  settleableUsdt: string;
  settleableHashpower: string;
  settledTotalUsdt: string;
  settledTotalHashpower: string;
  expiredTotalUsdt: string;
  expiredTotalHashpower: string;
}

export interface WalletLedgerItem {
  entryId: string;
  entryType: string;
  assetType: string;
  amount: string;
  balanceAfter: string | null;
  refOrderId: string | null;
  refTxHash: string | null;
  memo: string | null;
  createdAt: string;
}

export interface WalletLedgerResponse {
  summary: WalletSummary;
  items: WalletLedgerItem[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

// ============================================================================
// 授权信息
// ============================================================================

export interface AuthorizationRole {
  id: string;
  roleType: string;
  regionCode: string;
  regionName: string;
  displayTitle: string;
  status: string;
  benefitActive: boolean;
  benefitActivatedAt: string | null;
  authorizedAt: string | null;
  authorizedBy: string | null;
  initialTargetTreeCount: number;
  monthlyTargetType: string;
  lastAssessmentMonth: string | null;
  monthlyTreesAdded: number;
  officePhotoUrls: string[];
  createdAt: string;
}

export interface MonthlyAssessment {
  id: string;
  authorizationId: string;
  roleType: string;
  regionCode: string;
  assessmentMonth: string;
  monthIndex: number;
  monthlyTarget: number;
  monthlyCompleted: number;
  cumulativeTarget: number;
  cumulativeCompleted: number;
  result: string;
  rankingInRegion: number | null;
  isFirstPlace: boolean;
  isBypassed: boolean;
  completedAt: string | null;
  assessedAt: string | null;
}

export interface SystemAccountLedgerItem {
  ledgerId: string;
  accountId: string;
  accountType: string;
  entryType: string;
  amount: string;
  balanceAfter: string;
  sourceOrderId: string | null;
  sourceRewardId: string | null;
  txHash: string | null;
  memo: string | null;
  createdAt: string;
}

// [2026-01-08] 新增：权益考核记录
export interface BenefitAssessment {
  id: string;
  authorizationId: string;
  roleType: string;
  regionCode: string;
  regionName: string;
  assessmentMonth: string;
  monthIndex: number;
  monthlyTarget: number;
  cumulativeTarget: number;
  treesCompleted: number;
  treesRequired: number;
  benefitActionTaken: string; // ACTIVATED, RENEWED, DEACTIVATED, NO_CHANGE
  previousBenefitStatus: boolean;
  newBenefitStatus: boolean;
  newValidUntil: string | null;
  result: string;
  remarks: string | null;
  assessedAt: string;
  createdAt: string;
}

export interface AuthorizationDetailResponse {
  roles: AuthorizationRole[];
  assessments: MonthlyAssessment[];
  benefitAssessments: BenefitAssessment[]; // [2026-01-08] 新增
  systemAccountLedger: SystemAccountLedgerItem[];
}

// ============================================================================
// API 查询参数
// ============================================================================

export interface LedgerQueryParams {
  page?: number;
  pageSize?: number;
  startDate?: string;
  endDate?: string;
}

export interface WalletLedgerQueryParams extends LedgerQueryParams {
  assetType?: string;
  entryType?: string;
}

// ============================================================================
// 流水类型映射
// ============================================================================

export const WALLET_ENTRY_TYPE_LABELS: Record<string, string> = {
  // 充值
  DEPOSIT: '充值',
  DEPOSIT_USDT: '绿积分充值',
  DEPOSIT_BNB: 'BNB充值',
  // 提现
  WITHDRAW: '提现',
  WITHDRAW_FROZEN: '提现冻结',
  WITHDRAW_CONFIRMED: '提现确认',
  WITHDRAW_CANCELLED: '提现取消',
  // 认种
  PLANTING_PAYMENT: '认种支付',
  PLANTING_FROZEN: '认种冻结',
  PLANTING_DEDUCT: '认种扣款',
  // 收益
  REWARD_PENDING: '收益待领取',
  REWARD_SETTLED: '收益结算',
  REWARD_EXPIRED: '收益过期',
  // 转账
  TRANSFER_OUT: '转出',
  TRANSFER_IN: '转入',
  INTERNAL_TRANSFER: '内部转账',
  // 其他
  ADMIN_ADJUSTMENT: '管理员调整',
  SYSTEM_DEDUCT: '系统扣款',
  FEE: '手续费',
};

export const ASSET_TYPE_LABELS: Record<string, string> = {
  USDT: '绿积分',
  DST: 'DST',
  BNB: 'BNB',
  OG: 'OG',
  RWAD: 'RWAD',
  HASHPOWER: '算力',
};

export const PLANTING_STATUS_LABELS: Record<string, string> = {
  CREATED: '已创建',
  PAID: '已支付',
  FUND_ALLOCATED: '资金已分配',
  MINING_ENABLED: '已开始挖矿',
  CANCELLED: '已取消',
  EXPIRED: '已过期',
};

export const AUTHORIZATION_ROLE_LABELS: Record<string, string> = {
  COMMUNITY_PARTNER: '社区合伙人',
  PROVINCE_COMPANY: '省公司',
  CITY_COMPANY: '市公司',
  AUTH_PROVINCE_COMPANY: '授权省公司',
  AUTH_CITY_COMPANY: '授权市公司',
};

export const AUTHORIZATION_STATUS_LABELS: Record<string, string> = {
  PENDING: '待授权',
  AUTHORIZED: '已授权',
  REVOKED: '已撤销',
  EXPIRED: '已过期',
};

export const ASSESSMENT_RESULT_LABELS: Record<string, string> = {
  NOT_ASSESSED: '未考核',
  PASSED: '通过',
  FAILED: '未通过',
  BYPASSED: '豁免',
};

// [2026-01-08] 新增：权益操作类型标签
export const BENEFIT_ACTION_LABELS: Record<string, string> = {
  ACTIVATED: '已激活',
  RENEWED: '已续期',
  DEACTIVATED: '已停用',
  NO_CHANGE: '无变化',
};
