const express = require('express');
const router = express.Router();
const db = require('../../database');
const { calculateStagger } = require('../../utils/scheduler');
const { syncToPterodactyl, auditServerSchedules, deleteSchedule, syncAllForNode, sleep } = require('../../lib/ptero-sync');

// GET /admin/scheduler - Main page
router.get('/', async (req, res) => {
    try {
        // Get config for both nodes
        const configResult = await db.query('SELECT * FROM global_restart_config ORDER BY node');
        const configs = configResult.rows;
        
        // Get all servers ordered by node and sort_order
        const serversResult = await db.query(`
            SELECT s.*, c.base_time, c.interval_minutes 
            FROM server_restart_schedules s
            JOIN global_restart_config c ON s.node = c.node
            ORDER BY s.node, s.sort_order
        `);
        
        res.render('admin/scheduler', {
            title: 'Global Restart Scheduler',
            configs,
            servers: serversResult.rows
        });
    } catch (err) {
        console.error('Scheduler page error:', err);
        res.status(500).send('Error loading scheduler');
    }
});

// GET /admin/scheduler/table-only - HTMX partial refresh
router.get('/table-only', async (req, res) => {
    try {
        const serversResult = await db.query(`
            SELECT s.*, c.base_time, c.interval_minutes 
            FROM server_restart_schedules s
            JOIN global_restart_config c ON s.node = c.node
            ORDER BY s.node, s.sort_order
        `);
        
        const servers = serversResult.rows;
        
        let html = `<table class="w-full text-left">
            <thead class="bg-gray-100 dark:bg-darkbg text-gray-600 dark:text-gray-300">
                <tr>
                    <th class="p-3 w-10"></th>
                    <th class="p-3">Server</th>
                    <th class="p-3">Node</th>
                    <th class="p-3">Restart Time (Central)</th>
                    <th class="p-3">Status</th>
                    <th class="p-3">Skip</th>
                </tr>
            </thead>
            <tbody id="sortable-servers">`;
        
        if (servers.length === 0) {
            html += `<tr><td colspan="6" class="p-6 text-center text-gray-500">No servers imported yet. Click "Import Servers" to populate from Pterodactyl.</td></tr>`;
        } else {
            servers.forEach(server => {
                const nodeClass = server.node === 'TX1' ? 'bg-fire/20 text-fire' : 'bg-frost/20 text-frost';
                let statusHtml;
                if (server.sync_status === 'SUCCESS') {
                    statusHtml = `<span class="text-green-500">● Synced</span>`;
                } else if (server.sync_status === 'FAILED') {
                    statusHtml = `<span class="text-red-500" title="${server.last_error || ''}">✕ Error</span>`;
                } else {
                    statusHtml = `<span class="text-yellow-500">○ Pending</span>`;
                }
                const skipClass = server.skip_restart ? 'bg-red-600 text-white' : 'bg-gray-200 dark:bg-gray-700 text-gray-600 dark:text-gray-300';
                const skipText = server.skip_restart ? 'Skipped' : 'Active';
                
                html += `<tr class="border-t border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-800 transition" data-id="${server.server_id}">
                    <td class="p-3 cursor-grab text-gray-400 hover:text-gray-900 dark:hover:text-white"><span class="drag-handle text-lg">☰</span></td>
                    <td class="p-3 font-medium">${server.server_name}</td>
                    <td class="p-3"><span class="px-2 py-1 rounded text-xs font-bold ${nodeClass}">${server.node}</span></td>
                    <td class="p-3 font-mono text-sm">${server.effective_time || 'Not set'}</td>
                    <td class="p-3 text-sm">${statusHtml}</td>
                    <td class="p-3">
                        <button hx-post="/admin/scheduler/toggle-skip/${server.server_id}" hx-swap="none" hx-on::after-request="htmx.ajax('GET', '/admin/scheduler/table-only', '#scheduler-table')" class="px-2 py-1 rounded text-xs ${skipClass}">${skipText}</button>
                    </td>
                </tr>`;
            });
        }
        
        html += `</tbody></table>`;
        res.send(html);
    } catch (err) {
        res.status(500).send('Error loading table');
    }
});

// POST /admin/scheduler/reorder-servers - Handle drag-and-drop reorder
router.post('/reorder-servers', async (req, res) => {
    try {
        const { orderedIds } = req.body;
        
        // Update sort_order for each server
        for (let i = 0; i < orderedIds.length; i++) {
            await db.query(
                'UPDATE server_restart_schedules SET sort_order = $1 WHERE server_id = $2',
                [i, orderedIds[i]]
            );
        }
        
        // Recalculate effective times for each node
        for (const node of ['TX1', 'NC1']) {
            const configResult = await db.query(
                'SELECT base_time, interval_minutes FROM global_restart_config WHERE node = $1',
                [node]
            );
            
            if (configResult.rows.length === 0) continue;
            
            const { base_time, interval_minutes } = configResult.rows[0];
            
            const serversResult = await db.query(
                'SELECT server_id FROM server_restart_schedules WHERE node = $1 ORDER BY sort_order',
                [node]
            );
            
            const servers = serversResult.rows;
            const staggered = calculateStagger(base_time, interval_minutes, servers);
            
            for (const server of staggered) {
                await db.query(
                    'UPDATE server_restart_schedules SET effective_time = $1 WHERE server_id = $2',
                    [server.effective_time, server.server_id]
                );
            }
        }
        
        res.json({ success: true });
    } catch (err) {
        console.error('Reorder error:', err);
        res.status(500).json({ error: err.message });
    }
});

// POST /admin/scheduler/update-config - Update node config
router.post('/update-config', async (req, res) => {
    try {
        let { node, base_time, interval_minutes } = req.body;
        const updatedBy = req.session?.user?.username || 'Unknown';
        
        // Normalize time to HH:mm:ss format
        if (base_time && !base_time.includes(':00', 3)) {
            base_time = base_time + ':00';
        }
        
        await db.query(
            `UPDATE global_restart_config 
             SET base_time = $1, interval_minutes = $2, updated_at = NOW(), updated_by = $3 
             WHERE node = $4`,
            [base_time, interval_minutes, updatedBy, node]
        );
        
        // Recalculate effective times for this node
        const serversResult = await db.query(
            'SELECT server_id FROM server_restart_schedules WHERE node = $1 ORDER BY sort_order',
            [node]
        );
        
        const servers = serversResult.rows;
        const staggered = calculateStagger(base_time, interval_minutes, servers);
        
        for (const server of staggered) {
            await db.query(
                'UPDATE server_restart_schedules SET effective_time = $1, sync_status = $2 WHERE server_id = $3',
                [server.effective_time, 'PENDING', server.server_id]
            );
        }
        
        res.redirect('/admin/scheduler');
    } catch (err) {
        console.error('Update config error:', err);
        res.status(500).send('Error updating config');
    }
});

// POST /admin/scheduler/sync/:node - Sync all servers for a node
router.post('/sync/:node', async (req, res) => {
    try {
        const { node } = req.params;
        const results = await syncAllForNode(node);
        
        const success = results.filter(r => r.success).length;
        const failed = results.filter(r => !r.success).length;
        
        res.json({ success: true, synced: success, failed });
    } catch (err) {
        console.error('Sync error:', err);
        res.status(500).json({ error: err.message });
    }
});

// GET /admin/scheduler/audit/:node - Audit a node for rogue schedules
router.get('/audit/:node', async (req, res) => {
    try {
        const { node } = req.params;
        
        const serversResult = await db.query(
            'SELECT server_id, server_name FROM server_restart_schedules WHERE node = $1',
            [node]
        );
        
        const results = [];
        let totalRogue = 0;
        
        for (const server of serversResult.rows) {
            const auditResult = await auditServerSchedules(server.server_id, server.server_name);
            if (auditResult.rogueSchedules.length > 0) {
                results.push(auditResult);
                totalRogue += auditResult.rogueSchedules.length;
            }
            await sleep(200); // Rate limiting
        }
        
        let html = `<div id="audit-modal" class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-70 backdrop-blur-sm">
            <div class="bg-white dark:bg-darkcard border ${totalRogue > 0 ? 'border-red-500' : 'border-green-500'} rounded-lg shadow-2xl w-full max-w-2xl p-6 relative">`;
        
        if (totalRogue > 0) {
            const nukePayload = [];
            results.forEach(r => r.rogueSchedules.forEach(s => nukePayload.push({
                serverId: r.serverId, scheduleId: s.id, scheduleName: s.name
            })));
            
            html += `<h2 class="text-2xl font-bold text-red-500 mb-2">⚠ Conflicts Detected</h2>
                <p class="text-gray-600 dark:text-gray-300 mb-4">
                    Found <strong class="text-gray-900 dark:text-white">${totalRogue}</strong> rogue restart schedule(s) across 
                    <strong class="text-gray-900 dark:text-white">${results.length}</strong> server(s) on ${node}. 
                    These must be removed before Trinity can take control.
                </p>
                <div class="bg-gray-100 dark:bg-darkbg rounded p-4 mb-6 max-h-64 overflow-y-auto border border-gray-200 dark:border-gray-700">
                    <ul class="space-y-3">`;
            
            results.forEach(result => {
                html += `<li class="border-b border-gray-200 dark:border-gray-700 pb-2 last:border-0">
                    <span class="text-fire font-semibold">${result.serverName}</span>
                    <ul class="ml-4 mt-1 text-sm text-gray-500 dark:text-gray-400">`;
                result.rogueSchedules.forEach(sched => {
                    html += `<li>- "${sched.name}" (Cron: ${sched.cron})</li>`;
                });
                html += `</ul></li>`;
            });
            
            html += `</ul></div>
                <form hx-post="/admin/scheduler/audit/nuke/${node}" hx-target="#audit-modal" hx-swap="outerHTML">
                    <input type="hidden" name="nukeData" value='${JSON.stringify(nukePayload)}'>
                    <div class="flex justify-end gap-4 mt-6">
                        <button type="button" onclick="document.getElementById('audit-modal').remove()" 
                                class="px-4 py-2 text-gray-500 hover:text-gray-900 dark:hover:text-white transition">Cancel</button>
                        <button type="submit" class="bg-red-600 hover:bg-red-500 text-white px-6 py-2 rounded font-bold transition">
                            🔥 Nuke ${totalRogue} Schedules
                        </button>
                    </div>
                </form>`;
        } else {
            html += `<h2 class="text-2xl font-bold text-green-500 mb-2">✓ All Clear</h2>
                <p class="text-gray-600 dark:text-gray-300 mb-6">No conflicts found on ${node}. Trinity is ready to take control.</p>
                <div class="flex justify-end">
                    <button type="button" onclick="document.getElementById('audit-modal').remove()" 
                            class="bg-green-600 hover:bg-green-500 text-white px-6 py-2 rounded transition">Close</button>
                </div>`;
        }
        
        html += `</div></div>`;
        res.send(html);
    } catch (err) {
        console.error('Audit error:', err);
        res.status(500).send('Error running audit');
    }
});

// POST /admin/scheduler/audit/nuke/:node - Delete all rogue schedules
router.post('/audit/nuke/:node', async (req, res) => {
    try {
        const { node } = req.params;
        const nukeData = JSON.parse(req.body.nukeData);
        
        let deleted = 0;
        let failed = 0;
        
        for (const item of nukeData) {
            const result = await deleteSchedule(item.serverId, item.scheduleId, item.scheduleName);
            if (result.success) {
                deleted++;
            } else {
                failed++;
            }
            await sleep(200); // Rate limiting
        }
        
        // Return success message as modal replacement
        res.send(`
            <div id="audit-modal" class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-70 backdrop-blur-sm">
                <div class="bg-slate-900 border border-green-500 rounded-lg shadow-2xl w-full max-w-md p-6">
                    <h2 class="text-2xl font-bold text-green-400 mb-4">✓ Cleanup Complete</h2>
                    <p class="text-slate-300 mb-6">
                        Deleted <strong class="text-white">${deleted}</strong> rogue schedule(s) on ${node}.
                        ${failed > 0 ? `<br><span class="text-red-400">${failed} failed.</span>` : ''}
                    </p>
                    <div class="flex justify-end">
                        <button type="button" onclick="document.getElementById('audit-modal').remove(); htmx.ajax('GET', '/admin/scheduler/table-only', '#scheduler-table');" 
                                class="bg-green-600 hover:bg-green-500 text-white px-6 py-2 rounded transition">Done</button>
                    </div>
                </div>
            </div>
        `);
    } catch (err) {
        console.error('Nuke error:', err);
        res.status(500).send('Error deleting schedules');
    }
});

// POST /admin/scheduler/toggle-skip/:serverId - Toggle skip_restart
router.post('/toggle-skip/:serverId', async (req, res) => {
    try {
        const { serverId } = req.params;
        
        await db.query(
            'UPDATE server_restart_schedules SET skip_restart = NOT skip_restart, sync_status = $1 WHERE server_id = $2',
            ['PENDING', serverId]
        );
        
        res.json({ success: true });
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

// POST /admin/scheduler/import-servers - Import servers from Pterodactyl
router.post('/import-servers', async (req, res) => {
    try {
        // Use discovery to get servers
        const { getMinecraftServers } = require('../../panel/discovery');
        const servers = await getMinecraftServers();
        
        let imported = 0;
        
        for (const server of servers) {
            const node = server.node || 'TX1'; // Default to TX1 if unknown
            
            // Check if server already exists
            const existing = await db.query(
                'SELECT id FROM server_restart_schedules WHERE server_id = $1',
                [server.identifier]
            );
            
            if (existing.rows.length === 0) {
                // Get current count for this node for sort_order
                const countResult = await db.query(
                    'SELECT COUNT(*) as count FROM server_restart_schedules WHERE node = $1',
                    [node]
                );
                const sortOrder = parseInt(countResult.rows[0].count);
                
                await db.query(
                    `INSERT INTO server_restart_schedules (server_id, server_name, node, sort_order)
                     VALUES ($1, $2, $3, $4)`,
                    [server.identifier, server.name, node, sortOrder]
                );
                imported++;
            }
        }
        
        // Recalculate effective times
        for (const node of ['TX1', 'NC1']) {
            const configResult = await db.query(
                'SELECT base_time, interval_minutes FROM global_restart_config WHERE node = $1',
                [node]
            );
            
            if (configResult.rows.length === 0) continue;
            
            const { base_time, interval_minutes } = configResult.rows[0];
            
            const serversResult = await db.query(
                'SELECT server_id FROM server_restart_schedules WHERE node = $1 ORDER BY sort_order',
                [node]
            );
            
            const staggered = calculateStagger(base_time, interval_minutes, serversResult.rows);
            
            for (const server of staggered) {
                await db.query(
                    'UPDATE server_restart_schedules SET effective_time = $1 WHERE server_id = $2',
                    [server.effective_time, server.server_id]
                );
            }
        }
        
        res.json({ success: true, imported });
    } catch (err) {
        console.error('Import error:', err);
        res.status(500).json({ error: err.message });
    }
});

module.exports = router;
