const express = require('express');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
require('dotenv').config();

const db = require('./db');
const { generateLicense } = require('./licenseManager');

const app = express();
const PORT = process.env.LICENSE_SERVER_PORT || 3006;

app.use(cors({
    origin: true,
    credentials: true
}));
app.use(express.json());

// Load Private Key for Generation
const privateKeyPath = path.join(__dirname, '../config/private_key.pem');
let privateKey = null;
try {
    if (fs.existsSync(privateKeyPath)) {
        privateKey = fs.readFileSync(privateKeyPath, 'utf8');
    } else {
        console.error('WARNING: private_key.pem not found in config. Generation will fail.');
    }
} catch (e) {
    console.error('Error loading private key:', e);
}

// 1. Get All Issued Licenses
app.get('/api/licenses', async (req, res) => {
    try {
        const [rows] = await db.query('SELECT * FROM issued_licenses ORDER BY created_at DESC');
        res.json(rows);
    } catch (err) {
        console.error(err);
        res.status(500).json({ error: 'Database error' });
    }
});

// 1.1 Delete License
app.delete('/api/licenses/:id', async (req, res) => {
    const { id } = req.params;
    try {
        await db.query('DELETE FROM issued_licenses WHERE id = ?', [id]);
        res.json({ success: true, message: 'License deleted' });
    } catch (err) {
        console.error(err);
        res.status(500).json({ error: 'Failed to delete license' });
    }
});

// 1.2 Activate License by Key (called by SmartIMS)
app.post('/api/licenses/activate', async (req, res) => {
    const { licenseKey } = req.body;
    if (!licenseKey) return res.status(400).json({ error: 'License key is required' });

    try {
        const [result] = await db.query('UPDATE issued_licenses SET status = "ACTIVATED" WHERE license_key = ?', [licenseKey]);
        if (result.affectedRows === 0) {
            return res.status(404).json({ error: 'License key not found' });
        }
        res.json({ success: true, message: 'License status updated to ACTIVATED' });
    } catch (err) {
        console.error(err);
        res.status(500).json({ error: 'Database error' });
    }
});

// 2. Generate New License
app.post('/api/licenses/generate', async (req, res) => {
    const { moduleCode, type, subscriberId, expiryDate } = req.body;

    if (!moduleCode || !type || !subscriberId) {
        return res.status(400).json({ error: 'Missing required fields' });
    }

    if (!privateKey) {
        return res.status(500).json({ error: 'Server key configuration error' });
    }

    try {
        // Generate the key
        // For DEV, we don't pass expiryDate to make it permanent
        const finalExpiryDate = type === 'dev' ? null : expiryDate;

        const licenseKey = generateLicense(
            moduleCode,
            type,
            subscriberId,
            null, // activationDays
            finalExpiryDate,
            privateKey
        );

        // Store in DB
        const sql = `
            INSERT INTO issued_licenses (module_code, license_key, license_type, subscriber_id, status, created_at)
            VALUES (?, ?, ?, ?, 'WAITING', NOW())
        `;
        await db.query(sql, [moduleCode, licenseKey, type, subscriberId]);

        res.json({ success: true, licenseKey });
    } catch (err) {
        console.error(err);
        res.status(500).json({ error: err.message || 'Failed to generate license' });
    }
});

app.listen(PORT, () => {
    console.log(`🚀 License Manager Server running on port ${PORT}`);
});
