const express = require('express');
const BaseSystem = require('../systems/BaseSystem');

const router = express.Router();

// Initialize base system
const baseSystem = new BaseSystem();

// Get player base
router.get('/player/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const base = baseSystem.getPlayerBase(userId);
    
    res.json({
      success: true,
      base
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Get base statistics
router.get('/stats/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const stats = baseSystem.getBaseStatistics(userId);
    
    res.json({
      success: true,
      stats
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Add experience to base
router.post('/experience/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const { amount } = req.body;
    
    if (!amount || amount <= 0) {
      return res.status(400).json({ 
        success: false, 
        error: 'Valid experience amount required' 
      });
    }
    
    const result = baseSystem.addExperience(userId, amount);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Build room
router.post('/build/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const { roomType, level = 1 } = req.body;
    
    if (!roomType) {
      return res.status(400).json({ 
        success: false, 
        error: 'Room type required' 
      });
    }
    
    const result = baseSystem.buildRoom(userId, roomType, level);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Upgrade room
router.post('/upgrade/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const { roomType } = req.body;
    
    if (!roomType) {
      return res.status(400).json({ 
        success: false, 
        error: 'Room type required' 
      });
    }
    
    const result = baseSystem.upgradeRoom(userId, roomType);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Demolish room
router.post('/demolish/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const { roomType } = req.body;
    
    if (!roomType) {
      return res.status(400).json({ 
        success: false, 
        error: 'Room type required' 
      });
    }
    
    const result = baseSystem.demolishRoom(userId, roomType);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Add decoration
router.post('/decoration/add/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const { decorationType } = req.body;
    
    if (!decorationType) {
      return res.status(400).json({ 
        success: false, 
        error: 'Decoration type required' 
      });
    }
    
    const result = baseSystem.addDecoration(userId, decorationType);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Remove decoration
router.post('/decoration/remove/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const { decorationIndex } = req.body;
    
    if (decorationIndex === undefined || decorationIndex === null) {
      return res.status(400).json({ 
        success: false, 
        error: 'Decoration index required' 
      });
    }
    
    const result = baseSystem.removeDecoration(userId, decorationIndex);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Get available rooms
router.get('/rooms/available/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const rooms = baseSystem.getAvailableRooms(userId);
    
    res.json({
      success: true,
      rooms
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Get room upgrades
router.get('/rooms/upgrades/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const upgrades = baseSystem.getRoomUpgrades(userId);
    
    res.json({
      success: true,
      upgrades
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

module.exports = router;
