import TeamService from '../services/teamService.js';
import { getUserByToken } from '../utils/userUtils.js';
import { devLog } from '../utils/logger.js';

export const getTeams = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { clubid: clubId } = req.params;
    const { seasonid: seasonId } = req.query;
    
    const user = await getUserByToken(token);
    
    // Check if user has access to this club
    const teams = await TeamService.getAllTeamsByClub(clubId, seasonId);
    
    res.status(200).json(teams);
  } catch (error) {
    console.error('[getTeams] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};

export const getTeam = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { teamid: teamId } = req.params;
    
    const user = await getUserByToken(token);
    
    const team = await TeamService.getTeamById(teamId);
    if (!team) {
      return res.status(404).json({ error: "notfound" });
    }
    
    res.status(200).json(team);
  } catch (error) {
    console.error('[getTeam] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};

export const createTeam = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { clubid: clubId } = req.params;
    const { name, leagueId, seasonId } = req.body;
    
    const user = await getUserByToken(token);
    
    if (!name) {
      return res.status(400).json({ error: "missingname" });
    }
    
    const teamData = {
      name,
      clubId: parseInt(clubId),
      leagueId: leagueId ? parseInt(leagueId) : null,
      seasonId: seasonId ? parseInt(seasonId) : null
    };
    
    const newTeam = await TeamService.createTeam(teamData);
    
    res.status(201).json(newTeam);
  } catch (error) {
    console.error('[createTeam] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};

export const updateTeam = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { teamid: teamId } = req.params;
    const { name, leagueId, seasonId } = req.body;
    
    const user = await getUserByToken(token);
    
    const updateData = {};
    if (name !== undefined) updateData.name = name;
    if (leagueId !== undefined) updateData.leagueId = leagueId ? parseInt(leagueId) : null;
    if (seasonId !== undefined) updateData.seasonId = seasonId ? parseInt(seasonId) : null;
    
    const success = await TeamService.updateTeam(teamId, updateData);
    if (!success) {
      return res.status(404).json({ error: "notfound" });
    }
    
    const updatedTeam = await TeamService.getTeamById(teamId);
    res.status(200).json(updatedTeam);
  } catch (error) {
    console.error('[updateTeam] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};

export const deleteTeam = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { teamid: teamId } = req.params;
    
    const user = await getUserByToken(token);
    
    const success = await TeamService.deleteTeam(teamId);
    if (!success) {
      return res.status(404).json({ error: "notfound" });
    }
    
    res.status(200).json({ message: "deleted" });
  } catch (error) {
    console.error('[deleteTeam] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};

export const getLeagues = async (req, res) => {
  try {
    const { authcode: token } = req.headers;
    const { clubid: clubId } = req.params;
    const { seasonid: seasonId } = req.query;
    
    const user = await getUserByToken(token);
    
    const leagues = await TeamService.getLeaguesByClub(clubId, seasonId);
    
    res.status(200).json(leagues);
  } catch (error) {
    console.error('[getLeagues] - Error:', error);
    res.status(500).json({ error: "internalerror" });
  }
};
