const express = require('express');
const cors = require('cors');
const https = require('https');
const fs = require('fs');
const app = express();




// 1. GLOBAL CORS: This handles both standard and "preflight" (OPTIONS) requests.
// We removed the app.options('*') line that was causing your crash.
app.use(cors());

app.use(express.json());

// 2. SSL CONFIGURATION: Use your existing Let's Encrypt certificates.
// Ensure these paths match your VPS exactly.
const sslOptions = {
  key: fs.readFileSync('/etc/letsencrypt/live/stream.ktono.com/privkey.pem'),
  cert: fs.readFileSync('/etc/letsencrypt/live/stream.ktono.com/fullchain.pem')
};

// 3. YOUR API ROUTE
app.post('/ask', async (req, res) => {
    try {
        const { text, voice } = req.body;
        // ... Your existing logic for audio/OpenAI goes here ...
        res.json({ success: true, message: "Securely connected to Node.js!" });
    } catch (error) {
        console.error("Error:", error);
        res.status(500).json({ error: "Internal Server Error" });
    }
});

// 4. THE SECURE SERVER: Create HTTPS server on port 3000
const PORT = 3000;
https.createServer(sslOptions, app).listen(PORT, '0.0.0.0', () => {
    console.log(`✅ Secure server running at https://stream.ktono`)});
