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

const mockPush = vi.fn()
let mockRoutePath = '/cloud'

vi.mock('vue-router', () => ({
  useRouter: () => ({ push: mockPush }),
  useRoute: () => ({ path: mockRoutePath }),
}))

vi.mock('../../../api/client.js', () => ({
  getCloudFoldersByConnectionId: vi.fn().mockResolvedValue({ items: [] }),
  getCloudFolders: vi.fn(), // deprecated — must NOT be called
}))

import * as api from '../../../api/client.js'
import CloudProviderTreeItem from '../CloudProviderTreeItem.vue'

const TreeItemStub = {
  name: 'TreeItem',
  template: `<div data-test="tree-item" @click="$emit('select')"><slot name="children" :children="[]" /></div>`,
  props: ['label', 'loadChildren', 'depth', 'isActive'],
  emits: ['select'],
}

const globalStubs = {
  TreeItem: TreeItemStub,
  CloudFolderTreeItem: true,
  AppIcon: true,
}

const connection = {
  id: 'uuid-conn-1',
  provider: 'nextcloud',
  display_name: 'My Nextcloud',
  status: 'ACTIVE',
}

beforeEach(() => {
  setActivePinia(createPinia())
  vi.clearAllMocks()
  mockRoutePath = '/cloud'
})

describe('CloudProviderTreeItem', () => {
  it('uses connection.id (UUID) for browse, never provider slug', async () => {
    const wrapper = mount(CloudProviderTreeItem, {
      props: { connection },
      global: { stubs: globalStubs },
    })
    // Call loadChildren from the stub's prop
    const treeItemStub = wrapper.findComponent(TreeItemStub)
    const loadChildren = treeItemStub.props('loadChildren')
    if (loadChildren) {
      await loadChildren()
      expect(api.getCloudFoldersByConnectionId).toHaveBeenCalledWith('uuid-conn-1', '')
      expect(api.getCloudFoldersByConnectionId).not.toHaveBeenCalledWith('nextcloud', expect.anything())
    }
    // getCloudFolders (deprecated) must never be called
    expect(api.getCloudFolders).not.toHaveBeenCalled()
  })

  it('navigates to /cloud/{uuid}/root on select, never /cloud/{provider}', async () => {
    const wrapper = mount(CloudProviderTreeItem, {
      props: { connection },
      global: { stubs: globalStubs },
    })
    await wrapper.find('[data-test="tree-item"]').trigger('click')
    expect(mockPush).toHaveBeenCalledWith('/cloud/uuid-conn-1/root')
    expect(mockPush).not.toHaveBeenCalledWith(expect.stringContaining('nextcloud'))
  })

  it('isActive=true when route matches connection root', () => {
    mockRoutePath = '/cloud/uuid-conn-1/root'
    const wrapper = mount(CloudProviderTreeItem, {
      props: { connection },
      global: { stubs: globalStubs },
    })
    expect(wrapper.findComponent(TreeItemStub).props('isActive')).toBe(true)
  })

  it('isActive=false when route is /cloud (overview)', () => {
    mockRoutePath = '/cloud'
    const wrapper = mount(CloudProviderTreeItem, {
      props: { connection },
      global: { stubs: globalStubs },
    })
    expect(wrapper.findComponent(TreeItemStub).props('isActive')).toBe(false)
  })
})
