import { describe, it, expect } from 'vitest'
import { mount } from '@vue/test-utils'
import FolderBreadcrumb from '../FolderBreadcrumb.vue'

function seg(id, name) { return { id, name } }

describe('FolderBreadcrumb', () => {
  it('always renders a "Home" / "Folders" root button', () => {
    const w = mount(FolderBreadcrumb, { props: { segments: [] } })
    expect(w.find('button').exists()).toBe(true)
  })

  it('clicking root button emits navigate(null)', async () => {
    const w = mount(FolderBreadcrumb, { props: { segments: [] } })
    await w.find('button').trigger('click')
    expect(w.emitted('navigate')).toBeTruthy()
    expect(w.emitted('navigate')[0]).toEqual([null])
  })

  it('renders intermediate segments as clickable buttons', () => {
    const w = mount(FolderBreadcrumb, {
      props: { segments: [seg('r1', 'Root'), seg('f1', 'Test')] },
    })
    // "Root" is intermediate (not last), "Test" is last (plain text)
    const buttons = w.findAll('button')
    // first button is "Home/Folders", second is "Root"
    expect(buttons.length).toBe(2)
    expect(buttons[1].text()).toBe('Root')
  })

  it('clicking intermediate segment emits navigate(id)', async () => {
    const w = mount(FolderBreadcrumb, {
      props: { segments: [seg('r1', 'Root'), seg('f1', 'Test')] },
    })
    const buttons = w.findAll('button')
    await buttons[1].trigger('click') // "Root" button
    expect(w.emitted('navigate')).toBeTruthy()
    expect(w.emitted('navigate')[0]).toEqual(['r1'])
  })

  it('renders last segment as plain non-interactive text', () => {
    const w = mount(FolderBreadcrumb, {
      props: { segments: [seg('r1', 'Root'), seg('f1', 'Test')] },
    })
    // Last segment "Test" should be a <span>, not a button
    const spans = w.findAll('span')
    const lastSpan = spans.find(s => s.text() === 'Test')
    expect(lastSpan).toBeTruthy()
  })

  it('last segment is NOT clickable (no navigate event)', async () => {
    const w = mount(FolderBreadcrumb, {
      props: { segments: [seg('r1', 'Root'), seg('f1', 'Test')] },
    })
    const spans = w.findAll('span')
    const lastSpan = spans.find(s => s.text() === 'Test')
    if (lastSpan) await lastSpan.trigger('click')
    // navigate should NOT have been emitted by clicking the last segment
    const navigateEvents = (w.emitted('navigate') || []).filter(e => e[0] === 'f1')
    expect(navigateEvents.length).toBe(0)
  })

  it('single segment: just root button + last segment as text', () => {
    const w = mount(FolderBreadcrumb, {
      props: { segments: [seg('f1', 'OnlyFolder')] },
    })
    // Only the "Home" button and "OnlyFolder" as plain text
    const buttons = w.findAll('button')
    expect(buttons.length).toBe(1) // just "Home"
    expect(w.text()).toContain('OnlyFolder')
  })

  it('collapses >4 segments with ellipsis, preserving first and last two', () => {
    const segments = [
      seg('a', 'A'), seg('b', 'B'), seg('c', 'C'),
      seg('d', 'D'), seg('e', 'E'),
    ]
    const w = mount(FolderBreadcrumb, { props: { segments } })
    const text = w.text()
    expect(text).toContain('A')   // first preserved
    expect(text).toContain('…')   // ellipsis present
    expect(text).toContain('D')   // second-to-last preserved
    expect(text).toContain('E')   // last preserved
    expect(text).not.toContain('B') // middle segments collapsed
    expect(text).not.toContain('C')
  })

  it('3 segments: all rendered without ellipsis', () => {
    const segments = [seg('a', 'A'), seg('b', 'B'), seg('c', 'C')]
    const w = mount(FolderBreadcrumb, { props: { segments } })
    const text = w.text()
    expect(text).toContain('A')
    expect(text).toContain('B')
    expect(text).toContain('C')
    expect(text).not.toContain('…')
  })

  it('deep 3-level path: clicking middle segment navigates correctly', async () => {
    const segments = [seg('root', 'Root'), seg('mid', 'Mid'), seg('cur', 'Current')]
    const w = mount(FolderBreadcrumb, { props: { segments } })
    const buttons = w.findAll('button')
    // buttons[0] = Home, buttons[1] = Root, buttons[2] = Mid
    await buttons[2].trigger('click')
    const events = w.emitted('navigate') || []
    const midClicks = events.filter(e => e[0] === 'mid')
    expect(midClicks.length).toBe(1)
  })
})
