import { describe, it, expect, beforeEach } from 'vitest'
import { useCompareStore } from '../compareStore'
import type { UnifiedMatchResult } from '../../domain/unifiedResult'

function makeResult(matchId: string, score = 80): UnifiedMatchResult {
  return {
    matchId,
    needId: 'need-1',
    matchScore: score,
    resultType: 'VERIFIED_PORTFOLIO',
    property: { id: 'prop-1' } as never,
    match: { id: matchId } as never,
  }
}

describe('compareStore', () => {
  beforeEach(() => {
    useCompareStore.setState({ compareItems: [] })
  })

  describe('addToCompare', () => {
    it('adds a result', () => {
      useCompareStore.getState().addToCompare(makeResult('m-1'))
      expect(useCompareStore.getState().compareItems).toHaveLength(1)
    })

    it('does not add duplicate matchId', () => {
      const r = makeResult('m-1')
      useCompareStore.getState().addToCompare(r)
      useCompareStore.getState().addToCompare(r)
      expect(useCompareStore.getState().compareItems).toHaveLength(1)
    })

    it('enforces max 4 items', () => {
      for (let i = 1; i <= 5; i++) {
        useCompareStore.getState().addToCompare(makeResult(`m-${i}`))
      }
      expect(useCompareStore.getState().compareItems).toHaveLength(4)
    })
  })

  describe('removeFromCompare', () => {
    it('removes item by matchId', () => {
      useCompareStore.getState().addToCompare(makeResult('m-1'))
      useCompareStore.getState().addToCompare(makeResult('m-2'))
      useCompareStore.getState().removeFromCompare('m-1')
      expect(useCompareStore.getState().compareItems.map(i => i.matchId)).toEqual(['m-2'])
    })

    it('is a no-op for unknown matchId', () => {
      useCompareStore.getState().addToCompare(makeResult('m-1'))
      useCompareStore.getState().removeFromCompare('m-x')
      expect(useCompareStore.getState().compareItems).toHaveLength(1)
    })
  })

  describe('clearCompare', () => {
    it('empties the list', () => {
      useCompareStore.getState().addToCompare(makeResult('m-1'))
      useCompareStore.getState().addToCompare(makeResult('m-2'))
      useCompareStore.getState().clearCompare()
      expect(useCompareStore.getState().compareItems).toHaveLength(0)
    })
  })

  describe('isInCompare', () => {
    it('returns true when present', () => {
      useCompareStore.getState().addToCompare(makeResult('m-1'))
      expect(useCompareStore.getState().isInCompare('m-1')).toBe(true)
    })

    it('returns false when absent', () => {
      expect(useCompareStore.getState().isInCompare('m-x')).toBe(false)
    })
  })

  describe('isFull', () => {
    it('returns false when fewer than 4 items', () => {
      useCompareStore.getState().addToCompare(makeResult('m-1'))
      expect(useCompareStore.getState().isFull()).toBe(false)
    })

    it('returns true when 4 items are present', () => {
      for (let i = 1; i <= 4; i++) useCompareStore.getState().addToCompare(makeResult(`m-${i}`))
      expect(useCompareStore.getState().isFull()).toBe(true)
    })
  })
})
