import Team from '../models/Team.js';
import League from '../models/League.js';
import Club from '../models/Club.js';
import Season from '../models/Season.js';
import SeasonService from './seasonService.js';
import { devLog } from '../utils/logger.js';

class TeamService {
  static async getAllTeamsByClub(clubId, seasonId = null) {
    try {
      
      // Wenn keine Saison angegeben, verwende die aktuelle
      if (!seasonId) {
        const currentSeason = await SeasonService.getOrCreateCurrentSeason();
        seasonId = currentSeason.id;
      }
      
      const teams = await Team.findAll({
        where: { clubId, seasonId },
        include: [
          {
            model: League,
            as: 'league',
            attributes: ['id', 'name']
          },
          {
            model: Season,
            as: 'season',
            attributes: ['id', 'season']
          }
        ],
        order: [['name', 'ASC']]
      });
      return teams;
    } catch (error) {
      console.error('[TeamService.getAllTeamsByClub] - Error:', error);
      throw error;
    }
  }

  static async getTeamById(teamId) {
    try {
      const team = await Team.findByPk(teamId, {
        include: [
          {
            model: League,
            as: 'league',
            attributes: ['id', 'name']
          },
          {
            model: Club,
            as: 'club',
            attributes: ['id', 'name']
          },
          {
            model: Season,
            as: 'season',
            attributes: ['id', 'season']
          }
        ]
      });
      return team;
    } catch (error) {
      console.error('[TeamService.getTeamById] - Error:', error);
      throw error;
    }
  }

  static async createTeam(teamData) {
    try {
      
      // Wenn keine Saison angegeben, verwende die aktuelle
      if (!teamData.seasonId) {
        const currentSeason = await SeasonService.getOrCreateCurrentSeason();
        teamData.seasonId = currentSeason.id;
      }
      
      const team = await Team.create(teamData);
      return team;
    } catch (error) {
      console.error('[TeamService.createTeam] - Error:', error);
      throw error;
    }
  }

  static async updateTeam(teamId, updateData) {
    try {
      const [updatedRowsCount] = await Team.update(updateData, {
        where: { id: teamId }
      });
      return updatedRowsCount > 0;
    } catch (error) {
      console.error('[TeamService.updateTeam] - Error:', error);
      throw error;
    }
  }

  static async deleteTeam(teamId) {
    try {
      const deletedRowsCount = await Team.destroy({
        where: { id: teamId }
      });
      return deletedRowsCount > 0;
    } catch (error) {
      console.error('[TeamService.deleteTeam] - Error:', error);
      throw error;
    }
  }

  static async getLeaguesByClub(clubId, seasonId = null) {
    try {
      
      // Wenn keine Saison angegeben, verwende die aktuelle
      if (!seasonId) {
        const currentSeason = await SeasonService.getOrCreateCurrentSeason();
        seasonId = currentSeason.id;
      }
      
      const leagues = await League.findAll({
        where: { clubId, seasonId },
        attributes: ['id', 'name', 'seasonId'],
        order: [['name', 'ASC']]
      });
      return leagues;
    } catch (error) {
      console.error('[TeamService.getLeaguesByClub] - Error:', error);
      throw error;
    }
  }
}

export default TeamService;
