import request from '@/utils/request'
import type {
  ForbiddenWord,
  ForbiddenWordCreate,
  ForbiddenWordUpdate,
  ForbiddenWordListResponse,
  BehaviorRule,
  BehaviorRuleCreate,
  BehaviorRuleUpdate,
  BehaviorRuleListResponse
} from '@/types/guardrail'

export interface GuardrailTestRequest {
  testTexts: string[]
}

export interface GuardrailTestResponse {
  results: GuardrailTestResult[]
  summary: {
    totalTests: number
    triggeredCount: number
    blockedCount: number
    triggerRate: number
  }
}

export interface GuardrailTestResult {
  originalText: string
  triggered: boolean
  triggeredWords: TriggeredWordInfo[]
  filteredText: string
  blocked: boolean
}

export interface TriggeredWordInfo {
  word: string
  category: string
  strategy: string
  replacement?: string
  fallbackReply?: string
}

export function listForbiddenWords(params?: {
  category?: string
  is_enabled?: boolean
}): Promise<ForbiddenWordListResponse> {
  return request({
    url: '/admin/guardrails/forbidden-words',
    method: 'get',
    params
  })
}

export function getForbiddenWord(wordId: string): Promise<ForbiddenWord> {
  return request({
    url: `/admin/guardrails/forbidden-words/${wordId}`,
    method: 'get'
  })
}

export function createForbiddenWord(data: ForbiddenWordCreate): Promise<ForbiddenWord> {
  return request({
    url: '/admin/guardrails/forbidden-words',
    method: 'post',
    data
  })
}

export function updateForbiddenWord(wordId: string, data: ForbiddenWordUpdate): Promise<ForbiddenWord> {
  return request({
    url: `/admin/guardrails/forbidden-words/${wordId}`,
    method: 'put',
    data
  })
}

export function deleteForbiddenWord(wordId: string): Promise<void> {
  return request({
    url: `/admin/guardrails/forbidden-words/${wordId}`,
    method: 'delete'
  })
}

export function listBehaviorRules(params?: {
  category?: string
}): Promise<BehaviorRuleListResponse> {
  return request({
    url: '/admin/guardrails/behavior-rules',
    method: 'get',
    params
  })
}

export function getBehaviorRule(ruleId: string): Promise<BehaviorRule> {
  return request({
    url: `/admin/guardrails/behavior-rules/${ruleId}`,
    method: 'get'
  })
}

export function createBehaviorRule(data: BehaviorRuleCreate): Promise<BehaviorRule> {
  return request({
    url: '/admin/guardrails/behavior-rules',
    method: 'post',
    data
  })
}

export function updateBehaviorRule(ruleId: string, data: BehaviorRuleUpdate): Promise<BehaviorRule> {
  return request({
    url: `/admin/guardrails/behavior-rules/${ruleId}`,
    method: 'put',
    data
  })
}

export function deleteBehaviorRule(ruleId: string): Promise<void> {
  return request({
    url: `/admin/guardrails/behavior-rules/${ruleId}`,
    method: 'delete'
  })
}

export function testGuardrail(data: GuardrailTestRequest): Promise<GuardrailTestResponse> {
  return request({
    url: '/admin/guardrails/test',
    method: 'post',
    data
  })
}

export type {
  ForbiddenWord,
  ForbiddenWordCreate,
  ForbiddenWordUpdate,
  ForbiddenWordListResponse,
  BehaviorRule,
  BehaviorRuleCreate,
  BehaviorRuleUpdate,
  BehaviorRuleListResponse
}
