import { describe, it, expect, beforeEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import FeedbackSummary from '@/views/FeedbackSummary.vue'

const { mockSummaryStats, mockByAlertType } = vi.hoisted(() => ({
  mockSummaryStats: {
    totalFeedback: 3,
    usefulRate: 67,
    falsePositiveRate: 33,
  },
  mockByAlertType: {
    SEPSIS_WARNING: [{ rating: 'useful' }, { rating: 'would-act' }],
    WARNING_HEART_RATE: [{ rating: 'false-positive' }],
  },
}))

vi.mock('@/stores/alertQuality', () => ({
  useAlertQualityStore: () => ({
    summaryStats: mockSummaryStats,
    byAlertType: mockByAlertType,
    submitFeedback: vi.fn(),
    getFeedback: vi.fn(() => null),
  }),
}))

describe('FeedbackSummary', () => {
  beforeEach(() => {
    setActivePinia(createPinia())
  })

  it('showsAggregateStats', () => {
    const wrapper = mount(FeedbackSummary)

    expect(wrapper.text()).toContain('Total Ratings')
    expect(wrapper.text()).toContain('Useful / Would Act')
    expect(wrapper.text()).toContain('False Positive')
    expect(wrapper.text()).toContain('67%')
    expect(wrapper.text()).toContain('33%')
  })

  it('showsPerAlertTypeBreakdown', () => {
    const wrapper = mount(FeedbackSummary)

    expect(wrapper.text()).toContain('Sepsis Warning (SIRS — Legacy)')
    expect(wrapper.text()).toContain('Warning Heart Rate')
    expect(wrapper.text()).toContain('2 ratings')
    expect(wrapper.text()).toContain('1 ratings')
  })

  it('exportButtonsExist', () => {
    const wrapper = mount(FeedbackSummary)
    const buttons = wrapper.findAll('button')

    expect(buttons.some(b => b.text().includes('Export JSON'))).toBe(true)
    expect(buttons.some(b => b.text().includes('Export CSV'))).toBe(true)
  })
})
