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

namespace Indotalent.Features.Purchase.Bill.Cqrs;

public class BillPurchaseOrderItemResponse
{
    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 GetBillByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public DateTime? BillDate { get; set; }
    public Data.Enums.BillStatus BillStatus { get; set; }
    public string? Description { get; set; }
    public string? PurchaseOrderId { get; set; }
    public string? PurchaseOrderAutoNumber { get; set; }
    public string? VendorName { get; set; }
    public string? VendorStreet { get; set; }
    public string? VendorCity { get; set; }
    public string? CompanyName { get; set; }
    public string? CompanyStreet { get; set; }
    public string? CompanyCity { get; set; }
    public string? CurrencySymbol { get; set; }
    public decimal? BeforeTaxAmount { get; set; }
    public decimal? TaxAmount { get; set; }
    public decimal? AfterTaxAmount { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<BillPurchaseOrderItemResponse> Items { get; set; } = new();
}

public record GetBillByIdQuery(string Id) : IRequest<GetBillByIdResponse?>;

public class GetBillByIdHandler : IRequestHandler<GetBillByIdQuery, GetBillByIdResponse?>
{
    private readonly AppDbContext _context;
    public GetBillByIdHandler(AppDbContext context) => _context = context;

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

        return await _context.Bill
            .AsNoTracking()
            .Include(x => x.PurchaseOrder)
                .ThenInclude(po => po!.Vendor)
            .Include(x => x.PurchaseOrder)
                .ThenInclude(po => po!.PurchaseOrderItemList)
                    .ThenInclude(i => i.Product)
            .Where(x => x.Id == request.Id)
            .Select(x => new GetBillByIdResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                BillDate = x.BillDate,
                BillStatus = x.BillStatus,
                Description = x.Description,
                PurchaseOrderId = x.PurchaseOrderId,
                PurchaseOrderAutoNumber = x.PurchaseOrder!.AutoNumber,
                VendorName = x.PurchaseOrder!.Vendor!.Name,
                VendorStreet = x.PurchaseOrder!.Vendor!.Street,
                VendorCity = x.PurchaseOrder!.Vendor!.City,
                CompanyName = company != null ? company.Name : string.Empty,
                CompanyStreet = company != null ? company.StreetAddress : string.Empty,
                CompanyCity = company != null ? company.City : string.Empty,
                CurrencySymbol = company != null && company.Currency != null ? company.Currency.Symbol : "IDR",
                BeforeTaxAmount = x.PurchaseOrder!.BeforeTaxAmount,
                TaxAmount = x.PurchaseOrder!.TaxAmount,
                AfterTaxAmount = x.PurchaseOrder!.AfterTaxAmount,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
                Items = x.PurchaseOrder!.PurchaseOrderItemList.Select(i => new BillPurchaseOrderItemResponse
                {
                    Id = i.Id,
                    ProductId = i.ProductId,
                    ProductName = i.Product!.Name,
                    Summary = i.Summary,
                    UnitPrice = i.UnitPrice,
                    Quantity = i.Quantity,
                    Total = i.Total
                }).ToList()
            }).FirstOrDefaultAsync(cancellationToken);
    }
}