import { describe, expect, it } from 'vitest'
import { findPipeline, getValidStatesForStage, type StatefulPipelineDefinition } from './sales'

const TEST_CRM_PIPELINE: StatefulPipelineDefinition = {
  pipelineKey: 'crm',
  label: 'CRM',
  entityKey: 'crm.deal',
  stages: [
    {
      stageKey: 'interested',
      label: 'Interested',
      color: 'blue',
      states: [
        { stateKey: 'discovery_replied', label: 'Discovery Replied' },
        { stateKey: 'discovery_link_sent', label: 'Discovery Link Sent' }
      ]
    },
    { stageKey: 'proposal', label: 'Proposal', color: 'yellow', states: [] }
  ]
}

const TEST_LEAD_GEN_PIPELINE: StatefulPipelineDefinition = {
  pipelineKey: 'lead-gen',
  label: 'Lead Gen',
  entityKey: 'leadgen.contact',
  stages: [
    {
      stageKey: 'outreach',
      label: 'Outreach',
      color: 'blue',
      states: [
        { stateKey: 'personalized', label: 'Personalized' },
        { stateKey: 'contacted', label: 'Contacted' }
      ]
    }
  ]
}

describe('sales domain published surface', () => {
  it('does not export Elevasis CRM runtime constants from the generic core sales domain', async () => {
    const sales = await import('./sales')

    expect(Object.keys(sales)).not.toEqual(
      expect.arrayContaining([
        'CRM_PIPELINE_DEFINITION',
        'CRM_DISCOVERY_REPLIED_STATE',
        'CRM_PRIORITY_BUCKETS',
        'DEFAULT_CRM_PRIORITY_RULE_CONFIG',
        'DEFAULT_CRM_NEXT_ACTION_RULE_CONFIG'
      ])
    )
  })

  it('does not export Elevasis lead-gen pipeline constants from the generic core sales domain', async () => {
    const sales = await import('./sales')

    expect(Object.keys(sales)).not.toEqual(
      expect.arrayContaining([
        'ACQ_LIST_MEMBERS_LEAD_GEN_PIPELINE',
        'ACQ_LIST_COMPANIES_LEAD_GEN_PIPELINE',
        'LEAD_GEN_PIPELINE_DEFINITIONS'
      ])
    )
  })
})

describe('getValidStatesForStage', () => {
  it('returns states for a known stage from a caller-supplied pipeline definition', () => {
    const states = getValidStatesForStage(TEST_CRM_PIPELINE, 'interested')

    expect(states).toEqual([
      { stateKey: 'discovery_replied', label: 'Discovery Replied' },
      { stateKey: 'discovery_link_sent', label: 'Discovery Link Sent' }
    ])
  })

  it('returns empty array for a stage with no states', () => {
    expect(getValidStatesForStage(TEST_CRM_PIPELINE, 'proposal')).toEqual([])
  })

  it('returns empty array for an unknown stage key', () => {
    expect(getValidStatesForStage(TEST_CRM_PIPELINE, 'unknown_stage')).toEqual([])
  })

  it('is case-sensitive', () => {
    expect(getValidStatesForStage(TEST_CRM_PIPELINE, 'Interested')).toEqual([])
  })

  it('works with a caller-supplied lead-gen pipeline for a known stage', () => {
    const states = getValidStatesForStage(TEST_LEAD_GEN_PIPELINE, 'outreach')
    expect(states.length).toBeGreaterThan(0)
    expect(states.map((s) => s.stateKey)).toContain('personalized')
  })
})

describe('findPipeline', () => {
  it('finds a pipeline by pipelineKey when present', () => {
    const found = findPipeline([TEST_LEAD_GEN_PIPELINE], 'lead-gen')
    expect(found).toBe(TEST_LEAD_GEN_PIPELINE)
  })

  it('returns undefined for an unknown pipelineKey', () => {
    const found = findPipeline([TEST_LEAD_GEN_PIPELINE], 'crm')
    expect(found).toBeUndefined()
  })

  it('returns undefined for an empty array', () => {
    const found = findPipeline([], 'lead-gen')
    expect(found).toBeUndefined()
  })
})
