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')
}

export interface ClassificationResultItem {
  id: number
  project_id: number
  column_id?: number
  column_name?: string
  data_type?: string
  comment?: string
  table_name?: string
  database_name?: string
  source_name?: string
  category_id?: number
  category_name?: string
  level_id?: number
  level_name?: string
  level_color?: string
  source: string
  confidence: number
  status: string
  created_at?: string
}

export function getClassificationResults(params: {
  project_id?: number
  level_id?: number
  keyword?: string
  page?: number
  page_size?: number
}) {
  return request.get('/classifications/results', { params })
}

export interface MLSuggestion {
  column_id: number
  column_name: string
  table_name?: string
  suggestions: {
    category_id: number
    category_name?: string
    category_code?: string
    confidence: number
  }[]
}

export function getMLSuggestions(project_id: number, column_ids?: number[], top_k: number = 3) {
  const params: any = { top_k }
  if (column_ids && column_ids.length) {
    params.column_ids = column_ids.join(',')
  }
  return request.get(`/classifications/ml-suggest/${project_id}`, { params })
}

export function trainMLModel(background: boolean = true, model_name?: string, algorithm: string = 'logistic_regression') {
  return request.post('/classifications/ml-train', null, { params: { background, model_name, algorithm } })
}
