using AutoMapper;
using BookHive.DTO.Author;
using BookHive.DTO.Book;
using BookHive.DTO.Loan;
using BookHive.DTO.Member;
using BookHive.DTO.Review;
using BookHive.Models;

namespace BookHive.MappingProfiles;

public class DtoToEntityMappings : Profile
{
    public DtoToEntityMappings()
    {
        CreateMap<CreateBookDto, Book>();
        CreateMap<UpdateBookDto, Book>()
            .ForMember(dest => dest.Id, opt => opt.Ignore());

        CreateMap<CreateAuthorDto, Author>();
        CreateMap<UpdateAuthorDto, Author>()
            .ForMember(dest => dest.Id, opt => opt.Ignore());

        CreateMap<CreateMemberDto, Member>();
        CreateMap<UpdateMemberDto, Member>()
            .ForMember(dest => dest.Id, opt => opt.Ignore())
            .ForMember(
                dest => dest.Email,
                opt => opt.PreCondition(src => !string.IsNullOrEmpty(src.Email)));

        CreateMap<CreateReviewDto, Review>();
        CreateMap<UpdateReviewDto, Review>()
            .ForMember(dest => dest.Id, opt => opt.Ignore());

        CreateMap<CreateLoanDto, Loan>();
        CreateMap<UpdateLoanDto, Loan>()
            .ForMember(dest => dest.Id, opt => opt.Ignore());
    }
}