"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HelpCommand = void 0;
const chalk_1 = __importDefault(require("chalk"));
const nextStepsSuggester_1 = require("../utils/nextStepsSuggester");
const workflowEngine_1 = require("../utils/workflowEngine");
const ProjectStateAnalyzer_1 = require("../utils/ProjectStateAnalyzer");
class HelpCommand {
    constructor(program) {
        this.program = program;
    }
    async execute(options) {
        const projectRoot = process.cwd();
        if (options.topic) {
            await this.showTopicHelp(options.topic, options.verbose);
            return;
        }
        // Context-aware help based on project state
        await this.showContextAwareHelp(projectRoot, options.verbose);
    }
    async showContextAwareHelp(projectRoot, verbose) {
        console.log(chalk_1.default.blue("🤖 MyContext - Context-Aware Help\n"));
        // Use enhanced project state analyzer
        const analyzer = new ProjectStateAnalyzer_1.ProjectStateAnalyzer(projectRoot);
        await analyzer.displayProjectStatus();
        // Show available workflows
        console.log(chalk_1.default.yellow("⚡ Available Workflows:"));
        this.displayAvailableWorkflows(verbose);
        console.log();
        // Show quick commands
        console.log(chalk_1.default.magenta("🚀 Quick Start Commands:"));
        this.displayQuickCommands();
        console.log();
        if (verbose) {
            console.log(chalk_1.default.gray("💡 Pro Tips:"));
            console.log(chalk_1.default.gray("  • Use 'mycontext setup-complete --auto-continue' for intelligent project completion"));
            console.log(chalk_1.default.gray("  • Use 'mycontext workflow suggest' for workflow recommendations"));
            console.log(chalk_1.default.gray("  • Use 'mycontext help <topic>' for detailed help on specific topics"));
            console.log();
        }
    }
    displayProjectStatus(context) {
        const statusItems = [
            {
                label: "PRD",
                status: context.hasPRD,
                description: "Product Requirements Document",
            },
            {
                label: "Context",
                status: context.hasContextFiles,
                description: "Context files (features, flows, etc.)",
            },
            {
                label: "Components",
                status: context.hasComponents,
                description: "React components generated",
            },
            {
                label: "shadcn/ui",
                status: context.hasShadcn,
                description: "UI component library",
            },
            {
                label: "InstantDB",
                status: context.hasInstantDB,
                description: "Real-time database",
            },
            {
                label: "AI Config",
                status: context.aiProviderConfigured,
                description: "AI provider configured",
            },
        ];
        statusItems.forEach((item) => {
            const statusIcon = item.status ? chalk_1.default.green("✅") : chalk_1.default.red("❌");
            const statusText = item.status
                ? chalk_1.default.green("Ready")
                : chalk_1.default.red("Missing");
            console.log(`  ${statusIcon} ${item.label}: ${statusText} - ${item.description}`);
        });
        if (context.projectType) {
            console.log(chalk_1.default.gray(`  🎯 Detected project type: ${chalk_1.default.cyan(context.projectType)}`));
        }
    }
    async displayWorkflowSuggestions(analyzer) {
        try {
            const suggestions = await analyzer.getWorkflowSuggestions();
            if (suggestions.length === 0) {
                console.log(chalk_1.default.gray("  ✨ Your project is fully set up! Ready for development."));
                return;
            }
            for (const suggestion of suggestions.slice(0, 3)) {
                const priorityIcon = suggestion.priority === "high"
                    ? "🔴"
                    : suggestion.priority === "medium"
                        ? "🟡"
                        : "🟢";
                console.log(`  ${priorityIcon} ${chalk_1.default.cyan(suggestion.command)}`);
                console.log(`     ${chalk_1.default.gray(suggestion.description)} (${suggestion.estimatedTime})`);
                if (suggestion.reason) {
                    console.log(`     ${chalk_1.default.gray("Reason: " + suggestion.reason)}`);
                }
                console.log();
            }
            if (suggestions.length > 3) {
                console.log(chalk_1.default.gray(`  ... and ${suggestions.length - 3} more suggestions.`));
                console.log(chalk_1.default.gray(`  Run 'mycontext help --topic workflows' for all options.`));
            }
        }
        catch (error) {
            console.log(chalk_1.default.gray("  💡 Run 'mycontext setup-complete --interactive' to get started"));
        }
    }
    async displayIntelligentSuggestions(projectRoot) {
        try {
            const suggestions = await nextStepsSuggester_1.NextStepsSuggester.getIntelligentSuggestions(projectRoot);
            if (suggestions.length === 0) {
                console.log(chalk_1.default.gray("  ✨ Your project is fully set up! Ready for development."));
                return;
            }
            suggestions.slice(0, 5).forEach((suggestion, index) => {
                console.log(`  ${index + 1}. ${suggestion.description}`);
                console.log(chalk_1.default.gray(`     ${suggestion.command}`));
                if (suggestion.context) {
                    console.log(chalk_1.default.gray(`     ℹ️  ${suggestion.context}`));
                }
                console.log();
            });
        }
        catch (error) {
            console.log(chalk_1.default.gray("  Unable to analyze project state for suggestions."));
        }
    }
    displayAvailableWorkflows(verbose) {
        const workflows = workflowEngine_1.WorkflowEngine.getWorkflows();
        workflows.forEach((workflow) => {
            console.log(chalk_1.default.cyan(`  ${workflow.id}`));
            console.log(`    ${workflow.name}`);
            if (verbose) {
                console.log(chalk_1.default.gray(`    ${workflow.description}`));
                console.log(chalk_1.default.gray(`    ⏱️ ${workflow.estimatedTotalTime}min • 📝 ${workflow.steps.length} steps`));
            }
            console.log();
        });
    }
    displayQuickCommands() {
        const commands = [
            {
                cmd: "mycontext setup-complete --interactive",
                desc: "Complete guided project setup with AI assistance",
                priority: "high",
            },
            {
                cmd: "mycontext workflow analyze",
                desc: "Analyze current project and get AI recommendations",
                priority: "medium",
            },
            {
                cmd: "mycontext generate architecture --auto-continue",
                desc: "Generate complete project architecture",
                priority: "medium",
            },
            {
                cmd: "mycontext status",
                desc: "Check current project status and health",
                priority: "low",
            },
            {
                cmd: "mycontext validate",
                desc: "Validate project structure and configuration",
                priority: "low",
            },
        ];
        commands
            .sort((a, b) => {
            const priorityOrder = {
                high: 3,
                medium: 2,
                low: 1,
            };
            const aPriority = priorityOrder[a.priority || "medium"] || 2;
            const bPriority = priorityOrder[b.priority || "medium"] || 2;
            return bPriority - aPriority;
        })
            .forEach((cmd) => {
            const priorityIcon = cmd.priority === "high"
                ? "🔥"
                : cmd.priority === "medium"
                    ? "💡"
                    : "📖";
            console.log(`  ${priorityIcon} ${chalk_1.default.cyan(cmd.cmd)}`);
            console.log(chalk_1.default.gray(`     ${cmd.desc}`));
        });
    }
    async showTopicHelp(topic, verbose) {
        const topics = {
            "getting-started": {
                title: "🚀 Getting Started",
                content: `
MyContext is an AI-powered component generation and project setup tool.

Quick Start:
  1. Initialize with your preferred framework:
     ${chalk_1.default.cyan("mycontext init my-app --framework instantdb")}  # Full stack (default)
     ${chalk_1.default.cyan("mycontext init my-app --framework nextjs")}     # Next.js + shadcn
     ${chalk_1.default.cyan("mycontext init my-app --framework other")}      # MyContext only

  2. Set up AI provider: Configure GEMINI_API_KEY (free) or ANTHROPIC_API_KEY
     ${chalk_1.default.gray("echo 'GEMINI_API_KEY=your-key' > .mycontext/.env")}

  3. Generate context: ${chalk_1.default.cyan("mycontext generate context --full")}

For InstantDB projects, template files are automatically copied to lib/:
  - instant-client.ts (Client SDK with schema)
  - instant-admin.ts (Admin SDK for server operations)
  - auth.ts (Password hashing, magic codes, subdomains)
  - instantdb-storage.ts (File upload/download)

Use ${chalk_1.default.cyan("mycontext help frameworks")} for detailed framework information.
Use ${chalk_1.default.cyan("mycontext help")} for context-aware guidance based on your project state.`,
            },
            frameworks: {
                title: "🎯 Framework Support",
                content: `
MyContext adapts to your tech stack with flexible initialization:

${chalk_1.default.cyan("1. InstantDB (Full Stack)")} - Default
   ${chalk_1.default.gray("Complete real-time backend with auth and storage")}

   Command: ${chalk_1.default.cyan("mycontext init my-app --framework instantdb")}

   What happens:
   ✅ Runs shadcn/ui init interactively
   ✅ Prompts to run instant-cli init
   ✅ Prompts to push schemas to InstantDB
   ✅ Installs: @instantdb/react, @instantdb/admin
   ✅ Installs auth deps: bcryptjs, nanoid, @types/bcryptjs
   ✅ Copies template files to lib/ (or src/lib/):
      • instant-client.ts - Client SDK with schema
      • instant-admin.ts - Admin SDK for server operations
      • auth.ts - Auth helpers (hashing, magic codes)
      • instantdb-storage.ts - File upload/download
   ✅ Creates .mycontext/ project structure

   After init, configure:
   ${chalk_1.default.gray("echo 'NEXT_PUBLIC_INSTANT_APP_ID=your-app-id' > .env.local")}

${chalk_1.default.cyan("2. Next.js (Frontend Focus)")}
   ${chalk_1.default.gray("shadcn/ui + MyContext for Next.js projects")}

   Command: ${chalk_1.default.cyan("mycontext init my-app --framework nextjs")}

   What happens:
   ✅ Runs shadcn/ui init interactively
   ✅ Creates .mycontext/ project structure
   ⏭️ No database setup (bring your own backend)

${chalk_1.default.cyan("3. Basic (MyContext Only)")}
   ${chalk_1.default.gray("Minimal setup for any project type")}

   Command: ${chalk_1.default.cyan("mycontext init my-app --framework other")}

   What happens:
   ✅ Creates .mycontext/ project structure only
   ⏭️ No UI library or database setup

${chalk_1.default.yellow("Template File Locations:")}
  • Projects with src/: ${chalk_1.default.gray("src/lib/")}
  • Projects without src/: ${chalk_1.default.gray("lib/")}
  • Auto-detected during init

${chalk_1.default.yellow("Next Steps:")}
  ${chalk_1.default.cyan("mycontext help getting-started")}  # Quick start guide
  ${chalk_1.default.cyan("mycontext status")}                # Check project status`,
            },
            workflows: {
                title: "⚡ Workflows",
                content: `
MyContext provides automated workflows for different project types:

Available Workflows:
${workflowEngine_1.WorkflowEngine.getWorkflows()
                    .map((w) => `  ${chalk_1.default.cyan(w.id)}: ${w.name}
    ${w.description}
    Time: ${w.estimatedTotalTime}min, Steps: ${w.steps.length}`)
                    .join("\n")}

Usage:
  mycontext workflow list                    # List all workflows
  mycontext workflow suggest                 # Get recommendations
  mycontext workflow start <id> --auto-continue  # Run workflow
  mycontext workflow status                  # Check progress
  mycontext workflow continue                # Resume workflow`,
            },
            commands: {
                title: "📋 Available Commands",
                content: `
Core Commands:
  ${chalk_1.default.cyan("init <name> [--framework <type>]")}
    Initialize new project with framework support
    Examples:
      mycontext init my-app --framework instantdb  # Full stack (default)
      mycontext init my-app --framework nextjs     # Next.js + shadcn
      mycontext init my-app --framework other      # MyContext only

  ${chalk_1.default.cyan("setup-complete")}        Analyze and complete project setup
  ${chalk_1.default.cyan("generate <type>")}       Generate code, types, components, etc.
  ${chalk_1.default.cyan("workflow <action>")}     Manage automated workflows
  ${chalk_1.default.cyan("validate")}              Check project quality
  ${chalk_1.default.cyan("status")}                Show project status

Setup Commands:
  ${chalk_1.default.cyan("setup-shadcn")}          Configure shadcn/ui components
  ${chalk_1.default.cyan("setup-instantdb")}       Configure InstantDB integration
  ${chalk_1.default.cyan("setup-database")}        Set up database and authentication

Development Commands:
  ${chalk_1.default.cyan("generate-components")}   Create React components
  ${chalk_1.default.cyan("generate-context-files")} Generate project context files
  ${chalk_1.default.cyan("compile-prd")}           Create comprehensive PRD
  ${chalk_1.default.cyan("analyze <image>")}       Analyze screenshot (Gemini Vision)

Utility Commands:
  ${chalk_1.default.cyan("help [topic]")}          Show help (context-aware)
    Topics: getting-started, frameworks, workflows, commands, ai-providers
  ${chalk_1.default.cyan("status --check-health")} Check AI provider status`,
            },
            "ai-providers": {
                title: "🤖 AI Provider Configuration",
                content: `
MyContext supports multiple AI providers with automatic fallback:

Primary Setup (Recommended):
  export ANTHROPIC_API_KEY="your_key_here"
  mycontext status --check-health

Alternative Providers:
  export XAI_API_KEY="your_key_here"  # For Grok 4
  export MYCONTEXT_PROVIDER=xai

The system automatically:
- Tests provider connectivity
- Falls back on failures
- Optimizes for speed vs. quality
- Handles rate limits gracefully

Check status: mycontext status --check-health`,
            },
            troubleshooting: {
                title: "🔧 Troubleshooting",
                content: `
Common Issues:

❌ "No AI providers available"
   → Configure ANTHROPIC_API_KEY or XAI_API_KEY
   → Run: mycontext status --check-health

❌ "Command failed"
   → Check project structure: mycontext validate
   → Ensure all dependencies are installed

❌ "Workflow interrupted"
   → Resume with: mycontext workflow continue
   → Check status: mycontext workflow status

❌ "Component generation failed"
   → Try with different provider: export MYCONTEXT_PROVIDER=xai
   → Check API key validity

For detailed troubleshooting:
  mycontext status --verbose
  mycontext validate --detailed`,
            },
        };
        const topicData = topics[topic];
        if (!topicData) {
            console.log(chalk_1.default.red(`❌ Unknown help topic: ${topic}`));
            console.log(chalk_1.default.yellow("Available topics:"));
            Object.keys(topics).forEach((t) => {
                console.log(`  ${chalk_1.default.cyan(t)}`);
            });
            return;
        }
        console.log(chalk_1.default.blue(topicData.title));
        console.log();
        console.log(topicData.content.trim());
        if (verbose) {
            console.log();
            console.log(chalk_1.default.gray("💡 For context-aware help: mycontext help"));
        }
    }
}
exports.HelpCommand = HelpCommand;
//# sourceMappingURL=help.js.map