using CMSMicroservice.Application.CommissionCQ.Queries.GetUserWeeklyBalances;
using CMSMicroservice.Application.CommissionCQ.Queries.GetAvailableWeeks;
using CMSMicroservice.Application.CommissionCQ.Queries.GetUserCommissionPayouts;
using CMSMicroservice.Application.CommissionCQ.Queries.GetWeekDefinitions;
using CMSMicroservice.Application.Common.Models;
using CMSMicroservice.Protobuf.Protos.Commission;
using Google.Protobuf.WellKnownTypes;
using Mapster;
using AppWeekFilter = CMSMicroservice.Application.CommissionCQ.Queries.GetWeekDefinitions.GetWeekDefinitionsFilter;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class CommissionProfile : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        // GetAvailableWeeks Request Mapping
        config.NewConfig<GetAvailableWeeksRequest, GetAvailableWeeksQuery>()
            .Map(dest => dest.FutureWeeksCount, src => src.FutureWeeksCount > 0 ? src.FutureWeeksCount : 4)
            .Map(dest => dest.PastWeeksCount, src => src.PastWeeksCount > 0 ? src.PastWeeksCount : 12);

        // GetAvailableWeeks Response Mapping
        config.NewConfig<GetAvailableWeeksResponseDto, GetAvailableWeeksResponse>()
            .Map(dest => dest.CurrentWeek, src => src.CurrentWeek)
            .Map(dest => dest.CalculatedWeeks, src => src.CalculatedWeeks)
            .Map(dest => dest.PendingWeeks, src => src.PendingWeeks)
            .Map(dest => dest.FutureWeeks, src => src.FutureWeeks);

        // WeekInfo Mapping
        config.NewConfig<WeekInfoDto, WeekInfo>()
            .Map(dest => dest.WeekDefinitionId, src => src.WeekDefinitionId)
            .Map(dest => dest.DisplayName, src => src.DisplayName)
            .Map(dest => dest.StartDate, src => Timestamp.FromDateTime(src.StartDate.ToUniversalTime()))
            .Map(dest => dest.EndDate, src => Timestamp.FromDateTime(src.EndDate.ToUniversalTime()))
            .Map(dest => dest.IsCalculated, src => src.IsCalculated)
            .Map(dest => dest.CalculatedAt, src => src.CalculatedAt.HasValue 
                ? Timestamp.FromDateTime(src.CalculatedAt.Value.ToUniversalTime()) 
                : null)

            .Map(dest => dest.LastExecutionStatus, src => src.LastExecutionStatus ?? string.Empty)
            .Map(dest => dest.TotalPoolAmount, src => src.TotalPoolAmount ?? 0)
            .Map(dest => dest.EligibleUsersCount, src => src.EligibleUsersCount ?? 0)
            .Map(dest => dest.DisplayText, src => src.DisplayText);

        // GetUserCommissionPayouts Response Model Mapping
        config.NewConfig<GetUserCommissionPayoutsResponseModel, UserCommissionPayoutModel>()
            .Map(dest => dest.Id, src => src.Id)
            .Map(dest => dest.WeekDefinitionId, src => src.WeekDefinitionId)
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.UserName, src => $"{src.FirstName} {src.LastName}")
            .Map(dest => dest.WeekDisplayName, src => src.WeekDisplayName)
            .Map(dest => dest.WeeklyPoolId, src => src.WeeklyPoolId)
            .Map(dest => dest.BalancesEarned, src => src.BalancesEarned)
            .Map(dest => dest.ValuePerBalance, src => src.ValuePerBalance)
            .Map(dest => dest.TotalAmount, src => src.TotalAmount)
            .Map(dest => dest.Status, src => (int)src.Status)
            .Map(dest => dest.PaidAt, src => src.PaidAt.HasValue 
                ? Timestamp.FromDateTime(src.PaidAt.Value.ToUniversalTime()) 
                : null)
            .Map(dest => dest.WithdrawalMethod, src => src.WithdrawalMethod.HasValue 
                ? (int)src.WithdrawalMethod.Value 
                : (int?)null)
            .Map(dest => dest.IbanNumber, src => src.IbanNumber ?? string.Empty)
            .Map(dest => dest.WithdrawnAt, src => src.WithdrawnAt.HasValue 
                ? Timestamp.FromDateTime(src.WithdrawnAt.Value.ToUniversalTime()) 
                : null)
            .Map(dest => dest.Created, src => Timestamp.FromDateTimeOffset(src.Created));

        // GetWeekDefinitions Request Mapping
        config.NewConfig<GetWeekDefinitionsRequest, GetWeekDefinitionsQuery>()
            .Map(dest => dest.PaginationState, src => src.PaginationState != null 
                ? new PaginationState { PageNumber = src.PaginationState.PageNumber, PageSize = src.PaginationState.PageSize } 
                : null)
            .Map(dest => dest.SortBy, src => src.SortBy)
            .Map(dest => dest.Filter, src => src.Filter != null 
                ? new AppWeekFilter
                {
                    SearchText = src.Filter.SearchText,
                    WeekOrder = src.Filter.WeekOrder,
                    GregorianWeekNumber = src.Filter.GregorianWeekNumber,
                    PersianWeekNumber = src.Filter.PersianWeekNumber,
                    GregorianYear = src.Filter.GregorianYear,
                    PersianYear = src.Filter.PersianYear,
                    IsActive = src.Filter.IsActive
                } 
                : null);

        // GetWeekDefinitions Response Mapping
        config.NewConfig<GetWeekDefinitionsResponseDto, GetWeekDefinitionsResponse>()
            .Map(dest => dest.TotalCount, src => src.TotalCount)
            .Map(dest => dest.Data, src => src.Data);

        // WeekDefinitionItem Mapping
        config.NewConfig<WeekDefinitionItemDto, WeekDefinitionItem>()
            .Map(dest => dest.Id, src => src.Id)
            .Map(dest => dest.WeekOrder, src => src.WeekOrder)
            .Map(dest => dest.DisplayName, src => src.DisplayName)
            .Map(dest => dest.GregorianWeekNumber, src => src.GregorianWeekNumber)
            .Map(dest => dest.PersianWeekNumber, src => src.PersianWeekNumber)
            .Map(dest => dest.StartDate, src => Timestamp.FromDateTime(DateTime.SpecifyKind(src.StartDate, DateTimeKind.Utc)))
            .Map(dest => dest.EndDate, src => Timestamp.FromDateTime(DateTime.SpecifyKind(src.EndDate, DateTimeKind.Utc)))
            .Map(dest => dest.GregorianYear, src => src.GregorianYear)
            .Map(dest => dest.PersianYear, src => src.PersianYear)
            .Map(dest => dest.IsActive, src => src.IsActive)
            .Map(dest => dest.IsCurrentWeek, src => src.IsCurrentWeek);

        // GetUserWeeklyBalances Response Model Mapping
        config.NewConfig<GetUserWeeklyBalancesResponseModel, UserWeeklyBalanceModel>()
            .Map(dest => dest.Id, src => src.Id)
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.UserFullName, src => src.UserFullName)
            .Map(dest => dest.WeekDefinitionId, src => src.WeekDefinitionId)
            .Map(dest => dest.WeekDisplayName, src => src.WeekDisplayName)
            .Map(dest => dest.LeftLegNewMembers, src => src.LeftLegNewMembers)
            .Map(dest => dest.LeftLegCarryover, src => src.LeftLegCarryover)
            .Map(dest => dest.LeftLegTotal, src => src.LeftLegTotal)
            .Map(dest => dest.RightLegNewMembers, src => src.RightLegNewMembers)
            .Map(dest => dest.RightLegCarryover, src => src.RightLegCarryover)
            .Map(dest => dest.RightLegTotal, src => src.RightLegTotal)
            .Map(dest => dest.TotalBalances, src => src.TotalBalances)
            .Map(dest => dest.WeeklyPoolContribution, src => src.WeeklyPoolContribution)
            .Map(dest => dest.CalculatedAt, src => src.CalculatedAt.HasValue 
                ? Timestamp.FromDateTime(DateTime.SpecifyKind(src.CalculatedAt.Value, DateTimeKind.Utc)) 
                : null)
            .Map(dest => dest.IsExpired, src => src.IsExpired)
            .Map(dest => dest.Created, src => Timestamp.FromDateTimeOffset(src.Created));
    }
}
