using CMSMicroservice.Application.ClubFeatureCQ.Commands.ToggleUserClubFeature;
using CMSMicroservice.Application.ClubFeatureCQ.Queries.GetUserClubFeatures;
using CMSMicroservice.Application.ClubMembershipCQ.Commands.AcceptClubMembershipContract;
using CMSMicroservice.Protobuf.Protos.ClubMembership;
using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;

namespace CMSMicroservice.WebApi.Common.Mappings;

public class ClubFeatureProfile : IRegister
{
    void IRegister.Register(TypeAdapterConfig config)
    {
        // GetUserClubFeaturesRequest → GetUserClubFeaturesQuery
        config.NewConfig<GetUserClubFeaturesRequest, GetUserClubFeaturesQuery>()
            .Map(dest => dest.UserId, src => src.UserId);

        // UserClubFeatureDto → UserClubFeatureModel
        config.NewConfig<UserClubFeatureDto, UserClubFeatureModel>()
            .Map(dest => dest.Id, src => src.Id)
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.ClubMembershipId, src => src.ClubMembershipId)
            .Map(dest => dest.ClubFeatureId, src => src.ClubFeatureId)
            .Map(dest => dest.FeatureTitle, src => src.FeatureTitle)
            .Map(dest => dest.FeatureDescription, src => src.FeatureDescription ?? "")
            .Map(dest => dest.IsActive, src => src.IsActive)
            .Map(dest => dest.GrantedAt, src => Timestamp.FromDateTime(DateTime.SpecifyKind(src.GrantedAt, DateTimeKind.Utc)))
            .Map(dest => dest.Notes, src => src.Notes ?? "")
            .Map(dest => dest.SortOrder, src => src.SortOrder)
            .Map(dest => dest.CreatedAt, src => Timestamp.FromDateTime(DateTime.SpecifyKind(src.CreatedAt, DateTimeKind.Utc)));

        // List<UserClubFeatureDto> → GetUserClubFeaturesResponse
        config.NewConfig<List<UserClubFeatureDto>, GetUserClubFeaturesResponse>()
            .Map(dest => dest.Features, src => src.Adapt<List<UserClubFeatureModel>>());

        // ToggleUserClubFeatureRequest → ToggleUserClubFeatureCommand
        config.NewConfig<ToggleUserClubFeatureRequest, ToggleUserClubFeatureCommand>()
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.ClubFeatureId, src => src.ClubFeatureId)
            .Map(dest => dest.IsActive, src => src.IsActive);

        // ToggleUserClubFeatureResponse (App) → ToggleUserClubFeatureResponse (Proto)
        config.NewConfig<Application.ClubFeatureCQ.Commands.ToggleUserClubFeature.ToggleUserClubFeatureResponse, Protobuf.Protos.ClubMembership.ToggleUserClubFeatureResponse>()
            .Map(dest => dest.Success, src => src.Success)
            .Map(dest => dest.Message, src => src.Message)
            .Map(dest => dest.UserClubFeatureId, src => src.UserClubFeatureId.HasValue ? (long?)src.UserClubFeatureId.Value : null)
            .Map(dest => dest.IsActive, src => src.IsActive.HasValue ? (bool?)src.IsActive.Value : null);

        // AcceptClubMembershipContractRequest → AcceptClubMembershipContractCommand
        config.NewConfig<AcceptClubMembershipContractRequest, AcceptClubMembershipContractCommand>()
            .Map(dest => dest.UserId, src => src.UserId)
            .Map(dest => dest.OtpCode, src => src.OtpCode)
            .Map(dest => dest.SignGuid, src => src.SignGuid)
            .Map(dest => dest.ContractHtml, src => src.ContractHtml);

        // AcceptClubMembershipContractResponseDto → AcceptClubMembershipContractResponse
        config.NewConfig<AcceptClubMembershipContractResponseDto, AcceptClubMembershipContractResponse>()
            .Map(dest => dest.Success, src => src.Success)
            .Map(dest => dest.Message, src => src.Message)
            .Map(dest => dest.ContractId, src => src.ContractId);
    }
}
