import { test } from '@japa/runner'
import { MatchUserPropertyAccessRule } from '../../../../src/modeling/rules/MatchUserProperty.js'

test.group('MatchUserPropertyAccessRule', () => {
  test('initializes with default values', ({ assert }) => {
    const rule = new MatchUserPropertyAccessRule()
    assert.equal(rule.type, 'matchUserProperty')
    assert.equal(rule.property, '')
    assert.equal(rule.value, '')
  }).tags(['@modeling', '@rule', '@match-user-property'])

  test('initializes with provided values', ({ assert }) => {
    const rule = new MatchUserPropertyAccessRule({
      property: 'department',
      value: 'engineering',
    })

    assert.equal(rule.type, 'matchUserProperty')
    assert.equal(rule.property, 'department')
    assert.equal(rule.value, 'engineering')
  }).tags(['@modeling', '@rule', '@match-user-property'])

  test('toJSON returns valid schema', ({ assert }) => {
    const rule = new MatchUserPropertyAccessRule({
      property: 'department',
      value: 'engineering',
    })
    const json = rule.toJSON()

    assert.equal(json.type, 'matchUserProperty')
    assert.equal(json.property, 'department')
    assert.equal(json.value, 'engineering')
  }).tags(['@modeling', '@rule', '@match-user-property', '@serialization'])

  test('notifies change when property changes', async ({ assert }) => {
    const rule = new MatchUserPropertyAccessRule()
    let notified = false
    rule.addEventListener('change', () => {
      notified = true
    })

    rule.property = 'dept'
    await Promise.resolve()
    assert.isTrue(notified)
  }).tags(['@modeling', '@rule', '@match-user-property', '@observed'])

  test('notifies change when value changes', async ({ assert }) => {
    const rule = new MatchUserPropertyAccessRule()
    let notified = false
    rule.addEventListener('change', () => {
      notified = true
    })

    rule.value = 'sales'
    await Promise.resolve()
    assert.isTrue(notified)
  }).tags(['@modeling', '@rule', '@match-user-property', '@observed'])
})
