using AutoMapper;
using Lada.Framework.Data.Domains.Rewards;
using Lada.Framework.Infrastructures.Helpers;
using Ladaer.BackEnd.Infrastructures.DTO.Items.Rewards;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Rewards;

namespace Ladaer.BackEnd.Infrastructures.DTO.Profiles
{
    public class RewardPenaltyMappingProfile : Profile
    {
        public RewardPenaltyMappingProfile()
        {
            #region RewardPenaltyTicket
            CreateMap<RewardPenaltyTicket, RewardPenaltyTicketItem>()
                .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.User != null ? src.User.FullName : string.Empty))
                .ForMember(dest => dest.UserName, opt => opt.MapFrom(src => src.User != null ? src.User.UserName : string.Empty));
            CreateMap<RewardPenaltyTicketItem, RewardPenaltyTicket>();
            CreateMap<RewardPenaltyTicket, RewardPenaltyTicketModel>();
            CreateMap<RewardPenaltyTicketModel, RewardPenaltyTicket>();
            #endregion

            #region AutoPenaltyConfig
            CreateMap<AutoPenaltyConfig, AutoPenaltyConfigItem>()
                .ForMember(dest => dest.CreatedTime, opt => opt.MapFrom(src => src.CreatedTime ?? DateTimeOffset.MinValue))
                .ForMember(dest => dest.CreatedByUserName, opt => opt.MapFrom(src => src.CreatedByUserName ?? string.Empty))
                .ForMember(dest => dest.LevelCount, opt => opt.MapFrom(src => src.Levels != null ? src.Levels.Count : 0));
            #endregion
        }
    }

    public static class RewardPenaltyMappingExtensions
    {
        // RewardPenaltyTicket extensions
        public static RewardPenaltyTicketItem ToItem(this RewardPenaltyTicket entity)
            => entity.MapTo<RewardPenaltyTicket, RewardPenaltyTicketItem>();

        public static RewardPenaltyTicketModel ToModel(this RewardPenaltyTicket entity)
            => entity.MapTo<RewardPenaltyTicket, RewardPenaltyTicketModel>();

        public static RewardPenaltyTicket ToEntity(this RewardPenaltyTicketModel model)
            => model.MapTo<RewardPenaltyTicketModel, RewardPenaltyTicket>();

        // AutoPenaltyConfig extensions
        public static AutoPenaltyConfigItem ToItem(this AutoPenaltyConfig entity)
            => entity.MapTo<AutoPenaltyConfig, AutoPenaltyConfigItem>();
    }
}