import { NextRequest, NextResponse } from 'next/server';
import { exec } from 'child_process';
import { promisify } from 'util';
import { getApiUrl } from '@/lib/utils/api-url';

const execAsync = promisify(exec);

/**
 * Intelligent V1 Launch Planning
 * Analyzes ACTUAL codebase to generate specific, actionable tasks
 */
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ projectId: string }> }
) {
  try {
    const { projectId } = await params;

    // 1. Load project context
    const contextResponse = await fetch(
      getApiUrl(`/api/projects/${projectId}/context`, request)
    );
    const context = await contextResponse.json();

    // 2. Scan actual codebase structure
    const repoPath = '/Users/markhenderson/ai-proxy';
    const { stdout: pagesOutput } = await execAsync(
      `cd "${repoPath}" && find vibn-frontend/app -name "*.tsx" | grep "page.tsx" | wc -l`
    );
    const { stdout: apiOutput } = await execAsync(
      `cd "${repoPath}" && find vibn-frontend/app/api -name "route.ts" | wc -l`
    );
    const { stdout: componentsOutput } = await execAsync(
      `cd "${repoPath}" && find vibn-frontend/components -name "*.tsx" 2>/dev/null | wc -l || echo 0`
    );

    const codebaseStats = {
      totalPages: parseInt(pagesOutput.trim()),
      totalAPIs: parseInt(apiOutput.trim()),
      totalComponents: parseInt(componentsOutput.trim())
    };

    // 3. Analyze what's ACTUALLY built vs vision
    const analysis = await analyzeRealCodebase(context, codebaseStats, repoPath);

    // 4. Generate intelligent, specific tasks
    const intelligentPlan = generateIntelligentPlan(context, analysis);

    return NextResponse.json({
      projectContext: {
        name: context.project.name,
        vision: context.project.vision
      },
      codebaseAnalysis: analysis,
      intelligentPlan,
      confidence: analysis.confidence
    });

  } catch (error) {
    console.error('Error generating intelligent plan:', error);
    return NextResponse.json(
      { 
        error: 'Failed to generate intelligent plan',
        details: error instanceof Error ? error.message : String(error)
      },
      { status: 500 }
    );
  }
}

async function analyzeRealCodebase(context: any, stats: any, repoPath: string) {
  const analysis: any = {
    builtFeatures: [],
    missingFeatures: [],
    confidence: 'high',
    specificInsights: []
  };

  // Analyze actual file structure
  const { stdout: pagesListOutput } = await execAsync(
    `cd "${repoPath}" && find vibn-frontend/app -name "page.tsx" | sed 's|vibn-frontend/app/||' | sed 's|/page.tsx||'`
  );
  const actualPages = pagesListOutput.trim().split('\n').filter(p => p);

  const { stdout: apiListOutput } = await execAsync(
    `cd "${repoPath}" && find vibn-frontend/app/api -name "route.ts" | sed 's|vibn-frontend/app/api/||' | sed 's|/route.ts||'`
  );
  const actualAPIs = apiListOutput.trim().split('\n').filter(a => a);

  // Analyze based on vision: "VIBN gets your project to v1 launch and beyond"
  const vision = context.project.vision || '';

  // Check for key features mentioned in vision
  const visionKeywords = {
    'project plan': { pages: ['plan', 'getting-started'], apis: ['plan/'] },
    'marketing automation': { pages: ['marketing'], apis: ['plan/marketing'] },
    'communication automation': { pages: ['communication', 'chat'], apis: ['ai/chat'] },
    'ai chat support': { pages: ['chat', 'v_ai_chat'], apis: ['ai/chat'] }
  };

  // Analyze what's built
  if (actualPages.some(p => p.includes('plan') || p.includes('getting-started'))) {
    analysis.builtFeatures.push({
      name: 'Project Planning System',
      evidence: actualPages.filter(p => p.includes('plan')).slice(0, 3),
      status: 'built'
    });
  }

  if (actualPages.some(p => p.includes('v_ai_chat')) && actualAPIs.some(a => a.includes('ai/chat'))) {
    analysis.builtFeatures.push({
      name: 'AI Chat Interface',
      evidence: ['v_ai_chat page', 'ai/chat API'],
      status: 'built'
    });
  }

  if (actualAPIs.some(a => a.includes('github/'))) {
    analysis.builtFeatures.push({
      name: 'GitHub Integration',
      evidence: actualAPIs.filter(a => a.includes('github/')),
      status: 'built'
    });
  }

  if (actualAPIs.some(a => a.includes('cursor/'))) {
    analysis.builtFeatures.push({
      name: 'Cursor History Import',
      evidence: actualAPIs.filter(a => a.includes('cursor/')),
      status: 'built'
    });
  }

  if (actualAPIs.some(a => a.includes('sessions/'))) {
    analysis.builtFeatures.push({
      name: 'Session Tracking',
      evidence: ['sessions/track', 'sessions/associate-project'],
      status: 'built'
    });
  }

  if (actualPages.some(p => p.includes('audit'))) {
    analysis.builtFeatures.push({
      name: 'Project Audit Report',
      evidence: ['audit page', 'audit/generate API'],
      status: 'built'
    });
  }

  // Identify gaps based on vision
  if (vision.includes('marketing automation') && !actualPages.some(p => p.includes('marketing'))) {
    analysis.missingFeatures.push({
      name: 'Marketing Automation UI',
      reason: 'Mentioned in vision but no UI found',
      priority: 'high'
    });
  }

  if (vision.includes('communication automation')) {
    const hasCommAutomation = actualAPIs.some(a => 
      a.includes('email') || a.includes('slack') || a.includes('notification')
    );
    if (!hasCommAutomation) {
      analysis.missingFeatures.push({
        name: 'Communication Automation',
        reason: 'Mentioned in vision but no APIs found',
        priority: 'high'
      });
    }
  }

  // Check for production readiness
  if (!actualAPIs.some(a => a.includes('health') || a.includes('status'))) {
    analysis.missingFeatures.push({
      name: 'Health Check Endpoint',
      reason: 'Needed for production monitoring',
      priority: 'medium'
    });
  }

  // Check for onboarding
  const hasOnboarding = actualPages.some(p => p.includes('getting-started') || p.includes('onboarding'));
  if (hasOnboarding) {
    analysis.builtFeatures.push({
      name: 'User Onboarding Flow',
      evidence: actualPages.filter(p => p.includes('getting-started')),
      status: 'built'
    });
  } else {
    analysis.missingFeatures.push({
      name: 'User Onboarding Tutorial',
      reason: 'Critical for first-time users',
      priority: 'high'
    });
  }

  // Check for task management
  const hasTaskUI = actualPages.some(p => p.includes('task') || p.includes('checklist') || p.includes('todo'));
  if (!hasTaskUI && actualAPIs.some(a => a.includes('plan/'))) {
    analysis.missingFeatures.push({
      name: 'Task Management UI',
      reason: 'Have plan APIs but no UI to track tasks',
      priority: 'high'
    });
  }

  // Specific insights from commit history
  const recentCommits = context.codebase?.topFiles || [];
  if (recentCommits.length > 0) {
    analysis.specificInsights.push(
      `Recently worked on: ${recentCommits.slice(0, 3).map((f: any) => f.filePath.split('/').pop()).join(', ')}`
    );
  }

  // Activity insights
  const topFiles = context.activity?.topEditedFiles || [];
  if (topFiles.length > 0) {
    const topFile = topFiles[0].file.split('/').pop();
    analysis.specificInsights.push(`Most edited: ${topFile} (${topFiles[0].count} times)`);
  }

  return analysis;
}

function generateIntelligentPlan(context: any, analysis: any) {
  const plan = {
    summary: `Based on ${analysis.builtFeatures.length} built features and ${analysis.missingFeatures.length} identified gaps`,
    categories: [] as any[]
  };

  // Product Completion (based on what's actually missing)
  const productTasks = [];
  for (const missing of analysis.missingFeatures) {
    if (missing.name === 'Task Management UI') {
      productTasks.push({
        id: `prod-task-ui`,
        title: 'Build Task Management UI',
        description: `You have plan/simulate API but no UI. Create a checklist interface to show and track V1 launch tasks.`,
        status: 'pending',
        priority: missing.priority,
        specificTo: 'Your codebase has the backend but missing frontend'
      });
    }
    if (missing.name === 'Marketing Automation UI') {
      productTasks.push({
        id: `prod-mkt-ui`,
        title: 'Build Marketing Automation Dashboard',
        description: `Your vision mentions marketing automation. Create UI for /plan/marketing API to manage campaigns.`,
        status: 'pending',
        priority: missing.priority,
        specificTo: 'Mentioned in your vision statement'
      });
    }
    if (missing.name === 'Communication Automation') {
      productTasks.push({
        id: `prod-comm-auto`,
        title: 'Add Communication Automation',
        description: `Build email/Slack notification system for project updates and milestones.`,
        status: 'pending',
        priority: missing.priority,
        specificTo: 'Core to your vision: "communication automation"'
      });
    }
    if (missing.name === 'User Onboarding Tutorial') {
      productTasks.push({
        id: `prod-onboard`,
        title: 'Create Interactive Onboarding',
        description: `Guide new users through: 1) New vs existing project, 2) GitHub connect, 3) Run Cursor import, 4) Define vision.`,
        status: 'pending',
        priority: missing.priority,
        specificTo: 'Your vision flow from earlier conversation'
      });
    }
  }

  if (productTasks.length > 0) {
    plan.categories.push({
      name: 'Product Completion',
      status: 'in_progress',
      description: 'Missing features identified from your codebase and vision',
      tasks: productTasks
    });
  }

  // Polish Existing Features (based on what's built but might need work)
  const polishTasks = [];
  for (const built of analysis.builtFeatures) {
    if (built.name === 'Project Planning System') {
      polishTasks.push({
        id: 'polish-plan',
        title: 'Connect Planning APIs to UI',
        description: `You have /plan/mvp, /plan/marketing, /plan/simulate APIs. Ensure they're all wired to your ${built.evidence.length} planning pages.`,
        status: 'in_progress',
        priority: 'high',
        specificTo: `Found ${built.evidence.length} planning pages in your codebase`
      });
    }
  }

  if (polishTasks.length > 0) {
    plan.categories.push({
      name: 'Polish & Integration',
      status: 'in_progress',
      description: 'Connect your existing features together',
      tasks: polishTasks
    });
  }

  // Launch Readiness (production concerns)
  const launchTasks = [
    {
      id: 'launch-monitoring',
      title: 'Add Production Monitoring',
      description: `Add health check endpoint and error tracking for your ${context.codebase?.totalCommits} commits of code.`,
      status: 'pending',
      priority: 'high',
      specificTo: 'Your 104k lines of code need monitoring'
    },
    {
      id: 'launch-docs',
      title: 'Document All Features',
      description: `Create docs for your ${analysis.builtFeatures.length} built features: ${analysis.builtFeatures.map((f: any) => f.name).join(', ')}.`,
      status: 'pending',
      priority: 'medium',
      specificTo: `Specific to your ${analysis.builtFeatures.length} features`
    },
    {
      id: 'launch-demo',
      title: 'Create Demo Video',
      description: `Show: GitHub import → Cursor analysis → AI chat → Launch plan. Highlight your unique value.`,
      status: 'pending',
      priority: 'high',
      specificTo: 'Your specific user journey'
    }
  ];

  plan.categories.push({
    name: 'Launch Preparation',
    status: 'pending',
    description: 'Get ready for public launch',
    tasks: launchTasks
  });

  return plan;
}

