import { describe, expect, it } from 'vitest';
import { getContent } from '@/content';
import { answerArchie, findArchetypes, STARTER_SUGGESTIONS, type ArchieContext } from '@/lib/archie';
import { encodeAnswers } from '@/lib/code';
import { EMAIL_GATE_AT_QUESTION } from '@/lib/config';
import type { Answer } from '@/lib/types';

const content = getContent();
const ctx: ArchieContext = { content, paymentsMode: 'mock', supportEmail: '' };
const ask = (q: string, extra: Partial<ArchieContext> = {}) => answerArchie(q, { ...ctx, ...extra });

const architectCode = encodeAnswers(
  content.questions,
  content.questions.map((q): Answer => (q.kind === 'likert' ? (q.archetype === 'architect' ? 5 : 2) : 'architect')),
);

describe('Archie', () => {
  it('answers every starter suggestion with something specific', () => {
    for (const s of STARTER_SUGGESTIONS) expect(ask(s).text).not.toMatch(/not sure I caught/);
  });

  it('lists all eight archetypes', () => {
    const { text } = ask('What are the 8 archetypes?');
    for (const a of content.archetypeList) expect(text).toContain(a.name);
  });

  it('explains the assessment with the real numbers', () => {
    const { text } = ask('How does the assessment work?');
    expect(text).toContain('24 questions');
    expect(text).toContain(`question ${EMAIL_GATE_AT_QUESTION}`);
  });

  it('describes the Ark Summit VIP Ticket with its price, contents and test-mode status', () => {
    const { text } = ask('What is the VIP Ticket?');
    expect(text).toContain('$27');
    expect(text).toContain('The Winners Circle');
    expect(text).toContain('Architect Session');
    expect(text).toContain('VIP Ark Blueprint');
    expect(text).toMatch(/test mode/);
    expect(ask('How much does it cost?', { paymentsMode: 'disabled' }).text).toMatch(/go on sale shortly/);
  });

  it('answers about the Architect Session without mistaking it for the Architect archetype', () => {
    const { text } = ask('What will I learn in the Architect Session?');
    expect(text).toContain('Pick your first offer');
    expect(text).toContain('Land your first paying client');
    expect(ask('So how do I start?').text).toContain('Ark Summit VIP Ticket');
    expect(ask('What is the VIP Ark Blueprint?').text).toMatch(/personal plan/);
  });

  it('links a visitor with a result straight to their ticket', () => {
    const r = ask('How do I get the VIP ticket?', { resultCode: architectCode });
    expect(r.links.map((l) => l.href)).toContain(`/vip?c=${architectCode}`);
  });

  it('recognises archetypes by name, alias and plural', () => {
    expect(findArchetypes('tell me about mechanics', content)).toEqual(['engineer']);
    expect(findArchetypes("the architect's blind spots", content)).toEqual(['architect']);
    expect(findArchetypes('is a star better than a mason', content)).toEqual(['luminary', 'builder']);
  });

  it('answers complement questions', () => {
    const { text } = ask('Who complements a Builder?');
    expect(text).toContain('Architect');
    expect(text).toContain('Broker');
  });

  it('compares two archetypes', () => {
    const { text } = ask('Architect vs Builder');
    expect(text).toContain('natural complementary pair');
  });

  it('finds an archetype from a described strength', () => {
    expect(ask('Who is good at negotiating?').text).toContain('**Broker**');
  });

  it('routes privacy, scoring, retake, validity and team questions', () => {
    expect(ask('Is my data stored?').text).toMatch(/without an account/);
    expect(ask('How is it scored?').text).toMatch(/blended profile/);
    expect(ask('Can I retake it?').text).toMatch(/Retake your assessment/);
    expect(ask('Is this scientifically validated?').text).toMatch(/not a scientifically validated/);
    expect(ask('When is the team edition coming?').text).toMatch(/coming soon/);
  });

  it('uses the visitor result when one is available', () => {
    expect(ask("What's my archetype?").text).toMatch(/can't see a result/);
    const mine = ask("What's my archetype?", { resultCode: architectCode });
    expect(mine.text).toContain('Your lead archetype is the **Architect**');
    expect(mine.links.map((l) => l.href)).toContain(`/result?c=${architectCode}`);
    expect(ask('Who should I hire?', { resultCode: architectCode }).text).toMatch(/priority|complement/);
  });

  it('falls back politely with suggestions', () => {
    const r = ask('purple elephants');
    expect(r.text).toMatch(/not sure I caught/);
    expect(r.suggestions.length).toBeGreaterThan(0);
  });
});
