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

const router = express.Router();

// Initialize skill system
const skillSystem = new SkillSystem();

// Get all skills
router.get('/', async (req, res) => {
  try {
    const skills = skillSystem.getAllSkills();
    res.json({
      success: true,
      skills
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Get skills by category
router.get('/category/:category', async (req, res) => {
  try {
    const { category } = req.params;
    const skills = skillSystem.getSkillsByCategory(category);
    
    res.json({
      success: true,
      skills
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

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

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

// Unlock skill
router.post('/unlock/:userId/:skillId', async (req, res) => {
  try {
    const { userId, skillId } = req.params;
    const result = skillSystem.unlockSkill(userId, skillId);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Get skill bonuses
router.get('/bonuses/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const bonuses = skillSystem.getSkillBonuses(userId);
    
    res.json({
      success: true,
      bonuses
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Award skill points
router.post('/points/award/: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 point amount required' 
      });
    }
    
    const result = skillSystem.awardSkillPoints(userId, amount);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Allocate skill point
router.post('/points/allocate/:userId/:skillId', async (req, res) => {
  try {
    const { userId, skillId } = req.params;
    const result = skillSystem.allocateSkillPoint(userId, skillId);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

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

module.exports = router;
