import path from 'node:path';
import { fileURLToPath } from 'node:url';

import type { StageSpec } from './types.js';

/** Package root resolved via import.meta.url — works regardless of install location. */
export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');

/** Pipeline asset directory (stages, agents, rails, gates, templates). */
export const PIPELINE_DIR = path.join(PACKAGE_ROOT, 'pipeline');

/** Stage specifications — defines every pipeline stage. */
export const STAGE_SPECS: Record<string, StageSpec> = {
  '00-intake': {
    number: '00',
    title: 'Forge Intake',
    dispatch: 'exec',
    type: 'research',
    gate: 'none',
    promptFile: '00-intake.md',
    qualityGates: [],
  },
  '00b-discovery': {
    number: '00b',
    title: 'Forge Discovery',
    dispatch: 'exec',
    type: 'research',
    gate: 'discovery-complete',
    promptFile: '00b-discovery.md',
    qualityGates: ['true'],
  },
  '01-board': {
    number: '01',
    title: 'Forge Board Review',
    dispatch: 'exec',
    type: 'review',
    gate: 'board-approval',
    promptFile: '01-board.md',
    qualityGates: [{ type: 'ci-pipeline', command: 'board-approval (via board-tasks)' }],
  },
  '01b-brief-analyzer': {
    number: '01b',
    title: 'Forge Brief Analyzer',
    dispatch: 'exec',
    type: 'research',
    gate: 'brief-analysis-complete',
    promptFile: '01-board.md',
    qualityGates: ['true'],
  },
  '02-planning-1': {
    number: '02',
    title: 'Forge Planning 1',
    dispatch: 'exec',
    type: 'research',
    gate: 'architecture-approval',
    promptFile: '02-planning-1-architecture.md',
    qualityGates: ['true'],
  },
  '03-planning-2': {
    number: '03',
    title: 'Forge Planning 2',
    dispatch: 'exec',
    type: 'research',
    gate: 'implementation-approval',
    promptFile: '03-planning-2-implementation.md',
    qualityGates: ['true'],
  },
  '04-planning-3': {
    number: '04',
    title: 'Forge Planning 3',
    dispatch: 'exec',
    type: 'research',
    gate: 'decomposition-approval',
    promptFile: '04-planning-3-decomposition.md',
    qualityGates: ['true'],
  },
  '05-coding': {
    number: '05',
    title: 'Forge Coding',
    dispatch: 'yolo',
    type: 'coding',
    gate: 'lint-build-test',
    promptFile: '05-coding.md',
    qualityGates: ['pnpm lint', 'pnpm build', 'pnpm test'],
  },
  '06-review': {
    number: '06',
    title: 'Forge Review',
    dispatch: 'exec',
    type: 'review',
    gate: 'review-pass',
    promptFile: '06-review.md',
    qualityGates: [
      {
        type: 'ai-review',
        command:
          'echo \'{"summary":"review-pass","verdict":"approve","findings":[],"stats":{"blockers":0,"should_fix":0,"suggestions":0}}\'',
      },
    ],
  },
  '07-remediate': {
    number: '07',
    title: 'Forge Remediation',
    dispatch: 'yolo',
    type: 'coding',
    gate: 're-review',
    promptFile: '07-remediate.md',
    qualityGates: ['true'],
  },
  '08-test': {
    number: '08',
    title: 'Forge Test Validation',
    dispatch: 'exec',
    type: 'review',
    gate: 'tests-green',
    promptFile: '08-test.md',
    qualityGates: ['pnpm test'],
  },
  '09-deploy': {
    number: '09',
    title: 'Forge Deploy',
    dispatch: 'exec',
    type: 'deploy',
    gate: 'deploy-verification',
    promptFile: '09-deploy.md',
    qualityGates: [{ type: 'ci-pipeline', command: 'deploy-verification' }],
  },
};

/** Ordered stage sequence — full pipeline. */
export const STAGE_SEQUENCE = [
  '00-intake',
  '00b-discovery',
  '01-board',
  '01b-brief-analyzer',
  '02-planning-1',
  '03-planning-2',
  '04-planning-3',
  '05-coding',
  '06-review',
  '07-remediate',
  '08-test',
  '09-deploy',
];

/** Per-stage timeout in seconds. */
export const STAGE_TIMEOUTS: Record<string, number> = {
  '00-intake': 120,
  '00b-discovery': 300,
  '01-board': 120,
  '01b-brief-analyzer': 300,
  '02-planning-1': 600,
  '03-planning-2': 600,
  '04-planning-3': 600,
  '05-coding': 3600,
  '06-review': 600,
  '07-remediate': 3600,
  '08-test': 600,
  '09-deploy': 600,
};

/** Human-readable labels per stage. */
export const STAGE_LABELS: Record<string, string> = {
  '00-intake': 'INTAKE',
  '00b-discovery': 'DISCOVERY',
  '01-board': 'BOARD',
  '01b-brief-analyzer': 'BRIEF ANALYZER',
  '02-planning-1': 'PLANNING 1',
  '03-planning-2': 'PLANNING 2',
  '04-planning-3': 'PLANNING 3',
  '05-coding': 'CODING',
  '06-review': 'REVIEW',
  '07-remediate': 'REMEDIATE',
  '08-test': 'TEST',
  '09-deploy': 'DEPLOY',
};

/** Keywords that indicate a strategic brief. */
export const STRATEGIC_KEYWORDS = new Set([
  'security',
  'pricing',
  'architecture',
  'integration',
  'budget',
  'strategy',
  'compliance',
  'migration',
  'partnership',
  'launch',
]);

/** Keywords that indicate a technical brief. */
export const TECHNICAL_KEYWORDS = new Set([
  'bugfix',
  'bug',
  'refactor',
  'ui',
  'style',
  'tweak',
  'typo',
  'lint',
  'cleanup',
  'rename',
  'hotfix',
  'patch',
  'css',
  'format',
]);
