using Koogle.Application.Interfaces; using Koogle.Infrastructure.Data; using KoogleApp.Entities; using Koopgle.Application.DTOs; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AutoMapper; using Koogle.Domain.Interfaces; using Microsoft.EntityFrameworkCore; namespace Koogle.Application.Services { public class PlayerService : IPlayerService { private readonly IMapper _mapper; private readonly ApplicationDbContext _context; private readonly IPlayerRepository _playerRepository; private readonly ICurrentUserService _currentUserService; //private readonly IAuthorizationService _authorizationService; public PlayerService( IMapper mapper, ApplicationDbContext context, IPlayerRepository playerRepository, ICurrentUserService currentUserService //IAuthorizationService authorizationService ) { _mapper = mapper; _context = context; _playerRepository = playerRepository; _currentUserService = currentUserService; //_authorizationService = authorizationService; } public async Task> GetAllAsync(CancellationToken cancellationToken = default) { var companies = await _context.Players //.Include(c => c.Goals.Where(g => !g.IsDeleted)) //.Include(c => c.UserCompanies) .OrderBy(c => c.Name) .ToListAsync(cancellationToken); return _mapper.Map>(companies); } public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) { var goal = await _context.Players .Include(g => g.Name) .Include(g => g.PlayerStatus) //.Include(g => g.ParentGoal) //.Include(g => g.Tags) //.Include(g => g.GoalOwners) //.ThenInclude(go => go.User) //.Include(g => g.ActualValues.Where(av => !av.IsDeleted)) .FirstOrDefaultAsync(g => g.Id == id, cancellationToken); if (goal == null) return null; return _mapper.Map(goal); } public async Task CreateAsync(CreatePlayerDto dto, CancellationToken cancellationToken = default) { var existingPlayer = await _context.Players .FirstOrDefaultAsync(c => c.Name == dto.Name, cancellationToken); if (existingPlayer != null) { throw new InvalidOperationException($"Player with name '{dto.Name}' already exists"); } var goal = _mapper.Map(dto); await _playerRepository.AddAsync(goal, cancellationToken); return await GetByIdAsync(goal.Id, cancellationToken) ?? throw new InvalidOperationException("Failed to create player"); } public async Task UpdateAsync(int id, UpdatePlayerDto dto, CancellationToken cancellationToken = default) { var player = await _context.Players.FindAsync(new object[] { id }, cancellationToken) ?? throw new KeyNotFoundException($"Player with ID {id} not found"); // Prüfen ob anderer Firma mit diesem Namen existiert var existingPlayer = await _context.Players .FirstOrDefaultAsync(c => c.Name == dto.Name && c.Id != id, cancellationToken); if (existingPlayer != null) { throw new InvalidOperationException($"Player with name '{dto.Name}' already exists"); } _mapper.Map(dto, player); await _context.SaveChangesAsync(cancellationToken); return await GetByIdAsync(id, cancellationToken) ?? throw new InvalidOperationException("Failed to update player"); } public async Task DeleteAsync(int id, CancellationToken cancellationToken = default) { var company = await _context.Players //.Include(c => c.Goals) .FirstOrDefaultAsync(c => c.Id == id, cancellationToken) ?? throw new KeyNotFoundException($"Player with ID {id} not found"); //if (company.Goals.Any(g => !g.IsDeleted)) //{ // throw new InvalidOperationException("Cannot delete player with existing goals"); //} await _playerRepository.DeleteAsync(id, cancellationToken); } } }