import { describe, it, expect, vi, beforeEach } from 'vitest'

// Hoist mock functions so vi.mock factories can reference them
const { mockFind, mockFindByIdAndUpdate, mockLookupByEmail } = vi.hoisted(() => ({
  mockFind: vi.fn(),
  mockFindByIdAndUpdate: vi.fn(),
  mockLookupByEmail: vi.fn()
}))

// Mock mongoose connection
vi.mock('../../../server/utils/mongoose.js', () => ({
  connectDB: vi.fn()
}))

// Mock Member model
vi.mock('../../../server/models/member.js', () => ({
  default: {
    find: mockFind,
    findByIdAndUpdate: mockFindByIdAndUpdate
  }
}))

// Mock @slack/web-api — use function expression so `new WebClient()` works
vi.mock('@slack/web-api', () => ({
  WebClient: vi.fn().mockImplementation(function () {
    this.users = { lookupByEmail: mockLookupByEmail }
  })
}))

import { checkSlackJoins } from '../../../server/utils/checkSlackJoins.js'

describe('checkSlackJoins', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    // Default: find returns empty with select chain
    mockFind.mockReturnValue({ select: vi.fn().mockResolvedValue([]) })
  })

  it('8.1: detects joined member — updates status and stores slackUserId', async () => {
    mockFind.mockReturnValue({
      select: vi.fn().mockResolvedValue([
        { _id: 'm1', email: 'alice@example.com', slackInviteStatus: 'sent' }
      ])
    })
    mockLookupByEmail.mockResolvedValue({ user: { id: 'U123ABC' } })
    mockFindByIdAndUpdate.mockResolvedValue({})

    const result = await checkSlackJoins('xoxb-test-token')

    expect(mockLookupByEmail).toHaveBeenCalledWith({ email: 'alice@example.com' })
    expect(mockFindByIdAndUpdate).toHaveBeenCalledWith('m1', {
      slackInviteStatus: 'joined',
      slackUserId: 'U123ABC'
    })
    expect(result).toEqual({ checked: 1, joined: 1 })
  })

  it('8.2: no change when member not found in Slack', async () => {
    mockFind.mockReturnValue({
      select: vi.fn().mockResolvedValue([
        { _id: 'm2', email: 'bob@example.com', slackInviteStatus: 'sent' }
      ])
    })
    // Slack throws users_not_found for unknown emails
    mockLookupByEmail.mockRejectedValue({ data: { error: 'users_not_found' } })

    const result = await checkSlackJoins('xoxb-test-token')

    expect(mockFindByIdAndUpdate).not.toHaveBeenCalled()
    expect(result).toEqual({ checked: 1, joined: 0 })
  })

  it('8.3: skips already-joined members (not included in query)', async () => {
    mockFind.mockReturnValue({
      select: vi.fn().mockResolvedValue([])
    })

    const result = await checkSlackJoins('xoxb-test-token')

    // Verify the query only looks for 'sent' and 'accepted'
    expect(mockFind).toHaveBeenCalledWith({
      slackInviteStatus: { $in: ['sent', 'accepted'] }
    })
    expect(result).toEqual({ checked: 0, joined: 0 })
    expect(mockLookupByEmail).not.toHaveBeenCalled()
  })

  it('8.4: skips members with null slackInviteStatus (not included in query)', async () => {
    mockFind.mockReturnValue({
      select: vi.fn().mockResolvedValue([])
    })

    await checkSlackJoins('xoxb-test-token')

    // Query uses $in with only 'sent' and 'accepted' — null is excluded
    const queryArg = mockFind.mock.calls[0][0]
    expect(queryArg.slackInviteStatus.$in).toEqual(['sent', 'accepted'])
    expect(queryArg.slackInviteStatus.$in).not.toContain(null)
  })

  it('8.6: partial failure does not abort batch — remaining members still processed', async () => {
    mockFind.mockReturnValue({
      select: vi.fn().mockResolvedValue([
        { _id: 'm1', email: 'alice@example.com', slackInviteStatus: 'sent' },
        { _id: 'm2', email: 'bob@example.com', slackInviteStatus: 'sent' },
        { _id: 'm3', email: 'carol@example.com', slackInviteStatus: 'sent' }
      ])
    })

    // First call: unexpected API error
    mockLookupByEmail
      .mockRejectedValueOnce(new Error('Slack API timeout'))
      // Second call: not found
      .mockRejectedValueOnce({ data: { error: 'users_not_found' } })
      // Third call: found
      .mockResolvedValueOnce({ user: { id: 'U999' } })

    mockFindByIdAndUpdate.mockResolvedValue({})

    const result = await checkSlackJoins('xoxb-test-token')

    // All three were checked
    expect(mockLookupByEmail).toHaveBeenCalledTimes(3)
    // Only the third member joined
    expect(mockFindByIdAndUpdate).toHaveBeenCalledTimes(1)
    expect(mockFindByIdAndUpdate).toHaveBeenCalledWith('m3', {
      slackInviteStatus: 'joined',
      slackUserId: 'U999'
    })
    expect(result).toEqual({ checked: 3, joined: 1 })
  })

  it('8.7: handles accepted status members too', async () => {
    mockFind.mockReturnValue({
      select: vi.fn().mockResolvedValue([
        { _id: 'm4', email: 'dave@example.com', slackInviteStatus: 'accepted' }
      ])
    })
    mockLookupByEmail.mockResolvedValue({ user: { id: 'UABC' } })
    mockFindByIdAndUpdate.mockResolvedValue({})

    const result = await checkSlackJoins('xoxb-test-token')

    expect(mockLookupByEmail).toHaveBeenCalledWith({ email: 'dave@example.com' })
    expect(mockFindByIdAndUpdate).toHaveBeenCalledWith('m4', {
      slackInviteStatus: 'joined',
      slackUserId: 'UABC'
    })
    expect(result).toEqual({ checked: 1, joined: 1 })
  })
})
