// Load Modules
const express = require('express');
const multer = require('multer');
const pdf = require('pdf-parse');
const { Document, Packer, Paragraph, TextRun } = require('docx');
const Anthropic = require('@anthropic-ai/sdk');
const mammoth = require('mammoth');

// Load Environment Variables
require('dotenv').config();

const router = express.Router();
const upload = multer({ storage: multer.memoryStorage() });

// initialize LLM
//TODO Update to have User Provide their own key
const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

// Extract resume text and return it for preview
router.post('/extract-resume', upload.single('resume'), async (req, res) => {
  try {
    if (!req.file) {
      return res.status(400).json({ error: 'No file uploaded' });
    }
    let extractedText = '';
    const fileBuffer = req.file.buffer;
    const fileType = req.file.mimetype;

    if (fileType === 'application/pdf') {
      const pdfData = await pdf(fileBuffer);
      extractedText = pdfData.text;
    } else if (
      fileType === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    ) {
      const result = await mammoth.extractRawText({ buffer: fileBuffer });
      extractedText = result.value;
    } else {
      return res.status(400).json({ error: 'Unsupported file type' });
    }
    // Load Resume Parser API Prompt Template
    const resume_parser_api = require('../data/resume_parser_api.json');

    // Replace placeholder with extracted resume text
    resume_parser_api.messages[0].content[0].text = resume_parser_api.messages[0].content[0].text.replace('{{resume}}', extractedText);

    // Send the extracted resume text to AI model
    const resumeResponse = await anthropic.messages.create(resume_parser_api);
    
    // Extract JSON-formatted profile from the response
    let candidateProfile;
    const responseText = resumeResponse.content[0].text;

    // First, extract from XML tags if present
    if (responseText.includes('<json_output>')) {
      candidateProfile = responseText.split('<json_output>')[1].split('</json_output>')[0].trim();
    } else {
      candidateProfile = responseText.trim();
    }

    // Then, strip any markdown code blocks that might be wrapping the JSON
    if (candidateProfile.includes('```json')) {
      candidateProfile = candidateProfile.split('```json')[1].split('```')[0].trim();
    } else if (candidateProfile.startsWith('```')) {
      candidateProfile = candidateProfile.split('```')[1].split('```')[0].trim();
    }

    // Try to extract JSON object if still not clean
    if (!candidateProfile.startsWith('{')) {
      const jsonMatch = candidateProfile.match(/\{[\s\S]*\}/);
      if (jsonMatch) {
        candidateProfile = jsonMatch[0];
      }
    }

    // Parse the JSON
    const parsedProfile = JSON.parse(candidateProfile);
    
    res.json({candidateProfile: parsedProfile });
   
  } catch (error) {
    console.error('Full error:', error);
    if (error instanceof SyntaxError) {
      console.error('JSON Parse Error - the extracted text was not valid JSON');
    }
    res.status(500).json({ error: 'Error extracting resume text', details: error.message });
  }
});

// Handle Resume upload and user input for Job Description
router.post('/', async (req, res) => {
  try {
    // Load Variables
    const { candidateProfile, jobDescription, keyPoints } = req.body;
    const coverLetterAPI = require('../data/cover_letter_api.json');
    const resumeTailoringAPI = require('../data/resume_tailoring_api.json');

    // Replace API placeholders
    coverLetterAPI.messages[0].content[0].text = coverLetterAPI.messages[0].content[0].text
      .replace('{{resume_json}}', candidateProfile)
      .replace('{{job_description}}', jobDescription)
      .replace('{{key_points}}', keyPoints )
      .replace('{{date}}', new Date().toDateString());


    resumeTailoringAPI.messages[0].content[0].text = resumeTailoringAPI.messages[0].content[0].text
      .replace('{{resume_json}}', candidateProfile)
      .replace('{{job_description}}', jobDescription)

    // Send Messages to LLM
    const coverLetterResponse = await anthropic.messages.create(coverLetterAPI);
    const tailoredResumeResponse = await anthropic.messages.create(resumeTailoringAPI);

    // Extract Relevant Responses
    const coverLetterRawText = coverLetterResponse.content[0].text.split('<cover_letter>')[1].split('</cover_letter>')[0].trim();
    const tailoredResumeText = tailoredResumeResponse.content[0].text.split('<tailored_resume>')[1].split('</tailored_resume>')[0].trim();
    const resumeChanges = tailoredResumeResponse.content[0].text.split('<key_changes>')[1].split('</key_changes>')[0].trim();


    res.json({ coverLetter: coverLetterRawText, tailoredResumeText, resumeChanges });
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: 'Error generating cover letter' });
  }
});

function generateDocx(rawText) {
  if (!rawText || typeof rawText !== "string") {
    throw new Error("Invalid resume text provided.");
  }

  const paragraphs = [];
  
  // Split text into lines
  rawText.split("\n").forEach(line => {
    if (line.trim().startsWith("•")) {
      // Format bullet points properly
      paragraphs.push(
        new Paragraph({
          text: line.trim(), // Keep bullet point text
          bullet: { level: 0 } // Apply bullet point formatting
        })
      );
    } else if (line.trim() === "") {
      // Add spacing for empty lines
      paragraphs.push(new Paragraph({ text: "" }));
    } else {
      // Format normal text
      paragraphs.push(
        new Paragraph({
          children: [
            new TextRun(line.trim()),
            new TextRun("\n")
          ]
        })
      );
    }
  });

  // Create the document
  const doc = new Document({
    sections: [{ properties: {}, children: paragraphs }]
  });

  return Packer.toBuffer(doc);
}


// Route to download cover letter
router.post('/download-cover-letter', async (req, res) => {
  try {
      const { content } = req.body;
      const docBuffer = await generateDocx(content);

      res.setHeader('Content-Disposition', 'attachment; filename="cover_letter.docx"');
      res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

      res.send(docBuffer);
  } catch (error) {
      console.error(error);
      res.status(500).json({ error: 'Error generating document' });
  }
});

// Route to download tailored resume
router.post('/download-resume', async (req, res) => {
  try {
      const { content } = req.body;
      const docBuffer = await generateDocx(content);

      res.setHeader('Content-Disposition', 'attachment; filename="tailored_resume.docx"');
      res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

      res.send(docBuffer);
  } catch (error) {
      console.error(error);
      res.status(500).json({ error: 'Error generating document' });
  }
});


module.exports = router;