import { describe, it, expect, beforeEach } from 'vitest'
import {
  updateFromCot,
  getActiveEntities,
  getActiveEntitiesInBbox,
  clearCotStore,
  onCotChange,
  pruneStaleEntities,
} from '../../../server/utils/cotStore.js'

describe('cotStore', () => {
  beforeEach(() => {
    clearCotStore()
  })

  it('upserts entity by id', async () => {
    await updateFromCot({ id: 'uid-1', lat: 37.7, lng: -122.4, label: 'Alpha' })
    const active = await getActiveEntities()
    expect(active).toHaveLength(1)
    expect(active[0].id).toBe('uid-1')
    expect(active[0].lat).toBe(37.7)
    expect(active[0].lng).toBe(-122.4)
    expect(active[0].label).toBe('Alpha')
    expect(active[0].source).toBe('tak')
  })

  it('stores enriched ADS-B fields and infers source', async () => {
    await updateFromCot({
      id: 'ICAO.abc123',
      lat: 37.7,
      lng: -122.4,
      label: 'UAL1',
      type: 'a-f-A-C-F',
      heading: 90,
      speed: 200,
      altitude: 10000,
    })
    const active = await getActiveEntities()
    expect(active[0]).toMatchObject({
      source: 'adsb',
      heading: 90,
      speed: 200,
      altitude: 10000,
      type: 'a-f-A-C-F',
    })
  })

  it('updates same uid', async () => {
    await updateFromCot({ id: 'uid-1', lat: 37.7, lng: -122.4 })
    await updateFromCot({ id: 'uid-1', lat: 38, lng: -123, label: 'Updated' })
    const active = await getActiveEntities()
    expect(active).toHaveLength(1)
    expect(active[0].lat).toBe(38)
    expect(active[0].lng).toBe(-123)
    expect(active[0].label).toBe('Updated')
  })

  it('ignores invalid parsed (no id)', async () => {
    await updateFromCot({ lat: 37, lng: -122 })
    const active = await getActiveEntities()
    expect(active).toHaveLength(0)
  })

  it('ignores invalid parsed (bad coords)', async () => {
    await updateFromCot({ id: 'x', lat: Number.NaN, lng: -122 })
    await updateFromCot({ id: 'y', lat: 37, lng: Infinity })
    const active = await getActiveEntities()
    expect(active).toHaveLength(0)
  })

  it('prunes expired entities after getActiveEntities', async () => {
    await updateFromCot({ id: 'uid-1', lat: 37, lng: -122 })
    const active1 = await getActiveEntities({ ttlMs: 100 })
    expect(active1).toHaveLength(1)
    await new Promise(r => setTimeout(r, 150))
    const active2 = await getActiveEntities({ ttlMs: 100 })
    expect(active2).toHaveLength(0)
  })

  it('returns multiple active entities within TTL', async () => {
    await updateFromCot({ id: 'a', lat: 1, lng: 2, label: 'A' })
    await updateFromCot({ id: 'b', lat: 3, lng: 4, label: 'B' })
    const active = await getActiveEntities()
    expect(active).toHaveLength(2)
    expect(active.map(e => e.id).sort()).toEqual(['a', 'b'])
  })

  it('filters OSINT entities by bbox but keeps team globally', async () => {
    await updateFromCot({ id: 'ICAO.abc', lat: 37.5, lng: -122.5, type: 'a-f-A-C-F' })
    await updateFromCot({ id: 'MMSI.123', lat: 40, lng: -100, type: 'a-f-S-C' })
    await updateFromCot({ id: 'ANDROID-1', lat: 50, lng: 10, source: 'tak' })
    const bbox = { west: -123, south: 37, east: -122, north: 38 }
    const active = await getActiveEntitiesInBbox(bbox, { takFilterBbox: false })
    const ids = active.map(e => e.id).sort()
    expect(ids).toEqual(['ANDROID-1', 'ICAO.abc'])
  })

  it('filters team by bbox when COT_TAK_FILTER_BBOX enabled', async () => {
    await updateFromCot({ id: 'ANDROID-1', lat: 50, lng: 10, source: 'tak' })
    const bbox = { west: -123, south: 37, east: -122, north: 38 }
    const active = await getActiveEntitiesInBbox(bbox, { takFilterBbox: true })
    expect(active).toHaveLength(0)
  })

  it('caps bbox query results at maxEntities', async () => {
    for (let i = 0; i < 5; i++) {
      await updateFromCot({ id: `ICAO.${i}`, lat: 37.5 + i * 0.01, lng: -122.4, type: 'a-f-A-C-F' })
    }
    const bbox = { west: -123, south: 37, east: -122, north: 38 }
    const active = await getActiveEntitiesInBbox(bbox, { maxEntities: 3 })
    expect(active).toHaveLength(3)
  })

  it('skips emit when silent upsert', async () => {
    const updates = []
    const off = onCotChange((event) => {
      if (event === 'update') updates.push(event)
    })
    await updateFromCot({ id: 'ICAO.silent', lat: 37, lng: -122, type: 'a-f-A-C-F' }, { silent: true })
    off()
    expect(updates).toHaveLength(0)
    const active = await getActiveEntities()
    expect(active.some(e => e.id === 'ICAO.silent')).toBe(true)
  })

  it('pruneStaleEntities uses shorter TTL for OSINT sources', async () => {
    await updateFromCot({ id: 'ICAO.old', lat: 37, lng: -122, source: 'adsb', type: 'a-f-A-C-F' })
    await updateFromCot({ id: 'ANDROID-1', lat: 37, lng: -122, source: 'tak' })
    const removed = []
    const off = onCotChange((event, payload) => {
      if (event === 'remove') removed.push(payload.id)
    })
    await new Promise(r => setTimeout(r, 60))
    await pruneStaleEntities({ ttlMs: 10_000, osintTtlMs: 50 })
    off()
    expect(removed).toContain('ICAO.old')
    expect(removed).not.toContain('ANDROID-1')
  })
})
