"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const client_1 = require("@prisma/client");
const VpnService_1 = require("../services/VpnService");
const router = (0, express_1.Router)();
const prisma = new client_1.PrismaClient();
const vpnService = new VpnService_1.VpnService();
// Helper to generate a VPN IP for a new device
async function getNextVpnIp() {
    const baseIp = '10.10.0.';
    // Find the highest VPN IP currently in use
    const lastDevice = await prisma.device.findFirst({
        where: { vpnIp: { startsWith: baseIp } },
        orderBy: { vpnIp: 'desc' }
    });
    if (!lastDevice || !lastDevice.vpnIp) {
        return `${baseIp}1`; // Start with 10.10.0.1
    }
    const lastOctet = parseInt(lastDevice.vpnIp.split('.')[3], 10);
    if (lastOctet >= 254) {
        throw new Error('VPN IP pool exhausted in this subnet');
    }
    return `${baseIp}${lastOctet + 1}`;
}
// Get all devices
router.get('/', async (req, res) => {
    try {
        const devices = await prisma.device.findMany({ include: { tenant: true } });
        res.json(devices);
    }
    catch (error) {
        res.status(500).json({ error: 'Internal server error' });
    }
});
// Create device for a tenant
router.post('/', async (req, res) => {
    try {
        const { tenantId, name, macAddress, opnsenseUrl, apiKey, apiSecret } = req.body;
        // Automatically generate the VPN IP for this customer device
        const nextVpnIp = await getNextVpnIp();
        const newDevice = await prisma.device.create({
            data: {
                tenantId,
                name,
                macAddress,
                vpnIp: nextVpnIp,
                opnsenseUrl,
                apiKey,
                apiSecret
            },
        });
        // Trigger OpenVPN config generation via SSH to the VPN Server (10.0.1.150)
        try {
            await vpnService.generateDeviceVpnConfig(macAddress, nextVpnIp);
            console.log(`Successfully generated VPN config for ${macAddress} at ${nextVpnIp}`);
        }
        catch (vpnErr) {
            console.error('Failed to generate VPN config:', vpnErr);
            // We don't fail the API request entirely, but log the error
        }
        res.status(201).json(newDevice);
    }
    catch (error) {
        console.error(error);
        res.status(500).json({ error: 'Internal server error' });
    }
});
exports.default = router;
