const express = require('express'); const cors = require('cors'); const multer = require('multer'); const pdfParse = require('pdf-parse'); const app = express(); app.use(cors()); app.use(express.json()); const upload = multer({ storage: multer.memoryStorage() }); const uploads = {}; // PDF upload and text extraction app.post('/api/upload', upload.single('file'), async (req, res) => { try { console.log('📄 Processing PDF upload...'); if (!req.file) { return res.status(400).json({ error: 'No file uploaded' }); } if (req.file.mimetype !== 'application/pdf') { return res.status(400).json({ error: 'Please upload a PDF file' }); } // Extract text from PDF using pdf-parse console.log('🔍 Extracting text from PDF...'); const pdfData = await pdfParse(req.file.buffer); const sceneText = pdfData.text; if (!sceneText || sceneText.trim().length < 10) { return res.status(400).json({ error: 'Could not extract readable text from PDF' }); } const uploadId = Date.now().toString(); uploads[uploadId] = { filename: req.file.originalname, sceneText: sceneText.trim(), uploadTime: new Date() }; console.log(`✅ PDF processed: ${sceneText.length} characters extracted from ${req.file.originalname}`); res.json({ uploadId, filename: req.file.originalname, textLength: sceneText.length, preview: sceneText.substring(0, 200) + '...' }); } catch (error) { console.error('❌ PDF processing error:', error); res.status(500).json({ error: 'Failed to process PDF', details: error.message }); } }); // Generate acting guide app.post('/api/guides/generate', async (req, res) => { try { const { characterName, productionTitle, productionType, uploadId } = req.body; console.log(`🎭 Generating guide for: ${characterName}`); if (!uploadId || !uploads[uploadId]) { return res.status(400).json({ error: 'No script uploaded' }); } const script = uploads[uploadId].sceneText; // Analyze the script for character insights const lines = script.split('\n').filter(line => line.trim()); const characterLines = lines.filter(line => line.toUpperCase().includes(characterName.toUpperCase()) ); const dialogueLines = lines.filter(line => line.includes(':') || (line.length > 10 && !line.match(/^[A-Z\s]+$/)) ); // Create comprehensive acting guide const guideHtml = ` Acting Guide - ${characterName}

🎭 Acting Guide

${characterName}
${productionTitle} (${productionType})
Generated from: ${uploads[uploadId].filename}

📊 Script Analysis

${script.length}
Total Characters
${lines.length}
Total Lines
${characterLines.length}
${characterName} References
${dialogueLines.length}
Dialogue Lines

📝 Script Preview

${script.substring(0, 1500)}${script.length > 1500 ? '\n\n[Full script continues...]' : ''}

🎭 Character Analysis for ${characterName}

🎯 Key Focus Areas

💬 Dialogue Analysis

Based on the script, consider these aspects of ${characterName}'s communication style:

🎬 Scene Objectives

For each scene, identify:

🎯 Acting Preparation Tips

🧠 Character Development

🗣️ Voice & Physicality

🎭 Scene Work

Before each take or performance:

📚 Next Steps

  1. Read through the full script multiple times, focusing on ${characterName}
  2. Highlight every line your character speaks
  3. Make notes about their emotional state in each moment
  4. Practice with scene partners if possible
  5. Record yourself reading the lines and listen back

✨ Generated by PREP101 • ${new Date().toLocaleDateString()} ✨

Professional Acting Guide from PDF Audition Sides

`; console.log('✅ Acting guide generated successfully!'); res.json({ ok: true, guideHtml: guideHtml }); } catch (error) { console.error('❌ Guide generation error:', error); res.status(500).json({ error: 'Failed to generate guide', message: error.message }); } }); // Auth endpoints (keep these for login) app.post('/api/auth/login', (req, res) => { res.json({ token: 'fake-token', user: { id: 1, name: 'Test User', email: req.body.email, subscription: 'free', guidesUsed: 0, guidesLimit: 3 } }); }); app.get('/api/auth/me', (req, res) => { res.json({ id: 1, name: 'Test User', email: 'test@test.com', subscription: 'free', guidesUsed: 0, guidesLimit: 3 }); }); app.get('/api/guides/my-guides', (req, res) => { res.json([]); }); app.listen(5000, () => { console.log('🚀 PREP101 AI Backend running on port 5000'); console.log('📄 Ready to process PDF audition sides!'); console.log('🎭 Generating professional acting guides'); });