import type { Command } from '../commands.js'
import type { LocalCommandCall } from '../types/command.js'
import {
  formatTrustStatus,
  resetTrust,
} from '../services/trustEngine/engine.js'
import { getCwd } from '../utils/cwd.js'

const call: LocalCommandCall = async (args) => {
  const arg = args.trim().toLowerCase()

  if (arg === 'reset') {
    await resetTrust(getCwd())
    return {
      type: 'text',
      value: 'Trust scores reset for this project.',
    }
  }

  if (!arg || arg === 'status') {
    const status = await formatTrustStatus(getCwd())
    return { type: 'text', value: status }
  }

  return {
    type: 'text',
    value: 'Usage: /trust [status|reset]',
  }
}

const trust = {
  type: 'local',
  name: 'trust',
  description: 'View or reset trust scores for auto-approval',
  argumentHint: '[status|reset]',
  supportsNonInteractive: true,
  load: () => Promise.resolve({ call }),
} satisfies Command

export default trust
