import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect } from 'vitest';
import { itAllure } from './helpers/allure-test.js';
import { ClaudeCodeAdapter } from '../src/core/command-generation/adapters/claude.js';
import type { ToolCommandAdapter } from '../src/core/command-generation/types.js';
import type { TemplateMetadata } from '../src/core/metadata.js';

const it = itAllure.epic('Platform & Distribution');

const ROOT = new URL('..', import.meta.url).pathname;
const SKILL_MD = readFileSync(join(ROOT, 'skills/agreements/open-agreements/SKILL.md'), 'utf-8');
const SHARED_EXECUTION_MD = readFileSync(
  // Per-skill copy; all copies are identical (enforced by skills-data.test.mjs).
  join(ROOT, 'skills/agreements/open-agreements/template-filling-execution.md'),
  'utf-8'
);

const SAMPLE_METADATA: TemplateMetadata = {
  name: 'Mutual Non-Disclosure Agreement',
  description: 'Mutual NDA',
  source_url: 'https://example.com/mnda',
  version: '1.0',
  license: 'CC-BY-4.0',
  allow_derivatives: true,
  attribution_text: 'Example attribution',
  fields: [
    {
      name: 'purpose',
      type: 'string',
      description: 'Purpose',
      section: 'Term Sheet',
    },
    {
      name: 'effective_date',
      type: 'date',
      description: 'Effective date',
      section: 'Term Sheet',
    },
    {
      name: 'governing_law',
      type: 'string',
      description: 'Governing law',
      section: 'Legal',
    },
  ],
  priority_fields: ['purpose', 'effective_date'],
};

describe('adapter architecture', () => {
  it.openspec('OA-CLI-007')('ClaudeCodeAdapter implements ToolCommandAdapter interface', () => {
    const adapter: ToolCommandAdapter = new ClaudeCodeAdapter();
    expect(adapter.name).toBe('claude-code');
    expect(adapter.getOutputPath('common-paper-mutual-nda')).toBe(
      '.claude/commands/open-agreements.md'
    );
    expect(typeof adapter.generateSkillFile).toBe('function');
  });

  it.openspec('OA-CLI-008')('new adapter can be added without modifying core', () => {
    const customAdapter: ToolCommandAdapter = {
      name: 'cursor',
      generateSkillFile: () => '# cursor skill',
      getOutputPath: (templateId: string) => `.cursor/rules/${templateId}.md`,
    };
    expect(customAdapter.generateSkillFile(SAMPLE_METADATA, 'nda')).toContain('cursor skill');
    expect(customAdapter.getOutputPath('nda')).toBe('.cursor/rules/nda.md');
  });
});

describe('Claude skill generation', () => {
  const adapter = new ClaudeCodeAdapter();
  const generatedSkill = adapter.generateSkillFile(SAMPLE_METADATA, 'common-paper-mutual-nda');

  it.openspec('OA-CLI-005')('generated Claude skill interviews user in AskUserQuestion rounds', () => {
    expect(generatedSkill).toContain('AskUserQuestion');
    expect(generatedSkill).toContain('Field Collection Rounds');
    expect(generatedSkill).toContain('Round 1');
  });

  it.openspec('OA-CLI-006')('generated Claude skill renders DOCX via fill command after interview', () => {
    expect(generatedSkill).toContain('open-agreements fill common-paper-mutual-nda');
  });
});

describe('published skills/agreements/open-agreements/SKILL.md execution paths', () => {
  it.openspec('OA-CLI-009')('documents remote MCP as primary zero-install path', () => {
    // Execution steps live in the skill's own template-filling-execution.md,
    // referenced from SKILL.md via relative link
    expect(SKILL_MD).toContain('template-filling-execution.md');
    expect(SHARED_EXECUTION_MD).toContain('Remote MCP');
    expect(SHARED_EXECUTION_MD).toContain('fill_template');
    expect(SHARED_EXECUTION_MD).not.toContain('npx -y open-agreements@latest');
  });

  it.openspec('OA-CLI-010')('documents installed CLI DOCX rendering path', () => {
    expect(SHARED_EXECUTION_MD).toContain('open-agreements fill <template-name>');
  });

  it.openspec('OA-CLI-011')('documents preview-only fallback when MCP and CLI are unavailable', () => {
    expect(SHARED_EXECUTION_MD).toContain('PREVIEW_ONLY');
    expect(SHARED_EXECUTION_MD).toContain('PREVIEW ONLY');
  });
});
