import SeasonService from '../services/seasonService.js';
import { getUserByToken } from '../utils/userUtils.js';
import { devLog } from '../utils/logger.js';

export const getSeasons = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const user = await getUserByToken(token);
    
    const seasons = await SeasonService.getAllSeasons();
    
    res.status(200).json(seasons);
  } catch (error) {
    console.error('[getSeasons] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};

export const getCurrentSeason = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const user = await getUserByToken(token);
    
    const season = await SeasonService.getOrCreateCurrentSeason();
    
    res.status(200).json(season);
  } catch (error) {
    console.error('[getCurrentSeason] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};

export const createSeason = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { season } = req.body;
    
    const user = await getUserByToken(token);
    
    if (!season) {
      return res.status(400).json({ error: "missingseason" });
    }
    
    // Validiere Saison-Format (z.B. "2023/2024")
    const seasonRegex = /^\d{4}\/\d{4}$/;
    if (!seasonRegex.test(season)) {
      return res.status(400).json({ error: "invalidseasonformat" });
    }
    
    const newSeason = await SeasonService.createSeason(season);
    
    res.status(201).json(newSeason);
  } catch (error) {
    console.error('[createSeason] - Error:', error);
    if (error.message === 'Season already exists') {
      res.status(409).json({ error: "alreadyexists" });
    } else {
      res.status(500).json({ error: "internalerror" });
    }
  }
};

export const getSeason = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { seasonid: seasonId } = req.params;
    
    const user = await getUserByToken(token);
    
    const season = await SeasonService.getSeasonById(seasonId);
    if (!season) {
      return res.status(404).json({ error: "notfound" });
    }
    
    res.status(200).json(season);
  } catch (error) {
    console.error('[getSeason] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};

export const deleteSeason = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { seasonid: seasonId } = req.params;
    
    const user = await getUserByToken(token);
    
    const success = await SeasonService.deleteSeason(seasonId);
    if (!success) {
      return res.status(404).json({ error: "notfound" });
    }
    
    res.status(200).json({ message: "deleted" });
  } catch (error) {
    console.error('[deleteSeason] - Error:', error);
    if (error.message === 'Season is used by teams' || error.message === 'Season is used by leagues') {
      res.status(409).json({ error: "seasoninuse" });
    } else {
      res.status(500).json({ error: "internalerror" });
    }
  }
};
