using AutoMapper;
using LittleShop.Models;
using LittleShop.DTOs;

namespace LittleShop.Mapping;

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<User, UserDto>();
        CreateMap<CreateUserDto, User>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(dest => dest.PasswordHash, opt => opt.Ignore())
            .ForMember(dest => dest.CreatedAt, opt => opt.Ignore())
            .ForMember(dest => dest.IsActive, opt => opt.MapFrom(src => true));

        CreateMap<Category, CategoryDto>()
            .ForMember(dest => dest.ProductCount, opt => opt.MapFrom(src => src.Products.Count(p => p.IsActive)));
        CreateMap<CreateCategoryDto, Category>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(dest => dest.CreatedAt, opt => opt.Ignore())
            .ForMember(dest => dest.IsActive, opt => opt.MapFrom(src => true));

        CreateMap<Product, ProductDto>()
            .ForMember(dest => dest.CategoryName, opt => opt.MapFrom(src => src.Category.Name));
        CreateMap<CreateProductDto, Product>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(dest => dest.CreatedAt, opt => opt.Ignore())
            .ForMember(dest => dest.IsActive, opt => opt.MapFrom(src => true));

        CreateMap<ProductPhoto, ProductPhotoDto>();

        CreateMap<Order, OrderDto>();
        CreateMap<CreateOrderDto, Order>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(dest => dest.Status, opt => opt.MapFrom(src => LittleShop.Enums.OrderStatus.PendingPayment))
            .ForMember(dest => dest.TotalAmount, opt => opt.Ignore())
            .ForMember(dest => dest.Currency, opt => opt.MapFrom(src => "GBP"))
            .ForMember(dest => dest.CreatedAt, opt => opt.Ignore());

        CreateMap<OrderItem, OrderItemDto>()
            .ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.Product.Name));

        CreateMap<CryptoPayment, CryptoPaymentDto>();

        // Customer mappings
        CreateMap<Customer, CustomerDto>()
            .ForMember(dest => dest.DisplayName, opt => opt.Ignore())
            .ForMember(dest => dest.CustomerType, opt => opt.Ignore());
        CreateMap<CreateCustomerDto, Customer>();
        CreateMap<UpdateCustomerDto, Customer>()
            .ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));

        CreateMap<Customer, CustomerSummaryDto>()
            .ForMember(dest => dest.DisplayName, opt => opt.MapFrom(src => src.DisplayName))
            .ForMember(dest => dest.CustomerType, opt => opt.MapFrom(src => src.CustomerType));

        // CustomerMessage mappings  
        CreateMap<CustomerMessage, CustomerMessageDto>()
            .ForMember(dest => dest.Customer, opt => opt.MapFrom(src => src.Customer))
            .ForMember(dest => dest.AdminUsername, opt => opt.MapFrom(src => src.AdminUser != null ? src.AdminUser.Username : null))
            .ForMember(dest => dest.OrderReference, opt => opt.MapFrom(src => src.Order != null ? src.Order.Id.ToString().Substring(0, 8) : null))
            .ForMember(dest => dest.TelegramUserId, opt => opt.MapFrom(src => src.Customer != null ? src.Customer.TelegramUserId : 0))
            .ForMember(dest => dest.DisplayTitle, opt => opt.MapFrom(src => src.GetDisplayTitle()))
            .ForMember(dest => dest.StatusDisplay, opt => opt.MapFrom(src => src.Status.ToString()))
            .ForMember(dest => dest.DirectionDisplay, opt => opt.MapFrom(src => src.Direction == MessageDirection.AdminToCustomer ? "Outbound" : "Inbound"))
            .ForMember(dest => dest.TypeDisplay, opt => opt.MapFrom(src => src.Type.ToString()));

        CreateMap<CreateCustomerMessageDto, CustomerMessage>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => Guid.NewGuid()))
            .ForMember(dest => dest.Direction, opt => opt.MapFrom(src => MessageDirection.AdminToCustomer))
            .ForMember(dest => dest.CreatedAt, opt => opt.MapFrom(src => DateTime.UtcNow))
            .ForMember(dest => dest.Status, opt => opt.MapFrom(src => MessageStatus.Pending))
            .ForMember(dest => dest.Platform, opt => opt.MapFrom(src => "Telegram"));
    }
}