import { test } from '@japa/runner'
import { ApiSchemaGenerator } from '../../../src/amf/ApiSchemaGenerator.js'
import { IAmfDocument } from '../../../src/amf/definitions/Amf.js'
import { IApiAnyShape } from '../../../src/amf/definitions/Shapes.js'
import { AmfLoader } from './AmfLoader.js'

test.group('Tracked examples', (group) => {
  const loader = new AmfLoader()
  const jsonMime = 'application/json'
  const xmlMime = 'application/xml'
  let model: IAmfDocument

  group.setup(async () => {
    model = await loader.getGraph('tracked-to-linked')
  })

  test('basic formatting and type name', ({ assert }) => {
    const payload = loader.getPayloads(model, '/employees', 'get')[0]

    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, jsonMime, {
      renderExamples: true,
      renderOptional: true,
    })!

    assert.equal(result.value, '-\n  id: 1\n  name: John\n-\n  id: 2\n  name: Sam', "Example's raw is set")

    const parsed = JSON.parse(result.renderValue as string)

    assert.typeOf(parsed, 'array', 'represents an array')
    assert.lengthOf(parsed, 2, 'has 2 items')

    const [e1, e2] = parsed
    assert.equal(e1.id, 1, 'has the example1.id')
    assert.equal(e1.name, 'John', 'has the example1.name')
    assert.equal(e2.id, 2, 'has the example2.id')
    assert.equal(e2.name, 'Sam', 'has the example2.name')
  })

  test('generates example for POST', ({ assert }) => {
    const payload = loader.getPayloads(model, '/employees', 'post')[0]

    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, jsonMime, {
      renderExamples: true,
      renderOptional: true,
    })!

    assert.equal(result.value, 'id: 1\nname: "John"', "Example's raw is set")

    const parsed = JSON.parse(result.renderValue as string)

    assert.deepEqual(parsed, {
      id: 1,
      name: 'John',
    })
  })

  test('generates example for DELETE', ({ assert }) => {
    const payload = loader.getPayloads(model, '/employees', 'delete')[0]

    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, jsonMime, {
      renderExamples: true,
      renderOptional: true,
    })!

    assert.equal(result.value, 'id: 1\nname: "John"', "Example's raw is set")

    const parsed = JSON.parse(result.renderValue as string)

    assert.deepEqual(parsed, {
      id: 1,
      name: 'John',
    })
  })

  test('generates example for HEAD', ({ assert }) => {
    const payload = loader.getPayloads(model, '/employees', 'head')[0]

    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, xmlMime, {
      renderExamples: true,
      renderOptional: true,
    })!

    assert.equal(result.value, 'id: 1\nname: "John"', "Example's raw is set")
    assert.equal(result.renderValue, '<Employee>\n  <id>1</id>\n  <name>John</name>\n</Employee>\n')
  })
})
