import { describe, it, expect } from 'vitest'
import {
  parseEmployeeRange,
  parseRevenueRange,
  buildAssessmentPayload,
} from '../scope-to-facts'
import type { CompanyProfile } from '../types'
import type { ScopeProfilingAnswer, ScopeDecision } from '../compliance-scope-types'

// =============================================================================
// parseEmployeeRange
// =============================================================================

describe('parseEmployeeRange', () => {
  it('returns 5 for "1-9"', () => {
    expect(parseEmployeeRange('1-9')).toBe(5)
  })

  it('returns 30 for "10-49"', () => {
    expect(parseEmployeeRange('10-49')).toBe(30)
  })

  it('returns 150 for "50-249"', () => {
    expect(parseEmployeeRange('50-249')).toBe(150)
  })

  it('returns 625 for "250-999"', () => {
    expect(parseEmployeeRange('250-999')).toBe(625)
  })

  it('returns 1500 for "1000+"', () => {
    expect(parseEmployeeRange('1000+')).toBe(1500)
  })

  it('returns 10 for null', () => {
    expect(parseEmployeeRange(null)).toBe(10)
  })

  it('returns 10 for undefined', () => {
    expect(parseEmployeeRange(undefined)).toBe(10)
  })
})

// =============================================================================
// parseRevenueRange
// =============================================================================

describe('parseRevenueRange', () => {
  it('returns 1000000 for "< 2 Mio"', () => {
    expect(parseRevenueRange('< 2 Mio')).toBe(1000000)
  })

  it('returns 6000000 for "2-10 Mio"', () => {
    expect(parseRevenueRange('2-10 Mio')).toBe(6000000)
  })

  it('returns 30000000 for "10-50 Mio"', () => {
    expect(parseRevenueRange('10-50 Mio')).toBe(30000000)
  })

  it('returns 75000000 for "> 50 Mio"', () => {
    expect(parseRevenueRange('> 50 Mio')).toBe(75000000)
  })

  it('returns 1000000 for null', () => {
    expect(parseRevenueRange(null)).toBe(1000000)
  })

  it('returns 1000000 for undefined', () => {
    expect(parseRevenueRange(undefined)).toBe(1000000)
  })
})

// =============================================================================
// buildAssessmentPayload
// =============================================================================

describe('buildAssessmentPayload', () => {
  const baseProfile = {
    companyName: 'Test GmbH',
    legalForm: 'gmbh',
    industry: ['IT', 'Software'],
    employeeCount: '50-249',
    annualRevenue: '10-50 Mio',
    headquartersCountry: 'DE',
    headquartersState: 'BW',
    isDataController: true,
    isDataProcessor: false,
    offerings: ['software_saas'],
  } as unknown as CompanyProfile

  const baseAnswers: ScopeProfilingAnswer[] = [
    { questionId: 'data_art9', value: false, blockId: 'data' },
    { questionId: 'data_minors', value: false, blockId: 'data' },
    { questionId: 'data_hr', value: true, blockId: 'data' },
    { questionId: 'data_financial', value: false, blockId: 'data' },
    { questionId: 'tech_third_country', value: true, blockId: 'tech' },
    { questionId: 'tech_subprocessors', value: true, blockId: 'tech' },
    { questionId: 'proc_adm_scoring', value: false, blockId: 'processing' },
    { questionId: 'proc_employee_monitoring', value: false, blockId: 'processing' },
    { questionId: 'proc_video_surveillance', value: false, blockId: 'processing' },
    { questionId: 'proc_tracking', value: false, blockId: 'processing' },
    { questionId: 'prod_cookies_consent', value: true, blockId: 'product' },
    { questionId: 'data_volume', value: false, blockId: 'data' },
    { questionId: 'ai_uses_ai', value: true, blockId: 'ai' },
    { questionId: 'ai_categories', value: ['ai_provider'], blockId: 'ai' },
    { questionId: 'ai_risk_assessment', value: 'limited', blockId: 'ai' },
    { questionId: 'org_cert_target', value: 'iso27001', blockId: 'organisation' },
  ]

  it('maps a full profile correctly', () => {
    const payload = buildAssessmentPayload(baseProfile, baseAnswers, null)

    expect(payload.employee_count).toBe(150)
    expect(payload.annual_revenue).toBe(30000000)
    expect(payload.country).toBe('DE')
    expect(payload.industry).toBe('IT, Software')
    expect(payload.legal_form).toBe('gmbh')
    expect(payload.is_controller).toBe(true)
    expect(payload.is_processor).toBe(false)
    expect(payload.cross_border_transfer).toBe(true)
    expect(payload.uses_processors).toBe(true)
    expect(payload.uses_cookies).toBe(true)
    expect(payload.processes_employee_data).toBe(true)
    expect(payload.operates_platform).toBe(true)
    expect(payload.proc_ai_usage).toBe(true)
    expect(payload.cert_target).toBe('iso27001')
  })

  it('uses defaults for null/undefined profile fields', () => {
    const emptyProfile = {
      companyName: 'Minimal',
    } as unknown as CompanyProfile
    const payload = buildAssessmentPayload(emptyProfile, [], null)

    expect(payload.employee_count).toBe(10) // parseEmployeeRange(undefined)
    expect(payload.annual_revenue).toBe(1000000)
    expect(payload.country).toBe('DE') // default
    expect(payload.industry).toBe('')
    expect(payload.legal_form).toBe('')
    expect(payload.is_controller).toBe(true) // default
    expect(payload.is_processor).toBe(false) // default
    expect(payload.determined_level).toBe('L2') // default
  })

  it('detects AI provider from ai_categories', () => {
    const payload = buildAssessmentPayload(baseProfile, baseAnswers, null)

    expect(payload.is_ai_provider).toBe(true)
    expect(payload.is_ai_deployer).toBe(false)
    expect(payload.limited_risk_ai).toBe(true)
    expect(payload.high_risk_ai).toBe(false)
  })

  it('detects AI deployer from ai_categories', () => {
    const deployerAnswers = baseAnswers.map(a =>
      a.questionId === 'ai_categories'
        ? { ...a, value: ['ai_deployer'] }
        : a
    )
    const payload = buildAssessmentPayload(baseProfile, deployerAnswers, null)

    expect(payload.is_ai_provider).toBe(false)
    expect(payload.is_ai_deployer).toBe(true)
  })

  it('detects financial institution from industry', () => {
    const finProfile: CompanyProfile = {
      ...baseProfile,
      industry: ['Finanzdienstleistungen', 'Banking'],
    }
    const payload = buildAssessmentPayload(finProfile, baseAnswers, null)

    expect(payload.is_financial_institution).toBe(true)
  })

  it('includes decision data when provided', () => {
    const decision: ScopeDecision = {
      determinedLevel: 'L3',
      triggeredHardTriggers: [
        { ruleId: 'rule-1', rule: { id: 'rule-1', name: 'Test Rule', description: '', targetLevel: 'L3', trigger: { field: '', op: 'eq', value: true } }, factValue: true },
      ],
      requiredDocuments: [
        { documentType: 'dsfa', reason: 'test', regulation: 'dsgvo' },
      ],
    } as any
    const payload = buildAssessmentPayload(baseProfile, baseAnswers, decision)

    expect(payload.determined_level).toBe('L3')
    expect(payload.triggered_rules).toEqual(['rule-1'])
    expect(payload.required_documents).toEqual(['dsfa'])
  })
})
