import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'
import { useFeedbackStore } from '@/stores/feedback'

describe('useFeedbackStore', () => {
  beforeEach(() => {
    localStorage.clear()
    setActivePinia(createPinia())
  })

  afterEach(() => {
    vi.restoreAllMocks()
  })

  it('addFeedback_createsEntry', () => {
    const store = useFeedbackStore()
    store.addFeedback('a1', 'SepsisWarning', 'Critical', 'useful')

    expect(store.entries).toHaveLength(1)
    expect(store.entries[0].alertId).toBe('a1')
    expect(store.entries[0].rating).toBe('useful')
  })

  it('addFeedback_updatesExisting', () => {
    const store = useFeedbackStore()
    store.addFeedback('a1', 'SepsisWarning', 'Critical', 'useful')
    store.addFeedback('a1', 'SepsisWarning', 'Critical', 'false-positive')

    expect(store.entries).toHaveLength(1)
    expect(store.entries[0].rating).toBe('false-positive')
  })

  it('stats_computesCorrectly', () => {
    const store = useFeedbackStore()
    store.addFeedback('a1', 'SepsisWarning', 'Critical', 'useful')
    store.addFeedback('a2', 'SepsisWarning', 'Critical', 'would-act')
    store.addFeedback('a3', 'WarningHeartRate', 'Warning', 'false-positive')
    store.addFeedback('a4', 'WarningHeartRate', 'Warning', 'too-early')

    expect(store.stats.total).toBe(4)
    expect(store.stats.useful).toBe(2)
    expect(store.stats.falsePositive).toBe(1)
    expect(store.stats.usefulPct).toBe(50)
    expect(store.stats.fpPct).toBe(25)
  })

  it('byAlertType_groupsCorrectly', () => {
    const store = useFeedbackStore()
    store.addFeedback('a1', 'SepsisWarning', 'Critical', 'useful')
    store.addFeedback('a2', 'WarningHeartRate', 'Warning', 'false-positive')
    store.addFeedback('a3', 'SepsisWarning', 'Critical', 'too-early')

    expect(Object.keys(store.byAlertType)).toHaveLength(2)
    expect(store.byAlertType.SepsisWarning).toHaveLength(2)
    expect(store.byAlertType.WarningHeartRate).toHaveLength(1)
  })

  it('exportAsJson_generatesValidJson', () => {
    const store = useFeedbackStore()
    store.addFeedback('a1', 'SepsisWarning', 'Critical', 'useful')

    let capturedBlob = null
    const click = vi.fn()
    const originalCreateElement = document.createElement.bind(document)

    URL.createObjectURL = vi.fn((blob) => {
      capturedBlob = blob
      return 'blob:url'
    })
    URL.revokeObjectURL = vi.fn()
    vi.spyOn(document, 'createElement').mockImplementation((tag) => {
      if (tag === 'a') return { href: '', download: '', click }
      return originalCreateElement(tag)
    })

    expect(() => store.exportAsJson()).not.toThrow()
    expect(capturedBlob).toBeInstanceOf(Blob)
    expect(capturedBlob.type).toBe('application/json')
    expect(click).toHaveBeenCalled()
  })

  it('clearAll_emptiesEntries', () => {
    const store = useFeedbackStore()
    store.addFeedback('a1', 'SepsisWarning', 'Critical', 'useful')
    store.clearAll()

    expect(store.entries).toHaveLength(0)
  })

  it('persistsToLocalStorage', () => {
    const store = useFeedbackStore()
    store.addFeedback('a1', 'SepsisWarning', 'Critical', 'useful')

    const stored = JSON.parse(localStorage.getItem('vigilcare-feedback'))
    expect(stored).toHaveLength(1)
    expect(stored[0].rating).toBe('useful')
  })
})
