/**
 * 咨询服务计价体系 - 统一类型定义
 *
 * 本文件集中定义所有计价相关的接口和类型，
 * 供 zxFw 主表、4个计价法组件、Store、计算模块共用。
 */

/* ----------------------------------------------------------------
 * 计价方法枚举
 * ---------------------------------------------------------------- */

/** 4种计价方法字段名（对应 zxFw 主表列） */
export type PricingMethodField = 'investScale' | 'landScale' | 'workload' | 'hourly'

/** 规模法类型：投资规模 / 用地规模 */
export type ScaleType = 'invest' | 'land'

/** 合同附加费用计费方式 */
export type HtFeeMethodType = 'rate-fee' | 'hourly-fee' | 'quantity-unit-price-fee'

/* ----------------------------------------------------------------
 * zxFw 主表行（咨询服务汇总）
 * ---------------------------------------------------------------- */

/** zxFw 主表每行数据 —— 一个咨询服务的4种计价法汇总 */
export interface ZxFwDetailRow {
  id: string
  code?: string
  name?: string
  /** 是否参与流程（1=参与, 0=不参与, null=固定行） */
  process?: number | null
  investScale: number | null
  landScale: number | null
  workload: number | null
  hourly: number | null
  /** 4种计价法小计 */
  subtotal?: number | null
  /** 最终费用 */
  finalFee?: number | null
  actions?: unknown
}

/** zxFw 主表状态（一个合同的全部咨询服务） */
export interface ZxFwState {
  selectedIds?: string[]
  selectedCodes?: string[]
  detailRows: ZxFwDetailRow[]
}

/* ----------------------------------------------------------------
 * 计价法明细状态（通用容器）
 * ---------------------------------------------------------------- */

/** 单个计价法的明细状态 */
export interface ServicePricingMethodState<TRow = unknown> {
  detailRows: TRow[]
  projectCount?: number | null
}

/** 一个咨询服务的全部计价法状态 */
export interface ServicePricingState {
  investScale?: ServicePricingMethodState
  landScale?: ServicePricingMethodState
  workload?: ServicePricingMethodState
  hourly?: ServicePricingMethodState
}

/* ----------------------------------------------------------------
 * 规模法明细行（投资规模法 + 用地规模法共用）
 * ---------------------------------------------------------------- */

/** 规模法 AG Grid 行数据 */
export interface ScaleDetailRow {
  id: string
  projectIndex?: number
  majorDictId?: string
  groupCode: string
  groupName: string
  majorCode: string
  majorName: string
  hasCost: boolean
  hasArea: boolean
  amount: number | null
  landArea: number | null
  benchmarkBudget: number | null
  benchmarkBudgetBasic: number | null
  benchmarkBudgetOptional: number | null
  benchmarkBudgetBasicChecked: boolean
  benchmarkBudgetOptionalChecked: boolean
  basicFormula: string | null
  optionalFormula: string | null
  consultCategoryFactor: number | null
  majorFactor: number | null
  workStageFactor: number | null
  workRatio: number | null
  budgetFee: number | null
  budgetFeeBasic: number | null
  budgetFeeOptional: number | null
  remark: string
  path: string[]
}

/** 规模法计算用精简行（pricingMethodTotals 使用） */
export interface ScaleCalcRow {
  id: string
  amount: number | null
  landArea: number | null
  consultCategoryFactor: number | null
  majorFactor: number | null
  workStageFactor: number | null
  workRatio: number | null
}

/* ----------------------------------------------------------------
 * 工作量法明细行
 * ---------------------------------------------------------------- */

/** 工作量法 AG Grid 行数据 */
export interface WorkloadDetailRow {
  id: string
  taskCode: string
  taskName: string
  unit: string
  conversion: number | null
  workload: number | null
  basicFee: number | null
  budgetBase: string
  budgetReferenceUnitPrice: string
  budgetAdoptedUnitPrice: number | null
  consultCategoryFactor: number | null
  serviceFee: number | null
  remark: string
  path: string[]
}

/** 工作量法计算用精简行 */
export interface WorkloadCalcRow {
  id: string
  conversion: number | null
  workload: number | null
  basicFee: number | null
  budgetAdoptedUnitPrice: number | null
  consultCategoryFactor: number | null
}

/* ----------------------------------------------------------------
 * 工时法明细行
 * ---------------------------------------------------------------- */

/** 工时法 AG Grid 行数据 */
export interface HourlyDetailRow {
  id: string
  adoptedBudgetUnitPrice: number | null
  personnelCount: number | null
  workdayCount: number | null
}

/* ----------------------------------------------------------------
 * 计价汇总结果
 * ---------------------------------------------------------------- */

/** 4种计价法的汇总金额 */
export interface PricingMethodTotals {
  investScale: number | null
  landScale: number | null
  workload: number | null
  hourly: number | null
}

/* ----------------------------------------------------------------
 * 合同附加费用
 * ---------------------------------------------------------------- */

/** 合同附加费用主表状态 */
export interface HtFeeMainState<TRow = unknown> {
  detailRows: TRow[]
}

/** 合同附加费用子方法载荷 */
export type HtFeeMethodPayload = unknown

/* ----------------------------------------------------------------
 * 字典相关
 * ---------------------------------------------------------------- */

/** 专业字典叶子节点 */
export interface MajorDictLeaf {
  id: string
  code: string
  name: string
  hasCost: boolean
  hasArea: boolean
}

/** 专业字典分组 */
export interface MajorDictGroup {
  id: string
  code: string
  name: string
  children: MajorDictLeaf[]
}

/** 专业字典精简信息 */
export interface MajorLite {
  code: string
  name: string
  defCoe: number | null
  hasCost?: boolean
  hasArea?: boolean
  industryId?: string | number | null
}

/** 咨询服务字典精简信息 */
export interface ServiceLite {
  defCoe: number | null
  onlyCostScale?: boolean | null
  mutiple?: boolean | null
}

/** 工作量法任务字典 */
export interface TaskLite {
  serviceID: number
  code?: string
  ref?: string
  name: string
  basicParam: string
  unit: string
  conversion: number | null
  maxPrice: number | null
  minPrice: number | null
  defPrice: number | null
  desc: string | null
}

/** 工时法专家字典 */
export interface ExpertLite {
  defPrice: number | null
  manageCoe: number | null
}

/* ----------------------------------------------------------------
 * 持久化相关
 * ---------------------------------------------------------------- */

/** 存储的明细行状态（通用） */
export interface StoredDetailRowsState<T = unknown> {
  detailRows?: T[]
}

/** 存储的系数状态 */
export interface StoredFactorState {
  detailRows?: Array<{
    id: string
    standardFactor?: number | null
    budgetValue?: number | null
  }>
}

/** 项目基础信息 */
export interface XmBaseInfoState {
  projectIndustry?: string
}
