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

test.group('APIC-187', (group) => {
  const loader = new AmfLoader()
  const jsonMime = 'application/json'
  const apiFile = 'APIC-187'

  let model: IAmfDocument

  group.setup(async () => {
    model = await loader.getGraph(apiFile)
  })
  ;[
    ['AS400CityName', 'string', 'Omaha, DG'],
    ['AS400ECM', 'number', 401571.6],
    ['AcquiredDate', 'string', '2019-01-16'],
    ['Age', 'number', 1],
    ['TransmissionRearRearSN', 'string', '1'],
    ['TransmissionRearRatio', 'number', 1],
    ['Weight', 'number', 9300],
    ['Year', 'string', '2016'],
  ].forEach((item) => {
    test(`returns ${item[1]} type (${item[0]})`, ({ assert }) => {
      const datatype = item[1] as string
      const payload = loader.getPayloads(model, '/record', 'post')[0]
      const anyShape = payload.schema as IShapeUnion
      const result = ApiSchemaGenerator.asExample(anyShape, jsonMime, {
        renderExamples: true,
        renderOptional: true,
      })!

      const data = JSON.parse(String(result.renderValue))
      assert.typeOf(data.records[0][item[0]], datatype, 'Data type matches')
      assert.equal(data.records[0][item[0]], item[2], 'Value matches')
    })
  })
})
