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

namespace Ladaer.BackEnd.Infrastructures.DTO.Profiles
{
    public class CrmMappingProfile : Profile
    {
        public CrmMappingProfile()
        {
            // VatInfo mappings
            CreateMap<VatInfo, VatInfoItem>()
                .ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.Customer != null ? src.Customer.FullName : ""));

            CreateMap<VatInfoModel, VatInfo>();
            CreateMap<VatInfo, VatInfoModel>();

            // Agency mappings
            CreateMap<Agency, AgencyModel>();
            CreateMap<AgencyModel, Agency>();
            CreateMap<Agency, AgencyItem>();

            // Customer mappings
            CreateMap<Customer, CustomerModel>();
            CreateMap<CustomerModel, Customer>();
            CreateMap<Customer, CustomerItem>();
            CreateMap<Customer, CustomerSugItem>();

            // CustomerAddress mappings
            CreateMap<CustomerAddress, CustomerAddressModel>();
            CreateMap<CustomerAddressModel, CustomerAddress>();
            CreateMap<CustomerAddress, CustomerAddressItem>();

            // CustomerBankAccount mappings
            CreateMap<CustomerBankAccount, CustomerBankAccountModel>();
            CreateMap<CustomerBankAccountModel, CustomerBankAccount>();
            CreateMap<CustomerBankAccount, CustomerBankAccountItem>()
                .ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.Customer != null ? src.Customer.FullName : ""));

            // CallLog mappings
            CreateMap<CallLog, CallLogModel>();
            CreateMap<CallLogModel, CallLog>();
            CreateMap<CallLog, CallLogItem>()
                .ForMember(dest => dest.HistoryID, opt => opt.MapFrom(src => src.HistoryID ?? string.Empty))
                .ForMember(dest => dest.CallID, opt => opt.MapFrom(src => src.CallID ?? string.Empty))
                .ForMember(dest => dest.Duration, opt => opt.MapFrom(src => src.Duration ?? string.Empty))
                .ForMember(dest => dest.ReasonTerminated, opt => opt.MapFrom(src => src.ReasonTerminated ?? string.Empty))
                .ForMember(dest => dest.FromNo, opt => opt.MapFrom(src => src.FromNo ?? string.Empty))
                .ForMember(dest => dest.ToNo, opt => opt.MapFrom(src => src.ToNo ?? string.Empty))
                .ForMember(dest => dest.DialNo, opt => opt.MapFrom(src => src.DialNo ?? string.Empty))
                .ForMember(dest => dest.FinalNumber, opt => opt.MapFrom(src => src.FinalNumber ?? string.Empty))
                .ForMember(dest => dest.BillName, opt => opt.MapFrom(src => src.BillName ?? string.Empty))
                .ForMember(dest => dest.FromType, opt => opt.MapFrom(src => src.FromType ?? string.Empty))
                .ForMember(dest => dest.ToType, opt => opt.MapFrom(src => src.ToType ?? string.Empty))
                .ForMember(dest => dest.FromDispName, opt => opt.MapFrom(src => src.FromDispName ?? string.Empty))
                .ForMember(dest => dest.ToDispName, opt => opt.MapFrom(src => src.ToDispName ?? string.Empty))
                .ForMember(dest => dest.FinalDN, opt => opt.MapFrom(src => src.FinalDN ?? string.Empty))
                .ForMember(dest => dest.FinalDispName, opt => opt.MapFrom(src => src.FinalDispName ?? string.Empty))
                .ForMember(dest => dest.IsMissedCall, opt => opt.MapFrom(src => src.FinalDN == "GRP0000"))
                .ForMember(dest => dest.CallType, opt => opt.MapFrom(src => src.CallType ?? string.Empty))
                .ForMember(dest => dest.StatusContent, opt => opt.MapFrom(src => src.StatusContent ?? string.Empty))
                .ForMember(dest => dest.CreatedByUserName, opt => opt.MapFrom(src => src.CreatedByUserName ?? string.Empty))
                .ForMember(dest => dest.UpdatedByUserName, opt => opt.MapFrom(src => src.UpdatedByUserName ?? string.Empty))
                .ForMember(dest => dest.CreatedTime, opt => opt.MapFrom(src => src.CreatedTime.HasValue ? src.CreatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""))
                .ForMember(dest => dest.UpdatedTime, opt => opt.MapFrom(src => src.UpdatedTime.HasValue ? src.UpdatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""))
                .ForMember(dest => dest.LeadStatusStr, opt => opt.MapFrom(src => src.LeadStatus.GetDescription()));


            // Lead mappings
            CreateMap<LeadModel, Lead>();
            CreateMap<Lead, LeadItem>();
            CreateMap<Lead, LeadStatusModel>();
        }
    }

    public static class CrmMappingExtensions
    {
        // VatInfo mapping extensions
        public static VatInfoItem ToItem(this VatInfo entity)
            => entity.MapTo<VatInfo, VatInfoItem>();

        public static VatInfo ToEntity(this VatInfoModel model)
            => model.MapTo<VatInfoModel, VatInfo>();

        public static VatInfoModel ToModel(this VatInfo entity)
            => entity.MapTo<VatInfo, VatInfoModel>();

        // Agency mapping extensions
        public static AgencyModel ToModel(this Agency entity)
            => entity.MapTo<Agency, AgencyModel>();

        public static AgencyItem ToItem(this Agency entity)
            => entity.MapTo<Agency, AgencyItem>();

        public static Agency ToEntity(this AgencyModel model)
            => model.MapTo<AgencyModel, Agency>();

        public static List<AgencyItem> ToItem(this List<Agency> entity)
            => entity.MapTo<List<Agency>, List<AgencyItem>>();

        // Customer mapping extensions
        public static CustomerModel ToModel(this Customer entity)
            => entity.MapTo<Customer, CustomerModel>();

        public static CustomerItem ToItem(this Customer entity)
            => entity.MapTo<Customer, CustomerItem>();

        public static CustomerSugItem ToSugItem(this Customer entity)
            => entity.MapTo<Customer, CustomerSugItem>();

        public static Customer ToEntity(this CustomerModel model)
            => model.MapTo<CustomerModel, Customer>();

        // CustomerAddress mapping extensions
        public static CustomerAddressModel ToModel(this CustomerAddress entity)
            => entity.MapTo<CustomerAddress, CustomerAddressModel>();

        public static CustomerAddressItem ToItem(this CustomerAddress entity)
            => entity.MapTo<CustomerAddress, CustomerAddressItem>();

        public static CustomerAddress ToEntity(this CustomerAddressModel model)
            => model.MapTo<CustomerAddressModel, CustomerAddress>();

        // Lead mapping extensions
        public static Lead ToEntity(this LeadModel model)
            => model.MapTo<LeadModel, Lead>();

        public static LeadItem ToItem(this Lead model)
            => model.MapTo<Lead, LeadItem>();

        // CustomerBankAccount extensions
        public static CustomerBankAccount ToEntity(this CustomerBankAccountModel model)
            => model.MapTo<CustomerBankAccountModel, CustomerBankAccount>();

        public static CustomerBankAccountItem ToItem(this CustomerBankAccount entity)
            => entity.MapTo<CustomerBankAccount, CustomerBankAccountItem>();

        public static CustomerBankAccountModel ToModel(this CustomerBankAccount entity)
            => entity.MapTo<CustomerBankAccount, CustomerBankAccountModel>();

        // CallLog extensions
        public static CallLogModel ToModel(this CallLog entity)
            => entity.MapTo<CallLog, CallLogModel>();

        public static CallLogItem ToItem(this CallLog entity)
            => entity.MapTo<CallLog, CallLogItem>();

        public static CallLog ToEntity(this CallLogModel model)
            => model.MapTo<CallLogModel, CallLog>();

        public static LeadStatusModel ToStatusModel(this Lead model)
            => model.MapTo<Lead, LeadStatusModel>();
    }
}