import request from './request'

export interface CategoryItem {
  id: number
  parent_id?: number
  level: number
  code: string
  name: string
  description?: string
  sort_order: number
  children?: CategoryItem[]
}

export interface DataLevel {
  id: number
  code: string
  name: string
  description?: string
  color: string
  control_requirements?: Record<string, any>
}

export interface RecognitionRule {
  id: number
  template_id: number
  category_id?: number
  level_id?: number
  rule_type: string
  rule_name?: string
  rule_content: string
  target_field: string
  priority: number
  is_active: boolean
  hit_count: number
  category_name?: string
  level_name?: string
  level_color?: string
}

export function getCategoryTree() {
  return request.get('/classifications/categories/tree')
}

export function createCategory(data: Partial<CategoryItem>) {
  return request.post('/classifications/categories', data)
}

export function updateCategory(id: number, data: Partial<CategoryItem>) {
  return request.put(`/classifications/categories/${id}`, data)
}

export function deleteCategory(id: number) {
  return request.delete(`/classifications/categories/${id}`)
}

export function getDataLevels() {
  return request.get('/classifications/levels')
}

export function getRules(params?: { template_id?: number; keyword?: string; page?: number; page_size?: number }) {
  return request.get('/classifications/rules', { params })
}

export function createRule(data: Partial<RecognitionRule>) {
  return request.post('/classifications/rules', data)
}

export function updateRule(id: number, data: Partial<RecognitionRule>) {
  return request.put(`/classifications/rules/${id}`, data)
}

export function deleteRule(id: number) {
  return request.delete(`/classifications/rules/${id}`)
}

export function getTemplates() {
  return request.get('/classifications/templates')
}
