import { test } from '@japa/runner'
import { DataValueGenerator, noValue } from '../../../src/amf/DataValueGenerator.js'
import type { DomainPropertyType } from '../../../src/modeling/DataFormat.js'

test.group('DataValueGenerator', (group) => {
  let generator: DataValueGenerator

  group.each.setup(() => {
    generator = new DataValueGenerator()
  })

  test('generate() returns noValue for unsupported types', ({ assert }) => {
    const result = generator.generate('random', 'unknown' as DomainPropertyType)
    assert.equal(result, noValue)
  })

  test('generateString() returns random string for "random" function', ({ assert }) => {
    const result = generator.generate('random', 'string')
    assert.typeOf(result, 'string')
    assert.lengthOf(result as string, 8)
  })

  test('generateString() returns uuid for "uuid-v4" function', ({ assert }) => {
    const result = generator.generate('uuid-v4', 'string')
    assert.typeOf(result, 'string')
    // Basic UUID validation -  check length and some characters
    assert.lengthOf(result as string, 36)
    assert.match(result as string, /^[0-9a-f-]+$/)
  })

  test('generateString() returns noValue for unsupported function', ({ assert }) => {
    const result = generator.generate('unsupported', 'string')
    assert.equal(result, noValue)
  })

  test('generateNumber() returns incremental number for "incremental" function', ({ assert }) => {
    const result1 = generator.generate('incremental', 'number')
    const result2 = generator.generate('incremental', 'number')
    assert.typeOf(result1, 'string')
    assert.typeOf(result2, 'string')
    assert.equal(parseInt(result1 as string), 0)
    assert.equal(parseInt(result2 as string), 1)
  })

  test('generateNumber() returns noValue for unsupported function', ({ assert }) => {
    const result = generator.generate('unsupported', 'number')
    assert.equal(result, noValue)
  })

  test('generateDate() returns current date for "now" function', ({ assert }) => {
    const result = generator.generate('now', 'date')
    assert.typeOf(result, 'string')
    assert.match(result as string, /^\d{4}-\d{2}-\d{2}$/)
  })

  test('generateDate() returns noValue for unsupported function', ({ assert }) => {
    const result = generator.generate('unsupported', 'date')
    assert.equal(result, noValue)
  })

  test('generateTime() returns current time for "now" function', ({ assert }) => {
    const result = generator.generate('now', 'time')
    assert.typeOf(result, 'string')
    assert.match(result as string, /^\d{2}:\d{2}:\d{2}$/)
  })

  test('generateTime() returns noValue for unsupported function', ({ assert }) => {
    const result = generator.generate('unsupported', 'time')
    assert.equal(result, noValue)
  })

  test('generateDateTime() returns current datetime for "now" function', ({ assert }) => {
    const result = generator.generate('now', 'datetime')
    assert.typeOf(result, 'string')
    assert.match(result as string, /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)
  })

  test('generateDateTime() returns noValue for unsupported function', ({ assert }) => {
    const result = generator.generate('unsupported', 'datetime')
    assert.equal(result, noValue)
  })
})
