import TaxiService from '../services/taxiService.js';

function extractHashedUserId(req) {
    return req.headers?.userid;
}

class TaxiController {
  constructor() {
    this.taxiService = new TaxiService();
  }

  // Spielstand laden
  async getGameState(req, res) {
    try {
      const hashedUserId = extractHashedUserId(req);
      const gameState = await this.taxiService.getGameState(hashedUserId);
      res.json({ success: true, data: gameState });
    } catch (error) {
      console.error('Error getting taxi game state:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Laden des Spielstands' });
    }
  }

  // Spielstand speichern
  async saveGameState(req, res) {
    try {
      const userId = extractHashedUserId(req);
      const { level, score, money, passengersDelivered, fuel } = req.body;
      
      const gameState = await this.taxiService.saveGameState(userId, {
        level,
        score,
        money,
        passengersDelivered,
        fuel
      });
      
      res.json({ success: true, data: gameState });
    } catch (error) {
      console.error('Error saving taxi game state:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Speichern des Spielstands' });
    }
  }

  // Level-Statistiken abrufen
  async getLevelStats(req, res) {
    try {
      const userId = extractHashedUserId(req);
      const { level } = req.params;
      
      const stats = await this.taxiService.getLevelStats(userId, parseInt(level));
      res.json({ success: true, data: stats });
    } catch (error) {
      console.error('Error getting level stats:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Laden der Level-Statistiken' });
    }
  }

  // Bestenliste abrufen
  async getLeaderboard(req, res) {
    try {
      const { type = 'score', limit = 10 } = req.query;
      const leaderboard = await this.taxiService.getLeaderboard(type, parseInt(limit));
      res.json({ success: true, data: leaderboard });
    } catch (error) {
      console.error('Error getting leaderboard:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Laden der Bestenliste' });
    }
  }

  // Spiel beenden und Punkte verarbeiten
  async finishGame(req, res) {
    try {
      const userId = extractHashedUserId(req);
      const { finalScore, finalMoney, passengersDelivered, level } = req.body;
      
      const result = await this.taxiService.finishGame(userId, {
        finalScore,
        finalMoney,
        passengersDelivered,
        level
      });
      
      res.json({ success: true, data: result });
    } catch (error) {
      console.error('Error finishing game:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Beenden des Spiels' });
    }
  }

  // Level freischalten
  async unlockLevel(req, res) {
    try {
      const userId = extractHashedUserId(req);
      const { level } = req.body;
      
      const result = await this.taxiService.unlockLevel(userId, level);
      res.json({ success: true, data: result });
    } catch (error) {
      console.error('Error unlocking level:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Freischalten des Levels' });
    }
  }

  // Spieler-Statistiken abrufen
  async getPlayerStats(req, res) {
    try {
      const userId = extractHashedUserId(req);
      const stats = await this.taxiService.getPlayerStats(userId);
      res.json({ success: true, data: stats });
    } catch (error) {
      console.error('Error getting player stats:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Laden der Spieler-Statistiken' });
    }
  }

  // Level zurücksetzen
  async resetLevel(req, res) {
    try {
      const userId = extractHashedUserId(req);
      const { level } = req.body;
      
      const result = await this.taxiService.resetLevel(userId, level);
      res.json({ success: true, data: result });
    } catch (error) {
      console.error('Error resetting level:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Zurücksetzen des Levels' });
    }
  }

  // Alle Spielstände zurücksetzen
  async resetAllProgress(req, res) {
    try {
      const userId = extractHashedUserId(req);
      const result = await this.taxiService.resetAllProgress(userId);
      res.json({ success: true, data: result });
    } catch (error) {
      console.error('Error resetting all progress:', error);
      res.status(500).json({ success: false, message: 'Fehler beim Zurücksetzen aller Fortschritte' });
    }
  }
}

export default TaxiController;
