/**
 * Tests for the shared PDF proposal document builder.
 *
 * Verifies that provider branding is fully caller-supplied and no
 * hardcoded "Elevasis" strings leak into the shared @repo/core builder.
 */

import { describe, it, expect } from 'vitest'
import { buildProposalDocument } from '../proposal-document'
import { acceptanceSection } from '../acceptance'
import type { AutomationConfig } from '../types'

// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------

const sampleAutomation: AutomationConfig = {
  title: 'Test Automation',
  howItWorks: 'Does stuff automatically.',
  workflow: ['Step 1', 'Step 2'],
  impact: ['Saves time'],
  hoursPerWeek: 5,
  annualSavings: 10000
}

const baseConfig = {
  company: 'Acme Corp',
  contact: 'Jane Doe',
  vertical: 'Tech',
  solutions: 'Automation',
  executiveSummary: 'Summary text.',
  automations: [sampleAutomation],
  tier: 'tier2' as const,
  initialFee: 5000,
  monthlyFee: 2500
}

// ---------------------------------------------------------------------------
// buildProposalDocument
// ---------------------------------------------------------------------------

describe('buildProposalDocument', () => {
  it('sets metadata.author from providerName', () => {
    const doc = buildProposalDocument({ ...baseConfig, providerName: 'Elevasis' })
    expect(doc.metadata.author).toBe('Elevasis')
  })

  it('sets metadata.author to a different caller-supplied value', () => {
    const doc = buildProposalDocument({ ...baseConfig, providerName: 'Acme Solutions' })
    expect(doc.metadata.author).toBe('Acme Solutions')
  })

  it('does not hardcode "Elevasis" in metadata when a different providerName is given', () => {
    const doc = buildProposalDocument({ ...baseConfig, providerName: 'OtherProvider' })
    expect(doc.metadata.author).not.toBe('Elevasis')
  })

  it('includes the providerName in the acceptance page text', () => {
    const doc = buildProposalDocument({ ...baseConfig, providerName: 'MyAgency' })
    // The acceptance page is the last page
    const lastPage = doc.pages[doc.pages.length - 1]
    const allText = JSON.stringify(lastPage)
    expect(allText).toContain('MyAgency')
    expect(allText).not.toContain('Elevasis')
  })

  it('includes correct metadata title', () => {
    const doc = buildProposalDocument({ ...baseConfig, providerName: 'Elevasis' })
    expect(doc.metadata.title).toBe('Proposal for Acme Corp')
  })

  it('builds the correct number of pages (cover + summary + automations + summary-investment + acceptance)', () => {
    const doc = buildProposalDocument({ ...baseConfig, providerName: 'Elevasis' })
    // 1 cover + 1 summary + 1 automation + 1 summary-investment + 1 acceptance = 5
    expect(doc.pages).toHaveLength(5)
  })
})

// ---------------------------------------------------------------------------
// acceptanceSection
// ---------------------------------------------------------------------------

describe('acceptanceSection', () => {
  it('uses providerName in the acceptance statement', () => {
    const page = acceptanceSection({
      companyName: 'Acme Corp',
      contactName: 'Jane Doe',
      tier: 'tier2',
      initialFee: 5000,
      providerName: 'Elevasis'
    })
    const text = JSON.stringify(page)
    expect(text).toContain('authorizes Elevasis to proceed')
  })

  it('uses a different providerName when supplied', () => {
    const page = acceptanceSection({
      companyName: 'Acme Corp',
      contactName: 'Jane Doe',
      tier: 'tier1',
      initialFee: 3000,
      providerName: 'AnotherAgency'
    })
    const text = JSON.stringify(page)
    expect(text).toContain('authorizes AnotherAgency to proceed')
    expect(text).not.toContain('Elevasis')
  })

  it('includes companyName and contactName in the acceptance statement', () => {
    const page = acceptanceSection({
      companyName: 'TestCo',
      contactName: 'Bob Smith',
      tier: 'tier2',
      initialFee: 4000,
      providerName: 'MyProvider'
    })
    const text = JSON.stringify(page)
    expect(text).toContain('Bob Smith')
    expect(text).toContain('TestCo')
  })

  it('omits monthly fee term when monthlyFee is not provided', () => {
    const page = acceptanceSection({
      companyName: 'Acme',
      contactName: 'Alice',
      tier: 'tier1',
      initialFee: 2000,
      providerName: 'Elevasis'
    })
    const text = JSON.stringify(page)
    expect(text).not.toContain('Monthly fee')
  })

  it('includes monthly fee term when monthlyFee is provided', () => {
    const page = acceptanceSection({
      companyName: 'Acme',
      contactName: 'Alice',
      tier: 'tier2',
      initialFee: 5000,
      monthlyFee: 1500,
      providerName: 'Elevasis'
    })
    const text = JSON.stringify(page)
    expect(text).toContain('Monthly fee')
  })
})
