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

const { submitAlertFeedback, fetchQualityMetricsSummary, fetchQualityMetrics } = vi.hoisted(() => ({
  submitAlertFeedback: vi.fn().mockResolvedValue({
    id: 'fb-1',
    createdAt: '2026-06-23T00:00:00Z',
    feedbackType: 'Useful',
  }),
  fetchQualityMetricsSummary: vi.fn().mockResolvedValue({
    totalAlerts: 10,
    totalFeedback: 4,
    acknowledgementRate: 0.8,
    usefulRate: 0.75,
    falsePositiveRate: 0.1,
    wouldActRate: 0.6,
    avgSecondsToAcknowledge: 300,
    avgSecondsToResolution: 1200,
  }),
  fetchQualityMetrics: vi.fn().mockResolvedValue({
    items: [
      { alertType: 'SEPSIS_WARNING', usefulRate: 0.75, falsePositiveRate: 0.1, acknowledgementRate: 0.8, totalAlerts: 5 },
      { alertType: 'WARNING_HEART_RATE', usefulRate: 0.5, falsePositiveRate: 0.2, acknowledgementRate: 0.7, totalAlerts: 5 },
    ],
  }),
}))

vi.mock('@/api/alertQuality', () => ({
  submitAlertFeedback,
  fetchQualityMetricsSummary,
  fetchQualityMetrics,
  FEEDBACK_TYPE_MAP: {
    useful: 'Useful',
    'false-positive': 'FalsePositive',
    'would-act': 'WouldAct',
    'too-early': 'TooEarly',
  },
}))

describe('useFeedbackStore (compat shim)', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    setActivePinia(createPinia())
  })

  it('addFeedback_delegatesToAlertQualityAndCaches', async () => {
    const store = useFeedbackStore()
    await store.addFeedback('a1', 'SEPSIS_WARNING', 'CRITICAL', 'useful')

    expect(submitAlertFeedback).toHaveBeenCalledWith('a1', 'useful', '')
    expect(store.getFeedback('a1').rating).toBe('useful')
  })

  it('addFeedback_updatesCachedRating', async () => {
    submitAlertFeedback
      .mockResolvedValueOnce({ id: 'fb-1', createdAt: '2026-06-23T00:00:00Z', feedbackType: 'Useful' })
      .mockResolvedValueOnce({ id: 'fb-2', createdAt: '2026-06-23T00:01:00Z', feedbackType: 'FalsePositive' })

    const store = useFeedbackStore()
    await store.addFeedback('a1', 'SEPSIS_WARNING', 'CRITICAL', 'useful')
    await store.addFeedback('a1', 'SEPSIS_WARNING', 'CRITICAL', 'false-positive')

    expect(store.getFeedback('a1').rating).toBe('false-positive')
  })

  it('stats_mapsFromQualitySummary', async () => {
    const store = useFeedbackStore()
    const { useAlertQualityStore } = await import('@/stores/alertQuality')
    await useAlertQualityStore().loadDashboard()

    expect(store.stats.value.total).toBe(4)
    expect(store.stats.value.usefulPct).toBe(75)
    expect(store.stats.value.fpPct).toBe(10)
  })

  it('byAlertType_groupsSnapshots', async () => {
    const store = useFeedbackStore()
    const { useAlertQualityStore } = await import('@/stores/alertQuality')
    await useAlertQualityStore().loadDashboard()

    expect(Object.keys(store.byAlertType.value)).toHaveLength(2)
    expect(store.byAlertType.value.SEPSIS_WARNING).toHaveLength(1)
    expect(store.byAlertType.value.WARNING_HEART_RATE).toHaveLength(1)
  })

  it('entries_isEmptyCompatStub', () => {
    const store = useFeedbackStore()
    expect(store.entries.value).toEqual([])
  })

  it('exportAndClearAreNoOps', () => {
    const store = useFeedbackStore()
    expect(() => store.exportAsJson()).not.toThrow()
    expect(() => store.exportAsCsv()).not.toThrow()
    expect(() => store.clearAll()).not.toThrow()
  })
})
