"use strict";
// AI-Powered Workflow Command
// Analyzes project context and generates intelligent, contextual workflows using AI
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowCommand = void 0;
const chalk_1 = __importDefault(require("chalk"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const prompts_1 = __importDefault(require("prompts"));
const generate_1 = require("./generate");
const generate_components_1 = require("./generate-components");
const hybridAIClient_1 = require("../utils/hybridAIClient");
class WorkflowCommand {
    constructor() {
        this.ai = new hybridAIClient_1.HybridAIClient();
    }
    register(program) {
        const command = program
            .command("workflow")
            .description("AI-powered workflow analysis and project planning")
            .option("--analyze", "Analyze current project and suggest optimal workflow")
            .option("--generate", "Generate and execute workflow based on project context")
            .option("--interactive", "Interactive workflow planning")
            .option("--description <text>", "Project description for analysis")
            .option("--context-file <path>", "Path to context file")
            .option("--output <dir>", "Output directory", "mycontext-workflow")
            .option("--force", "Overwrite existing files")
            .option("--auto-continue", "Automatically continue to component generation")
            .option("--skip-components", "Skip component generation step")
            .option("--skip-preview", "Skip preview setup step")
            .option("--deploy", "Include deployment setup")
            .option("--with-tests", "Generate tests for components", true);
        command
            .command("analyze")
            .description("Analyze project context and recommend optimal workflow")
            .option("--description <text>", "Project description")
            .option("--context-file <path>", "Context file path")
            .option("--verbose", "Show detailed analysis")
            .action(async (options) => {
            await this.analyzeProject(options);
        });
        command
            .command("generate")
            .description("Generate and execute complete workflow from project context")
            .option("--description <text>", "Project description")
            .option("--context-file <path>", "Context file path")
            .option("--output <dir>", "Output directory", "mycontext-workflow")
            .option("--force", "Overwrite existing files")
            .option("--auto-continue", "Continue to component generation")
            .option("--skip-components", "Skip component generation step")
            .option("--skip-features", "Skip feature assembly step")
            .option("--skip-preview", "Skip preview setup step")
            .option("--role <role>", "Role for feature assembly (admin/user/guest)", "admin")
            .option("--deploy", "Include deployment setup")
            .option("--with-tests", "Generate tests for components", true)
            .action(async (options) => {
            await this.generateWorkflow(options);
        });
        command.action(async (options) => {
            if (options.analyze) {
                await this.analyzeProject(options);
            }
            else if (options.interactive) {
                await this.interactiveWorkflow();
            }
            else {
                console.log(chalk_1.default.blue.bold("🎯 MyContext AI Workflow System\n"));
                console.log("This command uses AI to analyze your project requirements and generate");
                console.log("optimal, contextual workflows tailored to your specific needs.\n");
                console.log(chalk_1.default.cyan("Commands:"));
                console.log("  mycontext workflow analyze    # Analyze project and get recommendations");
                console.log("  mycontext workflow generate   # Generate and execute complete workflow");
                console.log("  mycontext workflow --interactive # Interactive workflow planning");
                console.log("");
                console.log(chalk_1.default.cyan("Examples:"));
                console.log(`  ${chalk_1.default.gray("# Analyze current project")}`);
                console.log(`  ${chalk_1.default.cyan('mycontext workflow analyze --description "E-commerce platform"')}`);
                console.log("");
                console.log(`  ${chalk_1.default.gray("# Generate complete workflow")}`);
                console.log(`  ${chalk_1.default.cyan("mycontext workflow generate --context-file ./context.md")}`);
                console.log("");
                console.log(chalk_1.default.green("💡 AI analyzes your requirements to create custom workflows!"));
            }
        });
    }
    async analyzeProject(options) {
        try {
            console.log(chalk_1.default.blue.bold("🔍 Analyzing Project Context...\n"));
            const context = await this.gatherProjectContext(options);
            const analysis = await this.performAIAnalysis(context);
            this.displayAnalysis(analysis);
        }
        catch (error) {
            console.error(chalk_1.default.red("❌ Analysis failed:"), error);
            throw error;
        }
    }
    async generateWorkflow(options) {
        try {
            console.log(chalk_1.default.blue.bold("🚀 Generating AI-Powered Workflow...\n"));
            // Step 1: Gather context
            const context = await this.gatherProjectContext(options);
            console.log(chalk_1.default.cyan("📋 Context gathered"));
            // Step 2: AI analysis
            const analysis = await this.performAIAnalysis(context);
            console.log(chalk_1.default.cyan("🤖 AI analysis completed"));
            // Step 3: Display analysis
            this.displayAnalysis(analysis);
            // Step 4: Confirm and execute
            const { confirm } = await (0, prompts_1.default)({
                type: "confirm",
                name: "confirm",
                message: `Execute this ${analysis.workflowType} workflow?`,
                initial: true,
            });
            if (!confirm) {
                console.log(chalk_1.default.yellow("Workflow execution cancelled."));
                return;
            }
            // Step 5: Execute workflow
            await this.executeWorkflow(analysis, options);
        }
        catch (error) {
            console.error(chalk_1.default.red("❌ Workflow generation failed:"), error);
            throw error;
        }
    }
    async interactiveWorkflow() {
        console.log(chalk_1.default.blue.bold("🎯 Interactive Workflow Planning\n"));
        const { description } = await (0, prompts_1.default)({
            type: "text",
            name: "description",
            message: "Describe your project:",
            validate: (value) => value.length > 10 || "Please provide a detailed description",
        });
        const { techStack } = await (0, prompts_1.default)({
            type: "multiselect",
            name: "techStack",
            message: "Select technologies:",
            choices: [
                { title: "Next.js", value: "nextjs" },
                { title: "React", value: "react" },
                { title: "TypeScript", value: "typescript" },
                { title: "Tailwind CSS", value: "tailwind" },
                { title: "Prisma", value: "prisma" },
                { title: "NextAuth", value: "nextauth" },
                { title: "Stripe", value: "stripe" },
                { title: "Supabase", value: "supabase" },
            ],
            min: 1,
        });
        const { complexity } = await (0, prompts_1.default)({
            type: "select",
            name: "complexity",
            message: "Project complexity:",
            choices: [
                { title: "Simple (landing page, blog)", value: "low" },
                { title: "Moderate (dashboard, e-commerce)", value: "medium" },
                { title: "Complex (social platform, LMS)", value: "high" },
            ],
        });
        const options = {
            description,
            techStack,
            complexity,
            interactive: true,
        };
        await this.generateWorkflow(options);
    }
    async gatherProjectContext(options) {
        const context = {
            description: options.description,
            output: options.output || "mycontext-workflow",
            force: options.force,
            autoContinue: options.autoContinue,
        };
        // Try to read context file
        if (options.contextFile) {
            if (await fs_extra_1.default.pathExists(options.contextFile)) {
                context.contextFile = options.contextFile;
                context.description = await fs_extra_1.default.readFile(options.contextFile, "utf-8");
            }
        }
        // Try to find existing PRD
        const prdPath = path_1.default.join(process.cwd(), ".mycontext", "01-prd.md");
        if (await fs_extra_1.default.pathExists(prdPath)) {
            context.prdFile = prdPath;
            if (!context.description) {
                context.description = await fs_extra_1.default.readFile(prdPath, "utf-8");
            }
        }
        // Detect existing components
        const componentsPath = path_1.default.join(process.cwd(), ".mycontext", "05-component-list.json");
        if (await fs_extra_1.default.pathExists(componentsPath)) {
            try {
                const componentsData = await fs_extra_1.default.readJson(componentsPath);
                context.existingComponents = componentsData.components || [];
            }
            catch (error) {
                // Ignore JSON parse errors
            }
        }
        return context;
    }
    async performAIAnalysis(context) {
        const prompt = `Analyze the following project requirements and generate a comprehensive workflow analysis:

Project Description:
${context.description || "Not provided"}

${context.existingComponents
            ? `Existing Components: ${context.existingComponents.join(", ")}`
            : ""}

Please provide a JSON response with this exact structure:
{
  "workflowType": "string (e.g., 'e-commerce', 'dashboard', 'blog', 'task-manager', 'social-platform', 'learning-platform')",
  "complexity": "low|medium|high",
  "estimatedHours": number,
  "components": ["array", "of", "component", "names"],
  "features": ["array", "of", "feature", "descriptions"],
  "architecture": "nextjs-app-router|nextjs-pages|react-spa",
  "techStack": ["array", "of", "technologies"],
  "workflow": {
    "steps": [
      {
        "id": "string",
        "name": "string",
        "description": "string",
        "estimatedTime": number,
        "dependencies": ["array", "of", "step", "ids"],
        "components": ["optional", "array", "of", "components", "for", "this", "step"]
      }
    ],
    "milestones": [
      {
        "name": "string",
        "description": "string",
        "components": ["array"],
        "estimatedTime": number
      }
    ]
  },
  "recommendations": ["array", "of", "specific", "recommendations"],
  "rationale": "string explaining why this workflow fits the project"
}

Be specific and actionable. Focus on realistic components and features for the described project.`;
        const result = await this.ai.generateText(prompt, {
            temperature: 0.3, // Lower temperature for more consistent analysis
        });
        if (!result || !result.text) {
            throw new Error("AI analysis failed");
        }
        try {
            // Extract JSON from response
            const jsonMatch = result.text.match(/\{[\s\S]*\}/);
            if (!jsonMatch) {
                throw new Error("No JSON found in AI response");
            }
            const analysis = JSON.parse(jsonMatch[0]);
            // Validate required fields
            if (!analysis.workflowType ||
                !analysis.workflow ||
                !analysis.workflow.steps) {
                throw new Error("Invalid analysis structure");
            }
            return analysis;
        }
        catch (error) {
            console.error("Failed to parse AI analysis:", error);
            throw new Error("AI returned invalid analysis format");
        }
    }
    displayAnalysis(analysis) {
        console.log(chalk_1.default.blue.bold(`📊 Workflow Analysis: ${analysis.workflowType.toUpperCase()}`));
        console.log("");
        // Project overview
        console.log(chalk_1.default.cyan("🏗️  Project Overview:"));
        console.log(`   Type: ${chalk_1.default.white(analysis.workflowType)}`);
        console.log(`   Complexity: ${chalk_1.default.white(analysis.complexity)}`);
        console.log(`   Estimated Time: ${chalk_1.default.white(analysis.estimatedHours + " hours")}`);
        console.log(`   Architecture: ${chalk_1.default.white(analysis.architecture)}`);
        console.log("");
        // Tech stack
        if (analysis.techStack && analysis.techStack.length > 0) {
            console.log(chalk_1.default.cyan("🛠️  Recommended Tech Stack:"));
            analysis.techStack.forEach((tech) => {
                console.log(`   • ${chalk_1.default.white(tech)}`);
            });
            console.log("");
        }
        // Key features
        if (analysis.features && analysis.features.length > 0) {
            console.log(chalk_1.default.cyan("✨ Key Features:"));
            analysis.features.slice(0, 5).forEach((feature) => {
                console.log(`   • ${chalk_1.default.white(feature)}`);
            });
            if (analysis.features.length > 5) {
                console.log(`   ... and ${analysis.features.length - 5} more features`);
            }
            console.log("");
        }
        // Components
        if (analysis.components && analysis.components.length > 0) {
            console.log(chalk_1.default.cyan("🧩 Components to Generate:"));
            analysis.components.slice(0, 10).forEach((component) => {
                console.log(`   • ${chalk_1.default.white(component)}`);
            });
            if (analysis.components.length > 10) {
                console.log(`   ... and ${analysis.components.length - 10} more components`);
            }
            console.log("");
        }
        // Workflow steps
        if (analysis.workflow?.steps && analysis.workflow.steps.length > 0) {
            console.log(chalk_1.default.cyan("🔄 Workflow Steps:"));
            analysis.workflow.steps.forEach((step, index) => {
                console.log(`   ${index + 1}. ${chalk_1.default.yellow(step.name)} (${step.estimatedTime}min)`);
                console.log(`      ${chalk_1.default.gray(step.description)}`);
                if (step.dependencies && step.dependencies.length > 0) {
                    console.log(`      ${chalk_1.default.gray("Dependencies: " + step.dependencies.join(", "))}`);
                }
            });
            console.log("");
        }
        // Milestones
        if (analysis.workflow?.milestones &&
            analysis.workflow.milestones.length > 0) {
            console.log(chalk_1.default.cyan("🎯 Milestones:"));
            analysis.workflow.milestones.forEach((milestone, index) => {
                console.log(`   ${index + 1}. ${chalk_1.default.green(milestone.name)} (${milestone.estimatedTime}min)`);
                console.log(`      ${chalk_1.default.gray(milestone.description)}`);
                console.log(`      ${chalk_1.default.gray("Components: " + milestone.components.slice(0, 3).join(", "))}`);
            });
            console.log("");
        }
        // Recommendations
        if (analysis.recommendations && analysis.recommendations.length > 0) {
            console.log(chalk_1.default.cyan("💡 Recommendations:"));
            analysis.recommendations.forEach((rec) => {
                console.log(`   • ${chalk_1.default.white(rec)}`);
            });
            console.log("");
        }
        // Rationale
        if (analysis.rationale) {
            console.log(chalk_1.default.cyan("🤔 Analysis Rationale:"));
            console.log(`   ${chalk_1.default.gray(analysis.rationale)}`);
            console.log("");
        }
    }
    async executeWorkflow(analysis, options) {
        const outputDir = options.output || `mycontext-${analysis.workflowType}`;
        console.log(chalk_1.default.blue.bold(`🚀 Executing ${analysis.workflowType} Workflow`));
        console.log(chalk_1.default.gray(`Output directory: ${outputDir}`));
        console.log("");
        try {
            // Step 1: Generate context files (Brain Sync)
            console.log(chalk_1.default.cyan("📝 Step 1: Generating context files (Brain Sync)..."));
            const contextDescription = this.generateContextDescription(analysis);
            const generateCommand = new generate_1.GenerateCommand();
            await generateCommand.execute({
                type: "context",
                full: true,
                description: contextDescription,
                force: options.force,
            });
            // Step 3: Generate architecture
            console.log(chalk_1.default.cyan("🏗️  Step 3: Generating project architecture..."));
            await generateCommand.execute({
                type: "architecture",
                description: contextDescription,
                output: path_1.default.join(process.cwd(), outputDir, ".mycontext"),
                autoContinue: options.autoContinue,
                force: options.force,
            });
            // Step 4: Generate Components
            if (!options.skipComponents) {
                console.log(chalk_1.default.cyan("🎨 Step 4: Generating components..."));
                const componentsCommand = new generate_components_1.GenerateComponentsCommand();
                await componentsCommand.execute("all", {
                    all: true,
                    withTests: options.withTests !== false,
                    output: path_1.default.join(process.cwd(), outputDir),
                    verbose: options.verbose,
                });
            }
            // Step 5: Feature Scaffolding
            if (!options.skipFeatures) {
                console.log(chalk_1.default.cyan("📦 Step 5: Finalizing feature scaffolding..."));
                // Feature assembly is now handled by the unified brain sync and architecture generation
                console.log(chalk_1.default.gray("   Features reconciled with Living Brain"));
            }
            // Step 6: Preview Information
            if (!options.skipPreview) {
                console.log(chalk_1.default.cyan("👁️  Step 6: Preview your components..."));
                console.log(chalk_1.default.blue("   Visit https://studio.mycontext.app"));
                console.log(chalk_1.default.gray("   Upload your .mycontext/ directory to preview all components"));
            }
            // Step 7: Summary and Next Steps
            console.log(chalk_1.default.green.bold(`\n✅ ${analysis.workflowType} workflow completed!`));
            console.log(chalk_1.default.cyan("📁 Project created in:"), outputDir);
            console.log(chalk_1.default.cyan("🧩 Components generated:"), analysis.components?.length || 0);
            console.log(chalk_1.default.cyan("✨ Features included:"), analysis.features?.length || 0);
            console.log(chalk_1.default.blue("\n📋 Next Steps:"));
            console.log(chalk_1.default.gray("   1. Review components: cd " + outputDir));
            console.log(chalk_1.default.gray("   2. Preview at: https://studio.mycontext.app"));
            console.log(chalk_1.default.gray("   3. Start development: npm run dev"));
            if (options.deploy) {
                console.log(chalk_1.default.gray("   4. Deploy: mycontext deploy"));
            }
        }
        catch (error) {
            console.error(chalk_1.default.red(`❌ Workflow execution failed: ${error}`));
            throw error;
        }
    }
    generateContextDescription(analysis) {
        return `Build a ${analysis.workflowType} application with the following requirements:

Technical Stack: ${analysis.techStack?.join(", ") || "Next.js, TypeScript, Tailwind CSS"}
Architecture: ${analysis.architecture}

Key Features:
${analysis.features?.map((f) => `• ${f}`).join("\n") || ""}

Components Needed:
${analysis.components?.map((c) => `• ${c}`).join("\n") || ""}

${analysis.rationale || ""}

Estimated development time: ${analysis.estimatedHours} hours`;
    }
    static getHelpText() {
        return `
${chalk_1.default.blue.bold("🎯 MyContext AI Workflow System")}

${chalk_1.default.yellow("Description:")}
  Intelligent workflow analysis and project planning using AI. Analyzes your
  project requirements and generates contextual, optimized workflows tailored
  to your specific needs.

${chalk_1.default.yellow("Commands:")}
  mycontext workflow analyze    # Analyze project and get AI recommendations
  mycontext workflow generate   # Generate and execute complete workflow
  mycontext workflow --interactive # Interactive workflow planning

${chalk_1.default.yellow("Analysis Features:")}
  • AI-powered project type detection
  • Complexity assessment and time estimation
  • Component and feature recommendations
  • Technology stack suggestions
  • Step-by-step workflow planning
  • Milestone identification

${chalk_1.default.yellow("Workflow Types Detected:")}
  • E-commerce platforms
  • Analytics dashboards
  • Content management systems
  • Task management tools
  • Social media platforms
  • Learning management systems
  • And many more...

${chalk_1.default.yellow("Options:")}
  --description <text>    Project description for analysis
  --context-file <path>   Path to existing context file
  --output <dir>          Output directory (default: mycontext-workflow)
  --force                 Overwrite existing files
  --auto-continue         Continue to component generation automatically

${chalk_1.default.yellow("Examples:")}
  ${chalk_1.default.gray("# Analyze a project idea")}
  ${chalk_1.default.cyan('mycontext workflow analyze --description "Build a task management app"')}

  ${chalk_1.default.gray("# Generate complete workflow")}
  ${chalk_1.default.cyan("mycontext workflow generate --context-file ./requirements.md")}

  ${chalk_1.default.gray("# Interactive planning")}
  ${chalk_1.default.cyan("mycontext workflow --interactive")}

${chalk_1.default.green("💡 AI analyzes your specific requirements to create custom workflows!")}
`;
    }
}
exports.WorkflowCommand = WorkflowCommand;
//# sourceMappingURL=workflow.js.map