const SESSION_TYPE_APPLICATION = 'application'
const SESSION_TYPE_APPROVAL = 'approval'
const SESSION_TYPE_BUDGET = 'budget'
const SESSION_TYPE_EXPENSE = 'expense'
const SESSION_TYPE_KNOWLEDGE = 'knowledge'

const DEFAULT_WORKBENCH_ENTRY = {
  source: 'workbench',
  sessionType: SESSION_TYPE_EXPENSE
}

const CAPABILITY_ASSISTANT_ENTRIES = {
  'expense-application': {
    source: 'application',
    sessionType: SESSION_TYPE_APPLICATION
  },
  'quick-reimbursement': DEFAULT_WORKBENCH_ENTRY,
  'budget-planning': {
    source: 'budget',
    sessionType: SESSION_TYPE_BUDGET
  },
  'quick-approval': {
    source: 'workbench',
    sessionType: SESSION_TYPE_APPROVAL
  },
  'finance-analysis': {
    source: 'budget',
    sessionType: SESSION_TYPE_BUDGET
  },
  'company-policy': {
    source: 'workbench',
    sessionType: SESSION_TYPE_KNOWLEDGE
  }
}

export function resolveWorkbenchCapabilityAssistantEntry(item = {}) {
  const key = String(item?.key || '').trim()
  const entry = CAPABILITY_ASSISTANT_ENTRIES[key] || DEFAULT_WORKBENCH_ENTRY

  return { ...entry }
}

export function buildWorkbenchCapabilityAssistantPayload(item = {}, basePayload = {}) {
  const entry = resolveWorkbenchCapabilityAssistantEntry(item)

  return {
    ...basePayload,
    ...entry,
    prompt: String(basePayload.prompt || '').trim(),
    files: Array.isArray(basePayload.files) ? basePayload.files : [],
    conversation: null
  }
}
