const express = require('express');
const logger = require('../utils/logger');
const ModService = require('../services/ModService');

const router = express.Router();

// Get all mods
router.get('/', async (req, res) => {
  try {
    const { enabledOnly } = req.query;
    const mods = await ModService.getMods(enabledOnly === 'true');
    
    res.json({
      success: true,
      mods
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error getting mods:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get specific mod
router.get('/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const mod = await ModService.getMod(parseInt(id));
    
    if (!mod) {
      return res.status(404).json({ error: 'Mod not found' });
    }
    
    res.json({
      success: true,
      mod
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error getting mod:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Enable mod
router.post('/:id/enable', async (req, res) => {
  try {
    const { id } = req.params;
    const success = await ModService.enableMod(parseInt(id));
    
    if (!success) {
      return res.status(404).json({ error: 'Mod not found' });
    }
    
    res.json({
      success: true,
      message: 'Mod enabled successfully'
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error enabling mod:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Disable mod
router.post('/:id/disable', async (req, res) => {
  try {
    const { id } = req.params;
    const success = await ModService.disableMod(parseInt(id));
    
    if (!success) {
      return res.status(404).json({ error: 'Mod not found' });
    }
    
    res.json({
      success: true,
      message: 'Mod disabled successfully'
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error disabling mod:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get mod assets by type
router.get('/assets/:type', async (req, res) => {
  try {
    const { type } = req.params;
    const assets = await ModService.getModAssets(type);
    
    res.json({
      success: true,
      assets
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error getting mod assets:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get specific mod asset
router.get('/assets/:type/:assetId', async (req, res) => {
  try {
    const { type, assetId } = req.params;
    const asset = await ModService.getModAsset(type, assetId);
    
    if (!asset) {
      return res.status(404).json({ error: 'Asset not found' });
    }
    
    res.json({
      success: true,
      asset
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error getting mod asset:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get mod load order
router.get('/load-order', async (req, res) => {
  try {
    const loadOrder = await ModService.getLoadOrder();
    
    res.json({
      success: true,
      loadOrder
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error getting load order:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Set mod load order
router.post('/load-order', async (req, res) => {
  try {
    const { modIds } = req.body;
    
    if (!Array.isArray(modIds)) {
      return res.status(400).json({ error: 'modIds must be an array' });
    }
    
    const success = await ModService.setLoadOrder(modIds);
    
    if (!success) {
      return res.status(500).json({ error: 'Failed to set load order' });
    }
    
    res.json({
      success: true,
      message: 'Load order updated successfully'
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error setting load order:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Get server mod preferences
router.get('/preferences/server', async (req, res) => {
  try {
    const preferences = await ModService.getServerPreferences();
    
    res.json({
      success: true,
      preferences
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error getting server preferences:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Set server mod preference
router.post('/preferences/server', async (req, res) => {
  try {
    const { key, value } = req.body;
    
    if (!key || value === undefined) {
      return res.status(400).json({ error: 'key and value are required' });
    }
    
    const success = await ModService.setServerPreference(key, value);
    
    if (!success) {
      return res.status(500).json({ error: 'Failed to set preference' });
    }
    
    res.json({
      success: true,
      message: 'Preference set successfully'
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error setting server preference:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

// Reload mods from filesystem
router.post('/reload', async (req, res) => {
  try {
    await ModService.reloadMods();
    
    res.json({
      success: true,
      message: 'Mods reloaded successfully'
    });
  } catch (error) {
    logger.error('[GAME SERVER MODS API] Error reloading mods:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

module.exports = router;
