import { NextResponse } from 'next/server';

const DEEPSEEK_API_URL = 'https://api.deepseek.com/v1/chat/completions';
const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY;

export async function POST(request) {
  const startTime = Date.now();
  
  try {
    const { topic, contents, headings } = await request.json();

    // Validation
    if (!topic?.trim()) {
      return NextResponse.json(
        { success: false, error: 'Topic is required' },
        { status: 400 }
      );
    }

    if (!contents || !Array.isArray(contents) || contents.length === 0) {
      return NextResponse.json(
        { success: false, error: 'At least one content source is required' },
        { status: 400 }
      );
    }

    if (!headings || !Array.isArray(headings) || headings.length === 0) {
      return NextResponse.json(
        { success: false, error: 'At least one heading is required' },
        { status: 400 }
      );
    }

    // Filter empty content and headings
    const filteredContents = contents.filter(c => c?.trim()).map(c => c.trim());
    const filteredHeadings = headings.filter(h => h?.trim()).map(h => h.trim());

    if (filteredContents.length === 0) {
      return NextResponse.json(
        { success: false, error: 'Please add some content' },
        { status: 400 }
      );
    }

    if (filteredHeadings.length === 0) {
      return NextResponse.json(
        { success: false, error: 'Please add some headings' },
        { status: 400 }
      );
    }

    // Check API key
    if (!DEEPSEEK_API_KEY) {
      return NextResponse.json({
        success: true,
        data: generateDemoData(topic, filteredContents, filteredHeadings),
        warning: 'Using demo data - Set DEEPSEEK_API_KEY for real extraction',
        processingTime: (Date.now() - startTime) / 1000
      });
    }

    // Prepare combined content
    const combinedContent = filteredContents.map((c, i) => 
      `--- CONTENT SOURCE ${i + 1} ---\n${c}`
    ).join('\n\n');

    // Create comprehensive prompt
    const prompt = createComprehensivePrompt(topic, combinedContent, filteredHeadings);

    // Call DeepSeek API
    const response = await fetch(DEEPSEEK_API_URL, {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${DEEPSEEK_API_KEY}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        model: 'deepseek-chat',
        messages: [
          {
            role: 'system',
            content: `You are an advanced semantic entity extraction engine operating with Google NLP, BERT, and MUM-style understanding.

IMPORTANT RULES:
1. Extract EVERYTHING relevant - no arbitrary limits
2. Be exhaustive and comprehensive
3. Include both major and minor entities
4. Capture numbers, statistics, names, dates, technologies
5. Each entity should have: entity name, context, source, relevance (1-10), type
6. Place under most appropriate heading
7. Return valid JSON with the exact structure specified`
          },
          {
            role: 'user',
            content: prompt
          }
        ],
        temperature: 0.1,
        max_tokens: 8000,
        top_p: 0.1,
        frequency_penalty: 0,
        presence_penalty: 0
      }),
    });

    if (!response.ok) {
      const errorText = await response.text();
      console.error('DeepSeek API error:', response.status, errorText);
      throw new Error(`AI service error: ${response.status}`);
    }

    const data = await response.json();
    const aiResponse = data.choices[0]?.message?.content;
    
    // Parse AI response
    let extractedData;
    try {
      const jsonMatch = aiResponse.match(/```json\n([\s\S]*?)\n```/) || 
                       aiResponse.match(/```([\s\S]*?)```/) ||
                       [null, aiResponse];
      
      let jsonString = jsonMatch[1] || jsonMatch[0];
      
      // Find JSON object if not in code blocks
      if (!jsonString.includes('{')) {
        const startIdx = aiResponse.indexOf('{');
        const endIdx = aiResponse.lastIndexOf('}');
        if (startIdx !== -1 && endIdx !== -1) {
          jsonString = aiResponse.substring(startIdx, endIdx + 1);
        }
      }
      
      jsonString = jsonString.trim();
      jsonString = jsonString.replace(/,\s*([}\]])/g, '$1');
      
      extractedData = JSON.parse(jsonString);
      
      // Validate structure
      if (!extractedData.entitiesByHeading) {
        extractedData.entitiesByHeading = {};
      }
      
      // Ensure all headings have arrays
      filteredHeadings.forEach(heading => {
        if (!extractedData.entitiesByHeading[heading]) {
          extractedData.entitiesByHeading[heading] = [];
        }
      });
      
      // Clean up entities
      Object.keys(extractedData.entitiesByHeading).forEach(heading => {
        if (!Array.isArray(extractedData.entitiesByHeading[heading])) {
          extractedData.entitiesByHeading[heading] = [];
        }
        
        // Remove duplicates
        const seen = new Set();
        extractedData.entitiesByHeading[heading] = extractedData.entitiesByHeading[heading]
          .filter(entity => {
            if (!entity || !entity.entity) return false;
            const key = entity.entity.toLowerCase().trim();
            if (seen.has(key)) return false;
            seen.add(key);
            return true;
          })
          .sort((a, b) => (b.relevance || 0) - (a.relevance || 0));
      });
      
    } catch (parseError) {
      console.error('Failed to parse AI response:', parseError);
      console.log('Raw response (first 1000 chars):', aiResponse.substring(0, 1000));
      extractedData = generateDemoData(topic, filteredContents, filteredHeadings);
    }

    // Add metadata
    const result = {
      ...extractedData,
      originalTopic: topic,
      originalHeadings: filteredHeadings,
      originalContents: filteredContents,
      processingTime: ((Date.now() - startTime) / 1000).toFixed(2),
      timestamp: new Date().toISOString(),
      totalEntities: Object.values(extractedData.entitiesByHeading || {}).reduce((sum, arr) => sum + arr.length, 0)
    };

    return NextResponse.json({
      success: true,
      data: result
    });

  } catch (error) {
    console.error('Entity extraction error:', error);
    
    // Return comprehensive demo data on error
    const { topic, contents, headings } = await request.json().catch(() => ({
      topic: 'Error Topic',
      contents: ['Error content'],
      headings: ['Error heading']
    }));
    
    return NextResponse.json({
      success: true,
      data: generateDemoData(
        topic || 'Sample Topic',
        Array.isArray(contents) ? contents : ['Sample content'],
        Array.isArray(headings) ? headings : ['Sample heading']
      ),
      warning: 'Using demo data due to processing error',
      processingTime: ((Date.now() - Date.now()) / 1000).toFixed(2)
    });
  }
}

// Create comprehensive prompt
function createComprehensivePrompt(topic, content, headings) {
  return `
  You are an advanced semantic entity extraction engine operating with Google NLP, BERT, and MUM-style understanding.
CRITICAL: EXTRACT ALL ENTITIES WITHOUT ANY LIMITS. BE EXHAUSTIVE.

TOPIC: ${topic}

HEADINGS FOR ORGANIZATION (USE THESE EXACT NAMES):
${headings.map((h, i) => `${i + 1}. "${h}"`).join('\n')}

CONTENT TO ANALYZE (MULTIPLE SOURCES):
${content}

TASK OBJECTIVE:
Extract ONLY semantically, contextually, and topically relevant entities from the provided content.

STRICT RULES:
- Use ONLY the provided content
- Do NOT invent or infer entities not present or implied
- Extract entities based on meaning, not grammar
- Treat each heading as an isolated semantic cluster
- Every extracted term must be relevant to BOTH:
  1. The heading it belongs to
  2. The overall topic

Extract entities across THREE TIERS:

Tier 1: Core entities (primary topic-defining terms)
Tier 2: Supporting entities (closely related concepts, processes, benefits)
Tier 3: Contextual entities (descriptive, intent-based, long-tail concepts)

ENTITY TYPES TO INCLUDE:
- Named entities
- Conceptual entities
- Commercial intent entities
- Action-oriented entities
- Informational entities
- Contextual synonyms and semantic expansions

OUTPUT FORMAT REQUIREMENTS:
- One heading (H3) followed by its extracted entities
- Entities must be comma-separated
- No bullets, no explanations, no paragraphs
- Shuffle entities naturally
- Do NOT repeat entities
- Count and mention total entities per heading

IMPORTANT:
If a word is contextually important but not a named entity, INCLUDE it.
If a word appears important grammatically but irrelevant semantically, EXCLUDE it.
Include ALL three tiers in the final output.
Do not make separate sections of entities, provide me all combine comma separated
Do NOT limit extraction to only high-salience entities.

FINAL OUTPUT MUST BE CLEAN, SEO-READY, AND MACHINE-USABLE.

RETURN VALID JSON WITH THIS EXACT STRUCTURE:
{
  "summary": "Brief overall summary",
  "missingCategories": ["suggested heading 1", "suggested heading 2"],
  "entitiesByHeading": {
    "${headings[0]}": [
      {
        "entity": "Detailed entity description",
        "context": "Supporting context/details",
        "source": "1",
        "relevance": 8,
        "type": "Technology"
      },
      // ADD AS MANY AS YOU FIND - MINIMUM 15 PER HEADING IF CONTEXT ALLOWS
    ],
    "${headings[1]}": [
      // ADD ALL ENTITIES FOR THIS HEADING
    ]
    // INCLUDE ALL HEADINGS
  }
}

REMEMBER: EXTRACT EVERYTHING. NO LIMITS.
`;
}

// Generate comprehensive demo data
function generateDemoData(topic, contents, headings) {
  const demoEntities = {};
  
  // Entity types for variety
  const entityTypes = [
    "Technology", "Concept", "Benefit", "Application", "Tool", 
    "Framework", "Statistic", "Challenge", "Solution", "Trend",
    "Platform", "Methodology", "Standard", "Protocol", "Principle"
  ];
  
  // Generate 15-25 entities per heading
  headings.forEach(heading => {
    const entityCount = 15 + Math.floor(Math.random() * 11); // 15-25 entities
    
    const entities = [];
    for (let i = 1; i <= entityCount; i++) {
      entities.push({
        entity: `${heading} - Entity ${i}: Detailed specific information about this aspect`,
        context: `This entity represents important information extracted from the content. It provides context about how this relates to ${topic}. This is demo entity ${i} showing comprehensive extraction.`,
        source: `${Math.floor(Math.random() * contents.length) + 1}`,
        relevance: 5 + Math.floor(Math.random() * 6), // 5-10
        type: entityTypes[Math.floor(Math.random() * entityTypes.length)]
      });
    }
    
    // Sort by relevance
    entities.sort((a, b) => b.relevance - a.relevance);
    demoEntities[heading] = entities;
  });
  
  // Calculate total entities
  const totalEntities = Object.values(demoEntities).reduce((sum, arr) => sum + arr.length, 0);
  
  return {
    summary: `Comprehensive entity extraction for "${topic}" covering ${headings.length} categories. This demo shows how AI extracts and organizes information from multiple content sources.`,
    missingCategories: [
      "Future Developments",
      "Case Studies", 
      "Implementation Guidelines",
      "Cost Analysis"
    ],
    entitiesByHeading: demoEntities,
    originalTopic: topic,
    originalHeadings: headings,
    originalContents: contents,
    processingTime: (0.5 + Math.random() * 1).toFixed(2),
    timestamp: new Date().toISOString(),
    totalEntities: totalEntities,
    isDemoData: true
  };
}