import { test } from '@japa/runner'
import { ApiModel, DataDomain } from '../../../src/index.js'

test.group('ApiModel.exposeEntity()', () => {
  test('exposes a new entity', ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const e1 = dm.addEntity()
    const model = new ApiModel()
    model.attachDataDomain(domain)
    const exposedEntity = model.exposeEntity({ key: e1.key })

    assert.isDefined(exposedEntity)
    assert.typeOf(exposedEntity.key, 'string')
    assert.deepEqual(exposedEntity.entity, { key: e1.key })
    assert.deepEqual(exposedEntity.actions, [])
  }).tags(['@modeling', '@api'])

  test('returns an existing entity if already exposed', ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const e1 = dm.addEntity()
    const model = new ApiModel()
    model.attachDataDomain(domain)
    const initialExposedEntity = model.exposeEntity({ key: e1.key })
    const retrievedExposedEntity = model.exposeEntity({ key: e1.key })

    assert.deepEqual(retrievedExposedEntity.toJSON(), initialExposedEntity.toJSON())
    assert.lengthOf(model.exposes, 1)
  }).tags(['@modeling', '@api'])

  test('notifies change when a new entity is exposed', async ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const e1 = dm.addEntity()
    const model = new ApiModel()
    model.attachDataDomain(domain)
    let notified = false
    model.addEventListener('change', () => {
      notified = true
    })
    model.exposeEntity({ key: e1.key })
    await Promise.resolve() // Allow microtask to run
    assert.isTrue(notified)
  }).tags(['@modeling', '@api'])

  test('does not notify change if entity already exposed', async ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const e1 = dm.addEntity()
    const model = new ApiModel()
    model.attachDataDomain(domain)
    model.exposeEntity({ key: e1.key }) // First exposure
    await Promise.resolve() // Allow microtask to run
    let notified = false
    model.addEventListener('change', () => {
      notified = true
    })
    model.exposeEntity({ key: e1.key }) // Second exposure
    await Promise.resolve() // Allow microtask to run
    assert.isFalse(notified)
  }).tags(['@modeling', '@api'])

  test('exposes nested entities through associations', ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const eA = dm.addEntity({ info: { name: 'A' } })
    const eB = dm.addEntity({ info: { name: 'B' } })
    // Add association from A to B
    eA.addAssociation({ key: eB.key }, { info: { name: 'entityB' } })
    const model = new ApiModel()
    model.attachDataDomain(domain)
    const exposedA = model.exposeEntity({ key: eA.key }, { followAssociations: true })
    // Find nested exposure for B
    const nestedB = model.exposes.find((e) => !e.isRoot && e.entity.key === eB.key)
    assert.isDefined(nestedB)
    assert.deepEqual(nestedB?.parent?.key, exposedA.key)
    assert.strictEqual(nestedB?.collectionPath, '/entitybs')
  })

  test('does not infinitely expose circular associations', ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const eA = dm.addEntity({ info: { name: 'A' } })
    const eB = dm.addEntity({ info: { name: 'B' } })
    // A -> B, B -> A
    eA.addAssociation({ key: eB.key }, { info: { name: 'assocAB' } })
    eB.addAssociation({ key: eA.key }, { info: { name: 'assocBA' } })
    const model = new ApiModel()
    model.attachDataDomain(domain)
    model.exposeEntity({ key: eA.key }, { followAssociations: true, maxDepth: 4 })

    assert.lengthOf(model.exposes, 2, 'has only 2 exposures')
    // Should expose A (root), B (nested under A), but not infinitely nest
    const exposedA = model.exposes.find((e) => e.isRoot && e.entity.key === eA.key)
    const nestedB = model.exposes.find((e) => !e.isRoot && e.entity.key === eB.key)
    // There should be only one nested exposure for B under A
    assert.isDefined(exposedA)
    assert.isDefined(nestedB)
    // There should NOT be a nested exposure for A under B
    const circularA = model.exposes.find((e) => !e.isRoot && e.entity.key === eA.key && e.parent?.key === nestedB?.key)
    assert.isUndefined(circularA)
  })

  test('does not expose self-association as nested entity', ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const eA = dm.addEntity({ info: { name: 'A' } })
    // A -> A (self-association)
    eA.addAssociation({ key: eA.key }, { info: { name: 'assocAA' } })
    const model = new ApiModel()
    model.attachDataDomain(domain)
    model.exposeEntity({ key: eA.key }, { followAssociations: true })
    // Should only expose A as root, not as nested
    const exposedA = model.exposes.find((e) => e.isRoot && e.entity.key === eA.key)
    const nestedA = model.exposes.find((e) => !e.isRoot && e.entity.key === eA.key)
    assert.isDefined(exposedA)
    assert.isUndefined(nestedA)
  })

  test('exposes multi-level associations (A -> B -> C)', ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const eA = dm.addEntity({ info: { name: 'A' } })
    const eB = dm.addEntity({ info: { name: 'B' } })
    const eC = dm.addEntity({ info: { name: 'C' } })
    eA.addAssociation({ key: eB.key }, { info: { name: 'toB' } })
    eB.addAssociation({ key: eC.key }, { info: { name: 'toC' } })
    const model = new ApiModel()
    model.attachDataDomain(domain)
    model.exposeEntity({ key: eA.key }, { followAssociations: true })
    const nestedB = model.exposes.find((e) => !e.isRoot && e.entity.key === eB.key)
    const nestedC = model.exposes.find((e) => !e.isRoot && e.entity.key === eC.key)
    assert.isDefined(nestedB)
    assert.isDefined(nestedC)
    assert.deepEqual(nestedB?.parent?.key, model.exposes.find((e) => e.isRoot && e.entity.key === eA.key)?.key)
    assert.deepEqual(nestedC?.parent?.key, nestedB?.key)
  })

  test('exposes multiple associations from one entity (A -> B, A -> C)', ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const eA = dm.addEntity({ info: { name: 'A' } })
    const eB = dm.addEntity({ info: { name: 'B' } })
    const eC = dm.addEntity({ info: { name: 'C' } })
    eA.addAssociation({ key: eB.key }, { info: { name: 'toB' } })
    eA.addAssociation({ key: eC.key }, { info: { name: 'toC' } })
    const model = new ApiModel()
    model.attachDataDomain(domain)
    model.exposeEntity({ key: eA.key }, { followAssociations: true })
    const nestedB = model.exposes.find((e) => !e.isRoot && e.entity.key === eB.key)
    const nestedC = model.exposes.find((e) => !e.isRoot && e.entity.key === eC.key)
    assert.isDefined(nestedB)
    assert.isDefined(nestedC)
    assert.deepEqual(nestedB?.parent?.key, model.exposes.find((e) => e.isRoot && e.entity.key === eA.key)?.key)
    assert.deepEqual(nestedC?.parent?.key, model.exposes.find((e) => e.isRoot && e.entity.key === eA.key)?.key)
  })

  test('respects maxDepth option (A -> B -> C -> D, maxDepth=2)', ({ assert }) => {
    const domain = new DataDomain()
    domain.info.version = '1.0.0'
    const dm = domain.addModel()
    const eA = dm.addEntity({ info: { name: 'A' } })
    const eB = dm.addEntity({ info: { name: 'B' } })
    const eC = dm.addEntity({ info: { name: 'C' } })
    const eD = dm.addEntity({ info: { name: 'D' } })
    eA.addAssociation({ key: eB.key }, { info: { name: 'toB' } })
    eB.addAssociation({ key: eC.key }, { info: { name: 'toC' } })
    eC.addAssociation({ key: eD.key }, { info: { name: 'toD' } })
    const model = new ApiModel()
    model.attachDataDomain(domain)
    model.exposeEntity({ key: eA.key }, { followAssociations: true, maxDepth: 2 })
    const nestedB = model.exposes.find((e) => !e.isRoot && e.entity.key === eB.key)
    const nestedC = model.exposes.find((e) => !e.isRoot && e.entity.key === eC.key)
    const nestedD = model.exposes.find((e) => !e.isRoot && e.entity.key === eD.key)
    assert.isDefined(nestedB)
    assert.isDefined(nestedC)
    assert.isUndefined(nestedD)
  })
})
