/**
 * generate.test.ts — Tests for BA order markdown generation.
 */
import { describe, it, expect } from 'vitest';
import { generateBaOrder } from '../generate.js';
import type { BaOrderPlan, ModuleKey } from '../types.js';

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

function minimalPlan(overrides: Partial<BaOrderPlan> = {}): BaOrderPlan {
  return {
    generatedAt: '2026-05-26',
    apps: ['ERP'],
    totalModules: 2,
    totalDeps: 1,
    waves: [
      {
        level: 1,
        modules: [
          {
            appCode: 'ERP',
            moduleCode: 'REFERENCES',
            key: 'ERP/REFERENCES' as ModuleKey,
            dependsOn: [],
            isExternal: false,
            warnings: [],
          },
        ],
      },
      {
        level: 2,
        modules: [
          {
            appCode: 'ERP',
            moduleCode: 'CUSTOMERS',
            key: 'ERP/CUSTOMERS' as ModuleKey,
            dependsOn: ['ERP/REFERENCES' as ModuleKey],
            isExternal: false,
            warnings: [],
          },
        ],
      },
    ],
    cycles: [],
    warnings: [],
    source: 'context',
    ...overrides,
  };
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

describe('generateBaOrder', () => {
  it('produces markdown with anchor comment', () => {
    const plan = minimalPlan();
    const file = generateBaOrder(plan, '_plan/ba-order.md');

    expect(file.path).toBe('_plan/ba-order.md');
    expect(file.content).toContain('<!-- ba:plan kind=ba-order');
    expect(file.content).toContain('generated=2026-05-26');
    expect(file.content).toContain('apps=ERP');
    expect(file.content).toContain('source=context');
  });

  it('includes heading with module and wave count', () => {
    const plan = minimalPlan();
    const file = generateBaOrder(plan, '_plan/ba-order.md');

    expect(file.content).toContain("# Ordre d'analyse BA — ERP");
    expect(file.content).toContain('2 modules · 2 vagues');
  });

  it('includes summary table', () => {
    const plan = minimalPlan();
    const file = generateBaOrder(plan, '_plan/ba-order.md');

    expect(file.content).toContain('| Modules dans le scope | 2 |');
    expect(file.content).toContain('| Vagues BA | 2 |');
    expect(file.content).toContain('| Dépendances inter-modules | 1 |');
  });

  it('generates wave sections with dependency info', () => {
    const plan = minimalPlan();
    const file = generateBaOrder(plan, '_plan/ba-order.md');

    expect(file.content).toContain('## Wave 1 — Fondations');
    expect(file.content).toContain('| ERP/REFERENCES | — |');
    expect(file.content).toContain('## Wave 2');
    expect(file.content).toContain('| ERP/CUSTOMERS | ERP/REFERENCES |');
  });

  it('includes Mermaid graph', () => {
    const plan = minimalPlan();
    const file = generateBaOrder(plan, '_plan/ba-order.md');

    expect(file.content).toContain('```mermaid');
    expect(file.content).toContain('graph LR');
    expect(file.content).toContain('ERP_REFERENCES["ERP/REFERENCES"]');
    expect(file.content).toContain('ERP_REFERENCES --> ERP_CUSTOMERS');
  });

  it('includes next steps with BA phase order', () => {
    const plan = minimalPlan();
    const file = generateBaOrder(plan, '_plan/ba-order.md');

    expect(file.content).toContain('/ba-create-actors');
    expect(file.content).toContain('/ba-create-screen');
  });

  it('renders external prerequisites in Wave 0', () => {
    const plan = minimalPlan({
      waves: [
        {
          level: 0,
          modules: [
            {
              appCode: 'HR',
              moduleCode: 'EMPLOYEES',
              key: 'HR/EMPLOYEES' as ModuleKey,
              dependsOn: [],
              isExternal: true,
              referencedBy: ['ERP/CUSTOMERS' as ModuleKey],
              warnings: [],
            },
          ],
        },
        ...minimalPlan().waves,
      ],
    });
    const file = generateBaOrder(plan, '_plan/ba-order.md');

    expect(file.content).toContain('Wave 0 — Prérequis externes');
    expect(file.content).toContain('HR/EMPLOYEES');
    expect(file.content).toContain('ERP/CUSTOMERS');
  });

  it('renders cycle warnings', () => {
    const plan = minimalPlan({
      cycles: [['X/A' as ModuleKey, 'X/B' as ModuleKey]],
      warnings: ['Dépendance cyclique détectée : X/A ↔ X/B'],
    });
    const file = generateBaOrder(plan, '_plan/ba-order.md');

    expect(file.content).toContain('Dépendances cycliques');
    expect(file.content).toContain('X/A ↔ X/B');
  });
});
