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

const router = express.Router();

// Initialize dungeon system
const dungeonSystem = new DungeonSystem();

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

// Get dungeons by difficulty
router.get('/difficulty/:difficulty', async (req, res) => {
  try {
    const { difficulty } = req.params;
    const dungeons = dungeonSystem.getDungeonsByDifficulty(difficulty);
    
    res.json({
      success: true,
      dungeons
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Get available dungeons for player
router.get('/available/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const { playerLevel = 1, playerCount = 1 } = req.query;
    const dungeons = dungeonSystem.getAvailableDungeons(parseInt(playerLevel), parseInt(playerCount));
    
    res.json({
      success: true,
      dungeons
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Create dungeon instance
router.post('/create/:dungeonId/:creatorId', async (req, res) => {
  try {
    const { dungeonId, creatorId } = req.params;
    const { playerIds = [] } = req.body;
    
    const instance = dungeonSystem.createInstance(dungeonId, creatorId, playerIds);
    
    res.json({
      success: true,
      instance
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Get dungeon instance
router.get('/instance/:instanceId', async (req, res) => {
  try {
    const { instanceId } = req.params;
    const instance = dungeonSystem.getInstance(instanceId);
    
    if (!instance) {
      return res.status(404).json({ 
        success: false, 
        error: 'Instance not found' 
      });
    }
    
    res.json({
      success: true,
      instance
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Get player's current instance
router.get('/player/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const instance = dungeonSystem.getPlayerInstance(userId);
    
    if (!instance) {
      return res.status(404).json({ 
        success: false, 
        error: 'No active instance found' 
      });
    }
    
    res.json({
      success: true,
      instance
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Join dungeon instance
router.post('/join/:instanceId/:userId', async (req, res) => {
  try {
    const { instanceId, userId } = req.params;
    const instance = dungeonSystem.joinInstance(instanceId, userId);
    
    res.json({
      success: true,
      instance
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Leave dungeon instance
router.post('/leave/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const instance = dungeonSystem.leaveInstance(userId);
    
    res.json({
      success: true,
      instance
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Start encounter
router.post('/encounter/start/:instanceId/:userId', async (req, res) => {
  try {
    const { instanceId, userId } = req.params;
    const result = dungeonSystem.startEncounter(instanceId, userId);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Complete encounter
router.post('/encounter/complete/:instanceId/:userId', async (req, res) => {
  try {
    const { instanceId, userId } = req.params;
    const { result } = req.body;
    
    if (!result) {
      return res.status(400).json({ 
        success: false, 
        error: 'Encounter result required' 
      });
    }
    
    const encounterResult = dungeonSystem.completeEncounter(instanceId, userId, result);
    
    res.json({
      success: true,
      result: encounterResult
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Complete dungeon
router.post('/complete/:instanceId', async (req, res) => {
  try {
    const { instanceId } = req.params;
    const result = dungeonSystem.completeDungeon(instanceId);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

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

// Cleanup expired instances
router.post('/cleanup', async (req, res) => {
  try {
    const cleanedCount = dungeonSystem.cleanupExpiredInstances();
    
    res.json({
      success: true,
      cleanedCount
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

module.exports = router;
