using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;

public class PaymentReceiveItemResponse
{
    public string? Id { get; set; }
    public string? ProductId { get; set; }
    public string? ProductName { get; set; }
    public string? Summary { get; set; }
    public decimal UnitPrice { get; set; }
    public double Quantity { get; set; }
    public decimal Total { get; set; }
}

public class GetPaymentReceiveByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? InvoiceId { get; set; }
    public string? InvoiceNumber { get; set; }
    public string? Description { get; set; }
    public DateTime? PaymentDate { get; set; }
    public string? PaymentMethodId { get; set; }
    public string? PaymentMethodName { get; set; }
    public decimal PaymentAmount { get; set; }
    public Data.Enums.PaymentReceiveStatus Status { get; set; }
    public string? CustomerName { get; set; }
    public string? CustomerStreet { get; set; }
    public string? CustomerCity { get; set; }
    public string? CompanyName { get; set; }
    public string? CompanyStreet { get; set; }
    public string? CompanyCity { get; set; }
    public string? CurrencySymbol { get; set; }
    public decimal SalesOrderBeforeTaxAmount { get; set; }
    public decimal SalesOrderTaxAmount { get; set; }
    public decimal SalesOrderAfterTaxAmount { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<PaymentReceiveItemResponse> Items { get; set; } = new();
}

public record GetPaymentReceiveByIdQuery(string Id) : IRequest<GetPaymentReceiveByIdResponse?>;

public class GetPaymentReceiveByIdHandler : IRequestHandler<GetPaymentReceiveByIdQuery, GetPaymentReceiveByIdResponse?>
{
    private readonly AppDbContext _context;
    public GetPaymentReceiveByIdHandler(AppDbContext context) => _context = context;

    public async Task<GetPaymentReceiveByIdResponse?> Handle(GetPaymentReceiveByIdQuery request, CancellationToken cancellationToken)
    {
        var company = await _context.Company
            .AsNoTracking()
            .Include(x => x.Currency)
            .OrderByDescending(x => x.IsDefault)
            .FirstOrDefaultAsync(cancellationToken);

        var payment = await _context.PaymentReceive
            .AsNoTracking()
            .Include(x => x.Invoice)
                .ThenInclude(i => i!.SalesOrder)
                    .ThenInclude(so => so!.Customer)
            .Include(x => x.Invoice)
                .ThenInclude(i => i!.SalesOrder)
                    .ThenInclude(so => so!.SalesOrderItemList)
                        .ThenInclude(si => si.Product)
            .Include(x => x.PaymentMethod)
            .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

        if (payment == null) return null;

        var salesOrder = payment.Invoice?.SalesOrder;

        return new GetPaymentReceiveByIdResponse
        {
            Id = payment.Id,
            AutoNumber = payment.AutoNumber,
            InvoiceId = payment.InvoiceId,
            InvoiceNumber = payment.Invoice?.AutoNumber,
            Description = payment.Description,
            PaymentDate = payment.PaymentDate,
            PaymentMethodId = payment.PaymentMethodId,
            PaymentMethodName = payment.PaymentMethod?.Name,
            PaymentAmount = payment.PaymentAmount ?? 0,
            Status = payment.Status,
            CustomerName = salesOrder?.Customer?.Name,
            CustomerStreet = salesOrder?.Customer?.Street,
            CustomerCity = salesOrder?.Customer?.City,
            CompanyName = company?.Name,
            CompanyStreet = company?.StreetAddress,
            CompanyCity = company?.City,
            CurrencySymbol = company?.Currency?.Symbol ?? "IDR",
            SalesOrderBeforeTaxAmount = salesOrder?.BeforeTaxAmount ?? 0,
            SalesOrderTaxAmount = salesOrder?.TaxAmount ?? 0,
            SalesOrderAfterTaxAmount = salesOrder?.AfterTaxAmount ?? 0,
            CreatedAt = payment.CreatedAt,
            CreatedBy = payment.CreatedBy,
            UpdatedAt = payment.UpdatedAt,
            UpdatedBy = payment.UpdatedBy,
            Items = salesOrder?.SalesOrderItemList.Select(x => new PaymentReceiveItemResponse
            {
                Id = x.Id,
                ProductId = x.ProductId,
                ProductName = x.Product?.Name,
                Summary = x.Summary,
                UnitPrice = x.UnitPrice ?? 0,
                Quantity = x.Quantity ?? 0,
                Total = x.Total ?? 0
            }).ToList() ?? new()
        };
    }
}