using AutoMapper;
using SportsDivision.Application.DTOs;
using SportsDivision.Domain.Entities;

namespace SportsDivision.Application.Mappings;

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Zone, ZoneDto>()
            .ForMember(d => d.SchoolCount, o => o.MapFrom(s => s.Schools != null ? s.Schools.Count : 0));

        CreateMap<School, SchoolDto>()
            .ForMember(d => d.ZoneName, o => o.MapFrom(s => s.Zone != null ? s.Zone.Name : string.Empty))
            .ForMember(d => d.StudentCount, o => o.MapFrom(s => s.Students != null ? s.Students.Count : 0));
        CreateMap<SchoolCreateDto, School>();
        CreateMap<SchoolUpdateDto, School>();

        CreateMap<Student, StudentDto>()
            .ForMember(d => d.SchoolName, o => o.MapFrom(s => s.School != null ? s.School.Name : string.Empty))
            .ForMember(d => d.FullName, o => o.MapFrom(s => s.FullName));
        CreateMap<StudentCreateDto, Student>();
        CreateMap<StudentUpdateDto, Student>();

        CreateMap<Event, EventDto>();
        CreateMap<EventCreateDto, Event>();
        CreateMap<EventUpdateDto, Event>();

        CreateMap<EventLevel, EventLevelDto>();
        CreateMap<EventLevelCreateDto, EventLevel>();

        CreateMap<Tournament, TournamentDto>()
            .ForMember(d => d.ZoneName, o => o.MapFrom(s => s.Zone != null ? s.Zone.Name : null))
            .ForMember(d => d.EventCount, o => o.MapFrom(s => s.TournamentEventLevels != null ? s.TournamentEventLevels.Count : 0));
        CreateMap<TournamentCreateDto, Tournament>();
        CreateMap<TournamentUpdateDto, Tournament>();

        CreateMap<TournamentEventLevel, TournamentEventLevelDto>()
            .ForMember(d => d.TournamentName, o => o.MapFrom(s => s.Tournament != null ? s.Tournament.Name : string.Empty))
            .ForMember(d => d.EventName, o => o.MapFrom(s => s.Event != null ? s.Event.Name : string.Empty))
            .ForMember(d => d.Category, o => o.MapFrom(s => s.Event != null ? s.Event.Category : default))
            .ForMember(d => d.EventLevelName, o => o.MapFrom(s => s.EventLevel != null ? s.EventLevel.Name : string.Empty))
            .ForMember(d => d.RegistrationCount, o => o.MapFrom(s => s.Registrations != null ? s.Registrations.Count : 0));
        CreateMap<TournamentEventLevelCreateDto, TournamentEventLevel>();

        CreateMap<EventRegistration, EventRegistrationDto>()
            .ForMember(d => d.EventName, o => o.MapFrom(s => s.TournamentEventLevel != null && s.TournamentEventLevel.Event != null ? s.TournamentEventLevel.Event.Name : string.Empty))
            .ForMember(d => d.EventLevelName, o => o.MapFrom(s => s.TournamentEventLevel != null && s.TournamentEventLevel.EventLevel != null ? s.TournamentEventLevel.EventLevel.Name : string.Empty))
            .ForMember(d => d.StudentName, o => o.MapFrom(s => s.Student != null ? s.Student.FullName : null))
            .ForMember(d => d.SchoolName, o => o.MapFrom(s => s.Student != null && s.Student.School != null ? s.Student.School.Name : null));

        CreateMap<Score, ScoreDto>()
            .ForMember(d => d.StudentName, o => o.MapFrom(s => s.EventRegistration != null && s.EventRegistration.Student != null ? s.EventRegistration.Student.FullName : null))
            .ForMember(d => d.SchoolName, o => o.MapFrom(s => s.EventRegistration != null && s.EventRegistration.Student != null && s.EventRegistration.Student.School != null ? s.EventRegistration.Student.School.Name : null))
            .ForMember(d => d.EventName, o => o.MapFrom(s => s.EventRegistration != null && s.EventRegistration.TournamentEventLevel != null && s.EventRegistration.TournamentEventLevel.Event != null ? s.EventRegistration.TournamentEventLevel.Event.Name : null));

        CreateMap<Round, RoundDto>();
        CreateMap<RoundCreateDto, Round>();

        CreateMap<Heat, HeatDto>();

        CreateMap<HeatLane, HeatLaneDto>()
            .ForMember(d => d.StudentName, o => o.MapFrom(s => s.EventRegistration != null && s.EventRegistration.Student != null ? s.EventRegistration.Student.FullName : null))
            .ForMember(d => d.SchoolName, o => o.MapFrom(s => s.EventRegistration != null && s.EventRegistration.Student != null && s.EventRegistration.Student.School != null ? s.EventRegistration.Student.School.Name : null));

        CreateMap<HighJumpHeight, HighJumpHeightDto>();
        CreateMap<HighJumpAttempt, HighJumpAttemptDto>()
            .ForMember(d => d.StudentName, o => o.MapFrom(s => s.EventRegistration != null && s.EventRegistration.Student != null ? s.EventRegistration.Student.FullName : null))
            .ForMember(d => d.SchoolName, o => o.MapFrom(s => s.EventRegistration != null && s.EventRegistration.Student != null && s.EventRegistration.Student.School != null ? s.EventRegistration.Student.School.Name : null));

        CreateMap<ScoringConstant, ScoringConstantDto>()
            .ForMember(d => d.EventName, o => o.MapFrom(s => s.Event != null ? s.Event.Name : string.Empty));

        CreateMap<PlacementPointConfig, PlacementPointConfigDto>();
    }
}
