using AutoMapper;
using Codecrete.SwissQRBill.Generator;
using DocumentOperator.Application.Common.DTOs;
using DocumentOperator.Domain.Models.ValueObjects;

namespace DocumentOperator.Application.Common.Mapping;

/// <summary>
/// AutoMapper profile for mapping domain entities and external models to DTOs.
/// NOTE: Always use AutoMapper for all mappings in this project.
/// </summary>
public class MappingProfile : Profile
{
    public MappingProfile()
    {
        // PdfMetadata -> PdfValidationResult
        CreateMap<PdfMetadata, PdfValidationResult>();

        // PdfAMetadata -> PdfAValidationResult
        CreateMap<PdfAMetadata, PdfAValidationResult>()
            .ForMember(dest => dest.FileSize, opt => opt.MapFrom(src => src.FileSizeBytes));

        // Codecrete Bill -> SwissQrBillDto
        CreateMap<Bill, SwissQrBillDto>()
            .ForMember(dest => dest.Version, opt => opt.MapFrom(src => src.Version.ToString()))
            .ForMember(dest => dest.Currency, opt => opt.MapFrom(src => src.Currency ?? "CHF"))
            .ForMember(dest => dest.Account, opt => opt.MapFrom(src => src.Account ?? string.Empty))
            .ForMember(dest => dest.ReferenceType, opt => opt.MapFrom(src => src.ReferenceType ?? Bill.ReferenceTypeNoRef));

        // Codecrete Address -> AddressDto
        CreateMap<Address, AddressDto>()
            .ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type.ToString()))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name ?? string.Empty))
            .ForMember(dest => dest.PostalCode, opt => opt.MapFrom(src => src.PostalCode ?? string.Empty))
            .ForMember(dest => dest.Town, opt => opt.MapFrom(src => src.Town ?? string.Empty))
            .ForMember(dest => dest.CountryCode, opt => opt.MapFrom(src => src.CountryCode ?? string.Empty))
#pragma warning disable CS0618 // Suppress obsolete warning for AddressLine1/2 mapping
            .ForMember(dest => dest.AddressLine1, opt => opt.MapFrom(src => src.AddressLine1))
            .ForMember(dest => dest.AddressLine2, opt => opt.MapFrom(src => src.AddressLine2));
#pragma warning restore CS0618

        // Codecrete AlternativeScheme -> AlternativeSchemeDto
        CreateMap<AlternativeScheme, AlternativeSchemeDto>()
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name ?? string.Empty))
            .ForMember(dest => dest.Instruction, opt => opt.MapFrom(src => src.Instruction ?? string.Empty));

        // AttachmentInfo -> AttachmentCheckResult
        CreateMap<AttachmentInfo, AttachmentCheckResult>();

        // AttachmentMetadata -> AttachmentDto
        CreateMap<AttachmentMetadata, AttachmentDto>()
            .ForMember(dest => dest.Size, opt => opt.MapFrom(src => src.SizeBytes));
    }
}
