import chalk from 'chalk'
import {
  formatSecurityModeStatus,
  isSecurityModeEnabled,
} from '../../services/securityMode/securityMode.js'
import type { LocalCommandCall } from '../../types/command.js'
import { saveGlobalConfig } from '../../utils/config.js'

function usage(): string {
  return [
    'Usage:',
    '  /security-mode            Show current status',
    '  /security-mode on         Enable the strict preset',
    '  /security-mode off        Disable it',
  ].join('\n')
}

export const call: LocalCommandCall = async (args: string) => {
  const verb = args.trim().toLowerCase()

  if (verb === '' || verb === 'status') {
    return {
      type: 'text',
      value: chalk.dim(formatSecurityModeStatus(isSecurityModeEnabled())),
    }
  }

  if (verb === 'on' || verb === 'enable' || verb === 'true') {
    saveGlobalConfig((c) => ({ ...c, securityMode: true }))
    return {
      type: 'text',
      value: formatSecurityModeStatus(true),
    }
  }

  if (verb === 'off' || verb === 'disable' || verb === 'false' || verb === 'clear') {
    saveGlobalConfig((c) => ({ ...c, securityMode: false }))
    return {
      type: 'text',
      value: formatSecurityModeStatus(false),
    }
  }

  return { type: 'text', value: usage() }
}
