export type Option<T = string> = {
  label: string
  value: T
}

export const OWNERSHIP_TYPE = {
  major: 'major',
  comprehensive: 'comprehensive',
  specialSubcontract: 'special_subcontract_major',
  sourceCoopSubcontract: 'special_subcontract_source_coop',
  comprehensiveSubcontract: 'special_subcontract_comprehensive'
} as const

export const OWNERSHIP_TYPE_LABEL: Record<(typeof OWNERSHIP_TYPE)[keyof typeof OWNERSHIP_TYPE], string> = {
  [OWNERSHIP_TYPE.major]: '专业所',
  [OWNERSHIP_TYPE.comprehensive]: '综合所',
  [OWNERSHIP_TYPE.specialSubcontract]: '专项分包-专业所',
  [OWNERSHIP_TYPE.sourceCoopSubcontract]: '专项分包-源头合作分包',
  [OWNERSHIP_TYPE.comprehensiveSubcontract]: '专项分包-综合所'
}

export const CALCULATION_METHOD = {
  guidancePrice: 'guidance_price',
  contractPrice: 'contract_price',
  virtualOutput: 'virtual_output'
} as const

export const CALCULATION_METHOD_LABEL: Record<
  (typeof CALCULATION_METHOD)[keyof typeof CALCULATION_METHOD],
  string
> = {
  [CALCULATION_METHOD.guidancePrice]: '指导价法',
  [CALCULATION_METHOD.contractPrice]: '合同价法',
  [CALCULATION_METHOD.virtualOutput]: '虚拟产值法'
}

export const VIRTUAL_CALCULATION_METHOD = {
  guidancePrice: 'guidance_price',
  guidanceTotalPrice: 'guidance_total_price',
  workingDay: 'working_day'
} as const

export const VIRTUAL_CALCULATION_METHOD_LABEL: Record<
  (typeof VIRTUAL_CALCULATION_METHOD)[keyof typeof VIRTUAL_CALCULATION_METHOD],
  string
> = {
  [VIRTUAL_CALCULATION_METHOD.guidancePrice]: '指导单价法',
  [VIRTUAL_CALCULATION_METHOD.guidanceTotalPrice]: '指导总价法',
  [VIRTUAL_CALCULATION_METHOD.workingDay]: '工日法'
}

export const DESIGN_STAGE = {
  scheme: 'scheme',
  constructionDrawing: 'construction_drawing',
  schemeAndConstructionDrawing: 'scheme_and_construction_drawing'
} as const

export const DESIGN_STAGE_LABEL: Record<(typeof DESIGN_STAGE)[keyof typeof DESIGN_STAGE], string> = {
  [DESIGN_STAGE.scheme]: '方案',
  [DESIGN_STAGE.constructionDrawing]: '施工图',
  [DESIGN_STAGE.schemeAndConstructionDrawing]: '方案+施工图'
}

export const DESIGN_PART = {
  realEstate: 'above_ground',
  underground: 'underground',
  other: 'other'
} as const

export const DESIGN_PART_LABEL: Record<(typeof DESIGN_PART)[keyof typeof DESIGN_PART], string> = {
  [DESIGN_PART.realEstate]: '地上部分',
  [DESIGN_PART.underground]: '地下部分',
  [DESIGN_PART.other]: '其他'
}

export const PROJECT_TYPE = {
  building: '建筑工程',
  decoration: '精装工程',
  comprehensive: '综合工程',
  special: '专项设计',
  bim: 'BIM设计',
  other: '其他'
} as const

export const PROJECT_CATEGORY = {
  residential: '住宅',
  publicBuilding: '公建',
  industry: '工业',
  landscape: '园林景观',
  other: '其他'
} as const

export const PROJECT_STATUS = {
  inProgress: 'in_progress',
  completed: 'completed',
  paused: 'paused',
  terminated: 'terminated'
} as const

export const PROJECT_STATUS_LABEL: Record<(typeof PROJECT_STATUS)[keyof typeof PROJECT_STATUS], string> = {
  [PROJECT_STATUS.inProgress]: '进行中',
  [PROJECT_STATUS.completed]: '完成',
  [PROJECT_STATUS.paused]: '暂停',
  [PROJECT_STATUS.terminated]: '中止'
}

export const EMPLOYEE_GENDER = {
  male: '男',
  female: '女'
} as const

export const EMPLOYEE_STATUS = {
  active: '在职',
  resigned: '离职',
  suspended: '停职'
} as const

const normalizeValue = (value: string | undefined, validValues: string[]) =>
  value && validValues.includes(value) ? value : undefined

const findLabel = <T>(options: Array<Option<T>>, value?: T | string) =>
  options.find((item) => String(item.value) === String(value))?.label

export const normalizeOwnershipType = (value?: string) =>
  normalizeValue(value, Object.values(OWNERSHIP_TYPE))

export const normalizeCalculationMethod = (value?: string) =>
  normalizeValue(value, Object.values(CALCULATION_METHOD))

export const normalizeVirtualCalculationMethod = (value?: string) =>
  normalizeValue(value, Object.values(VIRTUAL_CALCULATION_METHOD))

export const normalizeDesignStage = (value?: string) =>
  normalizeValue(value, Object.values(DESIGN_STAGE))

export const normalizeDesignPart = (value?: string) =>
  normalizeValue(value, Object.values(DESIGN_PART))

export const normalizeProjectType = (value?: string) =>
  normalizeValue(value, Object.values(PROJECT_TYPE))

export const normalizeProjectCategory = (value?: string) =>
  normalizeValue(value, Object.values(PROJECT_CATEGORY))

export const normalizeProjectStatus = (value?: string) =>
  normalizeValue(value, Object.values(PROJECT_STATUS))

export const OWNERSHIP_TYPE_OPTIONS: Option[] = [
  { label: OWNERSHIP_TYPE_LABEL[OWNERSHIP_TYPE.major], value: OWNERSHIP_TYPE.major },
  { label: OWNERSHIP_TYPE_LABEL[OWNERSHIP_TYPE.comprehensive], value: OWNERSHIP_TYPE.comprehensive },
  {
    label: OWNERSHIP_TYPE_LABEL[OWNERSHIP_TYPE.specialSubcontract],
    value: OWNERSHIP_TYPE.specialSubcontract
  },
  {
    label: OWNERSHIP_TYPE_LABEL[OWNERSHIP_TYPE.sourceCoopSubcontract],
    value: OWNERSHIP_TYPE.sourceCoopSubcontract
  },
  {
    label: OWNERSHIP_TYPE_LABEL[OWNERSHIP_TYPE.comprehensiveSubcontract],
    value: OWNERSHIP_TYPE.comprehensiveSubcontract
  }
]

export const CALCULATION_METHOD_OPTIONS: Option[] = [
  {
    label: CALCULATION_METHOD_LABEL[CALCULATION_METHOD.guidancePrice],
    value: CALCULATION_METHOD.guidancePrice
  },
  {
    label: CALCULATION_METHOD_LABEL[CALCULATION_METHOD.contractPrice],
    value: CALCULATION_METHOD.contractPrice
  },
  {
    label: CALCULATION_METHOD_LABEL[CALCULATION_METHOD.virtualOutput],
    value: CALCULATION_METHOD.virtualOutput
  }
]

export const VIRTUAL_CALCULATION_METHOD_OPTIONS: Option[] = [
  {
    label: VIRTUAL_CALCULATION_METHOD_LABEL[VIRTUAL_CALCULATION_METHOD.guidancePrice],
    value: VIRTUAL_CALCULATION_METHOD.guidancePrice
  },
  {
    label: VIRTUAL_CALCULATION_METHOD_LABEL[VIRTUAL_CALCULATION_METHOD.guidanceTotalPrice],
    value: VIRTUAL_CALCULATION_METHOD.guidanceTotalPrice
  },
  {
    label: VIRTUAL_CALCULATION_METHOD_LABEL[VIRTUAL_CALCULATION_METHOD.workingDay],
    value: VIRTUAL_CALCULATION_METHOD.workingDay
  }
]

export const DESIGN_STAGE_OPTIONS: Option[] = [
  { label: DESIGN_STAGE_LABEL[DESIGN_STAGE.scheme], value: DESIGN_STAGE.scheme },
  {
    label: DESIGN_STAGE_LABEL[DESIGN_STAGE.constructionDrawing],
    value: DESIGN_STAGE.constructionDrawing
  },
  {
    label: DESIGN_STAGE_LABEL[DESIGN_STAGE.schemeAndConstructionDrawing],
    value: DESIGN_STAGE.schemeAndConstructionDrawing
  }
]

export const DESIGN_PART_OPTIONS: Option[] = [
  { label: DESIGN_PART_LABEL[DESIGN_PART.realEstate], value: DESIGN_PART.realEstate },
  { label: DESIGN_PART_LABEL[DESIGN_PART.underground], value: DESIGN_PART.underground },
  { label: DESIGN_PART_LABEL[DESIGN_PART.other], value: DESIGN_PART.other }
]

export const CONTRACT_SIGN_OPTIONS: Option<boolean>[] = [
  { label: '已签约', value: true },
  { label: '未签约', value: false }
]

export const PROJECT_TYPE_OPTIONS: Option[] = [
  { label: PROJECT_TYPE.building, value: PROJECT_TYPE.building },
  { label: PROJECT_TYPE.decoration, value: PROJECT_TYPE.decoration },
  { label: PROJECT_TYPE.comprehensive, value: PROJECT_TYPE.comprehensive },
  { label: PROJECT_TYPE.special, value: PROJECT_TYPE.special },
  { label: PROJECT_TYPE.bim, value: PROJECT_TYPE.bim },
  { label: PROJECT_TYPE.other, value: PROJECT_TYPE.other }
]

export const PROJECT_CATEGORY_OPTIONS: Option[] = [
  { label: PROJECT_CATEGORY.residential, value: PROJECT_CATEGORY.residential },
  { label: PROJECT_CATEGORY.publicBuilding, value: PROJECT_CATEGORY.publicBuilding },
  { label: PROJECT_CATEGORY.industry, value: PROJECT_CATEGORY.industry },
  { label: PROJECT_CATEGORY.landscape, value: PROJECT_CATEGORY.landscape },
  { label: PROJECT_CATEGORY.other, value: PROJECT_CATEGORY.other }
]

export const PROJECT_STATUS_OPTIONS: Option[] = [
  { label: PROJECT_STATUS_LABEL[PROJECT_STATUS.inProgress], value: PROJECT_STATUS.inProgress },
  { label: PROJECT_STATUS_LABEL[PROJECT_STATUS.completed], value: PROJECT_STATUS.completed },
  { label: PROJECT_STATUS_LABEL[PROJECT_STATUS.paused], value: PROJECT_STATUS.paused },
  { label: PROJECT_STATUS_LABEL[PROJECT_STATUS.terminated], value: PROJECT_STATUS.terminated }
]

export const EMPLOYEE_GENDER_OPTIONS: Option[] = [
  { label: EMPLOYEE_GENDER.male, value: EMPLOYEE_GENDER.male },
  { label: EMPLOYEE_GENDER.female, value: EMPLOYEE_GENDER.female }
]

export const EMPLOYEE_STATUS_OPTIONS: Option[] = [
  { label: EMPLOYEE_STATUS.active, value: EMPLOYEE_STATUS.active },
  { label: EMPLOYEE_STATUS.resigned, value: EMPLOYEE_STATUS.resigned },
  { label: EMPLOYEE_STATUS.suspended, value: EMPLOYEE_STATUS.suspended }
]

export const EMPLOYEE_JOB_TITLE = {
  junior: '初级',
  intermediate: '中级',
  viceSenior: '副高级',
  senior: '正高级'
} as const

export const EMPLOYEE_JOB_TITLE_OPTIONS: Option[] = [
  { label: EMPLOYEE_JOB_TITLE.junior, value: EMPLOYEE_JOB_TITLE.junior },
  { label: EMPLOYEE_JOB_TITLE.intermediate, value: EMPLOYEE_JOB_TITLE.intermediate },
  { label: EMPLOYEE_JOB_TITLE.viceSenior, value: EMPLOYEE_JOB_TITLE.viceSenior },
  { label: EMPLOYEE_JOB_TITLE.senior, value: EMPLOYEE_JOB_TITLE.senior }
]

export const DEFAULT_INNOVATION_OUTPUT_RATE = 0.01
export const DEFAULT_WORKING_DAY_UNIT_PRICE = 1000

export const QUARTER_OPTIONS: Option<number>[] = [
  { label: '一季度', value: 1 },
  { label: '二季度', value: 2 },
  { label: '三季度', value: 3 },
  { label: '四季度', value: 4 }
]

export const OUTPUT_SPLIT_SPECIALTY = {
  projectLead: 'project_lead',
  arch: 'arch',
  decor: 'decor',
  struct: 'struct',
  water: 'water',
  elec: 'elec',
  hvac: 'hvac',
  digital: 'digital'
} as const

export const OUTPUT_SPLIT_ROLE = {
  director: 'director',
  check: 'check',
  review: 'review',
  approve: 'approve',
  design: 'design',
  projectManager: 'project_manager',
  engineeringPrincipal: 'engineering_principal'
} as const

export const OUTPUT_SPLIT_SPECIALTY_OPTIONS: Option[] = [
  { label: '建筑', value: OUTPUT_SPLIT_SPECIALTY.arch },
  { label: '装饰', value: OUTPUT_SPLIT_SPECIALTY.decor },
  { label: '结构', value: OUTPUT_SPLIT_SPECIALTY.struct },
  { label: '给排水', value: OUTPUT_SPLIT_SPECIALTY.water },
  { label: '电气', value: OUTPUT_SPLIT_SPECIALTY.elec },
  { label: '暖通', value: OUTPUT_SPLIT_SPECIALTY.hvac },
  { label: '数字化设计', value: OUTPUT_SPLIT_SPECIALTY.digital }
]

export const SPECIALTY_ROLE_OPTIONS: Option[] = [
  { label: '专业负责人', value: OUTPUT_SPLIT_ROLE.director },
  { label: '校对', value: OUTPUT_SPLIT_ROLE.check },
  { label: '审核', value: OUTPUT_SPLIT_ROLE.review },
  { label: '审定', value: OUTPUT_SPLIT_ROLE.approve },
  { label: '设计', value: OUTPUT_SPLIT_ROLE.design }
]

export const isMajorOwnership = (value?: string) =>
  normalizeOwnershipType(value) === OWNERSHIP_TYPE.major
export const isComprehensiveOwnership = (value?: string) =>
  normalizeOwnershipType(value) === OWNERSHIP_TYPE.comprehensive
export const isSpecialSubcontractOwnership = (value?: string) =>
  normalizeOwnershipType(value) === OWNERSHIP_TYPE.specialSubcontract
export const isSourceCoopSubcontractOwnership = (value?: string) =>
  normalizeOwnershipType(value) === OWNERSHIP_TYPE.sourceCoopSubcontract
export const isComprehensiveSubcontractOwnership = (value?: string) =>
  normalizeOwnershipType(value) === OWNERSHIP_TYPE.comprehensiveSubcontract
export const isSubcontractOwnership = (value?: string) =>
  isSpecialSubcontractOwnership(value) ||
  isSourceCoopSubcontractOwnership(value) ||
  isComprehensiveSubcontractOwnership(value)

export const isGuidancePriceMethod = (value?: string) =>
  normalizeCalculationMethod(value) === CALCULATION_METHOD.guidancePrice
export const isContractPriceMethod = (value?: string) =>
  normalizeCalculationMethod(value) === CALCULATION_METHOD.contractPrice
export const isVirtualOutputMethod = (value?: string) =>
  normalizeCalculationMethod(value) === CALCULATION_METHOD.virtualOutput

export const isVirtualGuidanceMethod = (value?: string) =>
  normalizeVirtualCalculationMethod(value) === VIRTUAL_CALCULATION_METHOD.guidancePrice
export const isVirtualGuidanceTotalPriceMethod = (value?: string) =>
  normalizeVirtualCalculationMethod(value) === VIRTUAL_CALCULATION_METHOD.guidanceTotalPrice
export const isWorkingDayMethod = (value?: string) =>
  normalizeVirtualCalculationMethod(value) === VIRTUAL_CALCULATION_METHOD.workingDay

export const getOwnershipTypeLabel = (value?: string) =>
  findLabel(OWNERSHIP_TYPE_OPTIONS, normalizeOwnershipType(value)) || '-'

export const getCalculationMethodLabel = (value?: string) =>
  findLabel(CALCULATION_METHOD_OPTIONS, normalizeCalculationMethod(value)) || '-'

export const getProjectTypeLabel = (value?: string) =>
  findLabel(PROJECT_TYPE_OPTIONS, normalizeProjectType(value)) || '-'

export const getProjectCategoryLabel = (value?: string) =>
  findLabel(PROJECT_CATEGORY_OPTIONS, normalizeProjectCategory(value)) || '-'

export const getProjectStatusLabel = (value?: string) =>
  findLabel(PROJECT_STATUS_OPTIONS, normalizeProjectStatus(value)) || '-'

export const getDesignStageLabel = (value?: string) =>
  findLabel(DESIGN_STAGE_OPTIONS, normalizeDesignStage(value)) || '-'

export const getDesignPartLabel = (value?: string) =>
  findLabel(DESIGN_PART_OPTIONS, normalizeDesignPart(value)) || '-'

export const getVirtualCalculationMethodLabel = (value?: string) =>
  findLabel(VIRTUAL_CALCULATION_METHOD_OPTIONS, normalizeVirtualCalculationMethod(value)) || '-'

export const getCalculationRatioLabel = (ownershipType?: string) => {
  if (isComprehensiveOwnership(ownershipType)) {
    return '协作比例'
  }
  if (isSubcontractOwnership(ownershipType)) {
    return '分包比例'
  }
  return '比例'
}

export const getReviewOutsourceDefaultPercent = (
  ownershipType?: string,
  reviewOutsourceFlag?: boolean
) => {
  if (!reviewOutsourceFlag) {
    return 0
  }
  if (isMajorOwnership(ownershipType)) {
    return 6
  }
  return 25
}

export const getCalculationRatioDefaultPercent = (ownershipType?: string) => {
  if (isComprehensiveOwnership(ownershipType)) {
    return 8
  }
  if (isSubcontractOwnership(ownershipType)) {
    return 4
  }
  return undefined
}

export const toPercentValue = (value?: number | string | null, digits = 2) => {
  if (value === undefined || value === null || value === '') {
    return undefined
  }
  const numericValue = Number(value)
  if (Number.isNaN(numericValue)) {
    return undefined
  }
  return Number((numericValue * 100).toFixed(digits))
}

export const fromPercentValue = (value?: number | string | null, digits = 4) => {
  if (value === undefined || value === null || value === '') {
    return undefined
  }
  const numericValue = Number(value)
  if (Number.isNaN(numericValue)) {
    return undefined
  }
  return Number((numericValue / 100).toFixed(digits))
}

export const formatAmountText = (value?: number | string | null) => {
  if (value === undefined || value === null || value === '') {
    return '0.00'
  }
  const numericValue = Number(value)
  if (Number.isNaN(numericValue)) {
    return '0.00'
  }
  return numericValue.toFixed(2)
}

export const formatAreaText = (value?: number | string | null) => `${formatAmountText(value)} ㎡`

export const formatPercentText = (value?: number | string | null, digits = 2) => {
  const percentValue = toPercentValue(value, digits)
  return `${(percentValue ?? 0).toFixed(digits)}%`
}
