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

const router = express.Router();

// Initialize quest system
const questSystem = new QuestSystem();

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

// Get quests by type
router.get('/type/:type', async (req, res) => {
  try {
    const { type } = req.params;
    const quests = questSystem.getQuestsByType(type);
    
    res.json({
      success: true,
      quests
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

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

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

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

// Update quest progress
router.post('/progress/:userId/:questId', async (req, res) => {
  try {
    const { userId, questId } = req.params;
    const { progressUpdates } = req.body;
    
    if (!progressUpdates) {
      return res.status(400).json({ 
        success: false, 
        error: 'Progress updates required' 
      });
    }
    
    const result = questSystem.updateQuestProgress(userId, questId, progressUpdates);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Complete quest
router.post('/complete/:userId/:questId', async (req, res) => {
  try {
    const { userId, questId } = req.params;
    const result = questSystem.completeQuest(userId, questId);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

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

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

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

// Reset daily quests
router.post('/reset/daily/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const result = questSystem.resetDailyQuests(userId);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

// Reset weekly quests
router.post('/reset/weekly/:userId', async (req, res) => {
  try {
    const { userId } = req.params;
    const result = questSystem.resetWeeklyQuests(userId);
    
    res.json({
      success: true,
      result
    });
  } catch (error) {
    res.status(500).json({ 
      success: false, 
      error: error.message 
    });
  }
});

module.exports = router;
