import { describe, expect, it } from 'vitest'
import {
  mergeAskUserQuestions,
  normalizeAskUserQuestions,
} from '../src/dimsdk/ask-user.ts'

describe('dimsdk ask-user helpers', () => {
  it('normalizes string options and preserves question ids', () => {
    expect(
      normalizeAskUserQuestions([
        {
          id: 'meal',
          question: 'Tonight?',
          options: ['Spicy', { label: 'Light', description: 'Less oil' }, ''],
        },
        {
          question: 'Drink?',
          multiSelect: true,
          options: ['Tea'],
        },
      ]),
    ).toEqual([
      {
        allowCustom: true,
        id: 'meal',
        header: '',
        kind: 'choice',
        multiline: false,
        placeholder: '',
        question: 'Tonight?',
        options: [
          { label: 'Spicy' },
          { label: 'Light', description: 'Less oil' },
        ],
        multiSelect: false,
      },
      {
        allowCustom: true,
        id: 'answer_2',
        header: '',
        kind: 'choice',
        multiline: false,
        placeholder: '',
        question: 'Drink?',
        options: [{ label: 'Tea' }],
        multiSelect: true,
      },
    ])
  })

  it('normalizes a single ask-user question object and bare string question', () => {
    expect(
      normalizeAskUserQuestions({
        id: 'meal',
        question: 'Tonight?',
        options: ['Soup', 'Noodles'],
      }),
    ).toEqual([
      {
        allowCustom: true,
        id: 'meal',
        header: '',
        kind: 'choice',
        multiline: false,
        placeholder: '',
        question: 'Tonight?',
        options: [{ label: 'Soup' }, { label: 'Noodles' }],
        multiSelect: false,
      },
    ])

    expect(normalizeAskUserQuestions('Tonight?')).toEqual([
      {
        allowCustom: true,
        id: 'answer',
        header: '',
        kind: 'input',
        multiline: false,
        placeholder: '',
        question: 'Tonight?',
        options: [],
        multiSelect: false,
      },
    ])
  })

  it('normalizes wrapped question payloads without trusting malformed entries', () => {
    expect(
      normalizeAskUserQuestions({
        questions: [
          {
            id: 'meal',
            header: 'Dinner',
            question: 'Tonight?',
            options: [
              { label: 'Soup' },
              { label: 'Rice', description: 'Plain' },
              { label: '' },
              null,
            ],
          },
          null,
        ],
      }),
    ).toEqual([
      {
        allowCustom: true,
        id: 'meal',
        header: 'Dinner',
        kind: 'choice',
        multiline: false,
        placeholder: '',
        question: 'Tonight?',
        options: [
          { label: 'Soup' },
          { label: 'Rice', description: 'Plain' },
        ],
        multiSelect: false,
      },
    ])
  })

  it('preserves explicit input metadata and custom-choice flags', () => {
    expect(
      normalizeAskUserQuestions([
        {
          id: 'notes',
          kind: 'input',
          question: 'What should we avoid?',
          placeholder: 'No cilantro',
          multiline: true,
        },
        {
          id: 'meal',
          question: 'Choose dinner',
          allowCustom: false,
          options: ['Soup', 'Noodles'],
        },
      ]),
    ).toEqual([
      {
        allowCustom: true,
        id: 'notes',
        header: '',
        kind: 'input',
        multiline: true,
        placeholder: 'No cilantro',
        question: 'What should we avoid?',
        options: [],
        multiSelect: false,
      },
      {
        allowCustom: false,
        id: 'meal',
        header: '',
        kind: 'choice',
        multiline: false,
        placeholder: '',
        question: 'Choose dinner',
        options: [{ label: 'Soup' }, { label: 'Noodles' }],
        multiSelect: false,
      },
    ])
  })

  it('backfills incomplete paused questions from the original tool call', () => {
    expect(
      mergeAskUserQuestions(
        [
          {
            id: 'meal',
            question: 'Choose dinner',
            options: [],
          },
        ],
        [
          {
            id: 'meal',
            kind: 'choice',
            question: 'Choose dinner',
            allowCustom: false,
            options: ['Soup', 'Noodles'],
          },
        ],
      ),
    ).toEqual([
      {
        allowCustom: false,
        id: 'meal',
        header: '',
        kind: 'choice',
        multiline: false,
        placeholder: '',
        question: 'Choose dinner',
        options: [{ label: 'Soup' }, { label: 'Noodles' }],
        multiSelect: false,
      },
    ])
  })
})
