// @vitest-environment happy-dom
import { describe, it, expect, vi, afterEach } from 'vitest'

import GalleryFacetPanel from '../../src/components/gallery/GalleryFacetPanel.vue'
import { useGalleryStore } from '../../src/stores/gallery.js'
import { freshPinia, mountComponent } from '../support/mountComponent.js'

const FACETS = {
  total: 5,
  platforms: [
    { value: 'patreon', count: 3 },
    { value: null, count: 2 },  // unsourced → "No platform" bucket
  ],
  untagged: 4,
  no_artist: 1,
  date_min: '2020-01-01T00:00:00+00:00',
  date_max: '2026-06-01T00:00:00+00:00',
}

function stubFetchOk(body) {
  globalThis.fetch = vi.fn(async () => ({
    ok: true, status: 200, statusText: 'OK',
    text: async () => JSON.stringify(body),
  }))
}

describe('GalleryFacetPanel', () => {
  afterEach(() => vi.restoreAllMocks())

  it('renders platform options with counts and the curation flags', () => {
    const pinia = freshPinia()
    stubFetchOk(FACETS)              // covers the onMounted loadFacets() call
    const s = useGalleryStore()
    s.facets = FACETS               // seed so counts render synchronously
    const w = mountComponent(GalleryFacetPanel, { pinia })
    const t = w.text()
    expect(t).toContain('patreon')
    expect(t).toContain('3')
    expect(t).toContain('No platform')
    expect(t).toContain('Untagged')
    expect(t).toContain('4')
    expect(t).toContain('No artist')
  })

  it('shows an en-dash placeholder for a flag count before facets load', () => {
    const pinia = freshPinia()
    stubFetchOk(FACETS)
    useGalleryStore().facets = null  // not loaded yet
    const w = mountComponent(GalleryFacetPanel, { pinia })
    expect(w.text()).toContain('Untagged')
    expect(w.text()).toContain('–')
  })
})
