import type { BrandAlignmentScoreResult } from '@actuate-media/cms-core/brand-voice'
import { cmsApi } from './api.js'

export type { BrandAlignmentScoreResult }

export interface BrandVoiceFixResult {
  text: string
  deterministicFixes: string[]
  beforeScore: BrandAlignmentScoreResult
  afterScore: BrandAlignmentScoreResult
  usedAi: boolean
}

export async function fixBrandVoiceContent(input: {
  content: string
  channel?: string
  useAi?: boolean
}): Promise<{ ok: boolean; result?: BrandVoiceFixResult; error?: string }> {
  const res = await cmsApi<BrandVoiceFixResult>('/ai/brand-voice/fix', {
    method: 'POST',
    body: JSON.stringify(input),
  })
  if (res.error) return { ok: false, error: res.error }
  if (!res.data?.text) return { ok: false, error: 'No fixed copy was returned.' }
  return { ok: true, result: res.data }
}

export function brandAlignmentLabel(score: number): string {
  if (score >= 85) return 'On brand'
  if (score >= 65) return 'Mostly on brand'
  return 'Off brand'
}

export function brandAlignmentTone(score: number): string {
  if (score >= 85) return 'text-success'
  if (score >= 65) return 'text-warning'
  return 'text-destructive'
}
