import axios from './instance'
import { GET_TAGGING_LIST, GET_TAGGING_TOTAL } from './constant'
import type { PageResult, PageRequest, Project } from './'
import { ui18n } from '@/lang'

export enum TaggingStatus {
  pending = 1,
  progress = 2,
  unsolved = 3,
  solved = 4
}

export const TaggingStatusDesc = {
  [TaggingStatus.pending]: ui18n.t('project.taggingStatusDesc.pending'),
  [TaggingStatus.progress]: ui18n.t('project.taggingStatusDesc.progress'),
  [TaggingStatus.unsolved]: ui18n.t('project.taggingStatusDesc.unsolved'),
  [TaggingStatus.solved]: ui18n.t('project.taggingStatusDesc.solved')
}

export interface Tagging {
  markingStatus: TaggingStatus
  markingTitle: string
  markingId: number
  lastUpdateBy: string
  createBy: string
  updateTime: string
  users: { userId: number; nickName: string }[]
  remark: string
}

export type Taggings = Tagging[]

export type FetchTaggingsProps = PageRequest<{
  markingTitle: Tagging['markingTitle']
  projectId: Project['projectId']
  markingStatus?: Tagging['markingStatus']
}>
export const fetchTaggings = (params: FetchTaggingsProps) =>
  axios.post<PageResult<Tagging>>(GET_TAGGING_LIST, params)

export type FetchTaggingsTotalResult = {
  count: number
  markingStatus: TaggingStatus
}[]

export const fetchTaggingsTotal = async (id: Project['projectId']) => {
  const data = await axios.post<FetchTaggingsTotalResult>(GET_TAGGING_TOTAL, {
    projectId: id
  })
  const totals: { [key in TaggingStatus]: number } = {
    [TaggingStatus.pending]: 0,
    [TaggingStatus.progress]: 0,
    [TaggingStatus.solved]: 0,
    [TaggingStatus.unsolved]: 0
  }
  data.forEach(item => {
    if (item.markingStatus) {
      totals[item.markingStatus] = item.count
    }
  })
  return totals
}
