using AutoMapper;
using PowderCoating.Application.DTOs.Accounting;
using PowderCoating.Core.Entities;

namespace PowderCoating.Application.Mappings;

public class AccountingProfile : Profile
{
    public AccountingProfile()
    {
        // ── Account ──────────────────────────────────────────────────────────

        CreateMap<Account, AccountListDto>()
            .ForMember(d => d.ParentAccountName, o => o.MapFrom(s =>
                s.ParentAccount != null ? s.ParentAccount.Name : null));

        CreateMap<Account, AccountDto>()
            .ForMember(d => d.ParentAccountName, o => o.MapFrom(s =>
                s.ParentAccount != null ? s.ParentAccount.Name : null))
            .ForMember(d => d.SubAccounts, o => o.MapFrom(s => s.SubAccounts));

        CreateMap<CreateAccountDto, Account>()
            .ForMember(d => d.IsSystem, o => o.Ignore())
            .ForMember(d => d.CompanyId, o => o.Ignore());

        CreateMap<Account, EditAccountDto>();

        CreateMap<EditAccountDto, Account>()
            .ForMember(d => d.IsSystem, o => o.Ignore())
            .ForMember(d => d.CompanyId, o => o.Ignore());

        // ── Bill ─────────────────────────────────────────────────────────────

        CreateMap<Bill, BillListDto>()
            .ForMember(d => d.VendorName, o => o.MapFrom(s =>
                s.Vendor != null ? s.Vendor.CompanyName : string.Empty));

        CreateMap<Bill, BillDto>()
            .ForMember(d => d.VendorName, o => o.MapFrom(s =>
                s.Vendor != null ? s.Vendor.CompanyName : string.Empty))
            .ForMember(d => d.VendorEmail, o => o.MapFrom(s =>
                s.Vendor != null ? s.Vendor.Email : null))
            .ForMember(d => d.VendorPhone, o => o.MapFrom(s =>
                s.Vendor != null ? s.Vendor.Phone : null))
            .ForMember(d => d.APAccountName, o => o.MapFrom(s =>
                s.APAccount != null ? s.APAccount.Name : string.Empty))
            .ForMember(d => d.LineItems, o => o.MapFrom(s => s.LineItems))
            .ForMember(d => d.Payments, o => o.MapFrom(s => s.Payments));

        CreateMap<BillLineItem, BillLineItemDto>()
            .ForMember(d => d.AccountName, o => o.MapFrom(s =>
                s.Account != null ? s.Account.Name : string.Empty))
            .ForMember(d => d.AccountNumber, o => o.MapFrom(s =>
                s.Account != null ? s.Account.AccountNumber : string.Empty))
            .ForMember(d => d.JobNumber, o => o.MapFrom(s =>
                s.Job != null ? s.Job.JobNumber : null));

        CreateMap<BillPayment, BillPaymentDto>()
            .ForMember(d => d.BankAccountName, o => o.MapFrom(s =>
                s.BankAccount != null ? s.BankAccount.Name : string.Empty));

        CreateMap<Bill, EditBillDto>();

        CreateMap<CreateBillDto, Bill>()
            .ForMember(d => d.BillNumber, o => o.Ignore())
            .ForMember(d => d.Status, o => o.Ignore())
            .ForMember(d => d.SubTotal, o => o.Ignore())
            .ForMember(d => d.TaxAmount, o => o.Ignore())
            .ForMember(d => d.Total, o => o.Ignore())
            .ForMember(d => d.AmountPaid, o => o.Ignore())
            .ForMember(d => d.BalanceDue, o => o.Ignore())
            .ForMember(d => d.CompanyId, o => o.Ignore());

        CreateMap<CreateBillLineItemDto, BillLineItem>()
            .ForMember(d => d.Amount, o => o.Ignore())
            .ForMember(d => d.CompanyId, o => o.Ignore());

        CreateMap<RecordBillPaymentDto, BillPayment>()
            .ForMember(d => d.PaymentNumber, o => o.Ignore())
            .ForMember(d => d.VendorId, o => o.Ignore())
            .ForMember(d => d.CompanyId, o => o.Ignore());

        // ── Expense ──────────────────────────────────────────────────────────

        CreateMap<Expense, ExpenseListDto>()
            .ForMember(d => d.VendorName, o => o.MapFrom(s =>
                s.Vendor != null ? s.Vendor.CompanyName : null))
            .ForMember(d => d.ExpenseAccountName, o => o.MapFrom(s =>
                s.ExpenseAccount != null ? s.ExpenseAccount.Name : string.Empty))
            .ForMember(d => d.ExpenseAccountNumber, o => o.MapFrom(s =>
                s.ExpenseAccount != null ? s.ExpenseAccount.AccountNumber : string.Empty))
            .ForMember(d => d.PaymentAccountName, o => o.MapFrom(s =>
                s.PaymentAccount != null ? s.PaymentAccount.Name : string.Empty))
            .ForMember(d => d.JobNumber, o => o.MapFrom(s =>
                s.Job != null ? s.Job.JobNumber : null))
            .ForMember(d => d.HasReceipt, o => o.MapFrom(s =>
                !string.IsNullOrEmpty(s.ReceiptFilePath)));

        CreateMap<Expense, ExpenseDto>()
            .IncludeBase<Expense, ExpenseListDto>();

        CreateMap<CreateExpenseDto, Expense>()
            .ForMember(d => d.ExpenseNumber, o => o.Ignore())
            .ForMember(d => d.CompanyId, o => o.Ignore());

        CreateMap<Expense, EditExpenseDto>();

        CreateMap<EditExpenseDto, Expense>()
            .ForMember(d => d.ExpenseNumber, o => o.Ignore())
            .ForMember(d => d.CompanyId, o => o.Ignore());
    }
}
