using AutoMapper;
using PARR.API.Contracts.V1.Requests;
using PARR.API.Contracts.V1.Requests.Queries;
using PARR.Domain.Common.Pagination;
using PARR.Domain.Entities.Job;

namespace PARR.API.MappingProfiles
{
    public class RequestToDomainProfile : Profile
    {
        public RequestToDomainProfile()
        {
            CreateMap<PaginationQuery, PaginationFilter>();

            #region Job

            CreateMap<JobRequest, Job>()
                .ForMember(d => d.Id, o => o.MapFrom(s => Guid.NewGuid()))
                .ForMember(d => d.DateCreated, o => o.MapFrom(s => DateTimeOffset.UtcNow))
                .AfterMap((s, d) =>
                {
                    if (d.UnitFilters != null)
                        foreach (var unitFilter in d.UnitFilters)
                            unitFilter.JobId = d.Id;
                });

            CreateMap<UnitFilterRequest, JobUnitFilter>()
                .ForMember(d => d.DateCreated, o => o.MapFrom(s => DateTimeOffset.UtcNow))
                .ForMember(d => d.UnitFilter, o => o.MapFrom(s => s.UnitFilterMask))
                .AfterMap((s, d) =>
                {
                    if (d.FieldFilters != null)
                        foreach (var fieldFilter in d.FieldFilters)
                            fieldFilter.UnitFilterId = d.Id;

                    if (d.RelationshipFilters != null)
                        foreach (var relationshipFilter in d.RelationshipFilters)
                            relationshipFilter.UnitFilterId = d.Id;
                });

            CreateMap<FieldFilterRequest, JobFieldFilter>()
                .ForMember(d => d.Id, o => o.MapFrom(s => Guid.NewGuid()))
                .ForMember(d => d.DateCreated, o => o.MapFrom(s => DateTimeOffset.UtcNow));

            CreateMap<RelationshipFilterRequest, JobRelationshipFilter>();

            #endregion
        }
    }
}
