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

export const BUILTIN_PRD_TEMPLATES: Record<string, PrdTemplate> = {
  software: {
    id: 'software',
    name: 'Software Project',
    fields: ['owner', 'status', 'scopeVersion', 'successMetrics'],
    sections: [
      { id: 'introduction', title: 'Introduction', fields: ['context', 'objective'] },
      { id: 'problem-statement', title: 'Problem Statement', fields: ['painPoints'] },
      { id: 'scope-non-goals', title: 'Scope / Non-Goals', fields: ['inScope', 'outOfScope'] },
      { id: 'user-stories', title: 'User Stories / Requirements', fields: ['stories'] },
      { id: 'functional-requirements', title: 'Functional Requirements', fields: ['requirements'] },
      {
        id: 'non-functional-requirements',
        title: 'Non-Functional Requirements',
        fields: ['performance', 'reliability', 'security'],
      },
      { id: 'acceptance-criteria', title: 'Acceptance Criteria', fields: ['criteria'] },
      {
        id: 'technical-considerations',
        title: 'Technical Considerations',
        fields: ['constraints', 'dependencies'],
      },
      {
        id: 'risks-open-questions',
        title: 'Risks / Open Questions',
        fields: ['risks', 'openQuestions'],
      },
      {
        id: 'milestones-delivery',
        title: 'Milestones / Delivery',
        fields: ['milestones', 'timeline'],
      },
    ],
  },
  feature: {
    id: 'feature',
    name: 'Feature PRD',
    fields: ['owner', 'status', 'releaseTarget'],
    sections: [
      { id: 'problem-statement', title: 'Problem Statement', fields: ['problem'] },
      { id: 'goals', title: 'Goals', fields: ['goals'] },
      { id: 'scope', title: 'Scope', fields: ['inScope', 'outOfScope'] },
      { id: 'user-stories', title: 'User Stories', fields: ['stories'] },
      { id: 'requirements', title: 'Requirements', fields: ['functional', 'nonFunctional'] },
      { id: 'acceptance-criteria', title: 'Acceptance Criteria', fields: ['criteria'] },
      {
        id: 'technical-considerations',
        title: 'Technical Considerations',
        fields: ['constraints'],
      },
      {
        id: 'risks-open-questions',
        title: 'Risks / Open Questions',
        fields: ['risks', 'questions'],
      },
      { id: 'milestones', title: 'Milestones', fields: ['milestones'] },
      { id: 'success-metrics', title: 'Success Metrics / Testing', fields: ['metrics', 'testing'] },
    ],
  },
  spike: {
    id: 'spike',
    name: 'Research Spike',
    fields: ['owner', 'status', 'decisionDeadline'],
    sections: [
      { id: 'background', title: 'Background', fields: ['context'] },
      { id: 'research-questions', title: 'Research Questions', fields: ['questions'] },
      { id: 'constraints', title: 'Constraints', fields: ['constraints'] },
      { id: 'options', title: 'Options Considered', fields: ['options'] },
      { id: 'evaluation', title: 'Evaluation Criteria', fields: ['criteria'] },
      { id: 'findings', title: 'Findings', fields: ['findings'] },
      { id: 'recommendation', title: 'Recommendation', fields: ['recommendation'] },
      { id: 'risks', title: 'Risks / Unknowns', fields: ['risks', 'unknowns'] },
      { id: 'next-steps', title: 'Next Steps', fields: ['nextSteps'] },
      { id: 'milestones', title: 'Milestones / Delivery', fields: ['milestones'] },
    ],
  },
};

export function resolveTemplate(templateName?: string): PrdTemplate {
  const name =
    templateName === undefined || templateName.trim().length === 0 ? 'software' : templateName;

  const template = BUILTIN_PRD_TEMPLATES[name];
  if (template === undefined) {
    throw new Error(
      `Unknown PRD template: ${name}. Expected one of: ${Object.keys(BUILTIN_PRD_TEMPLATES).join(', ')}`,
    );
  }

  return template;
}
