using foodmarket.Domain.Catalog;
using foodmarket.Domain.Common;

namespace foodmarket.Domain.Sales;

public enum RetailSaleStatus
{
    Draft = 0,
    Posted = 1,
}

public enum PaymentMethod
{
    Cash = 0,
    Card = 1,
    BankTransfer = 2,
    Bonus = 3,
    Mixed = 99,
}

public class RetailSale : TenantEntity
{
    public string Number { get; set; } = "";
    public DateTime Date { get; set; } = DateTime.UtcNow;
    public RetailSaleStatus Status { get; set; } = RetailSaleStatus.Draft;

    public Guid StoreId { get; set; }
    public Store Store { get; set; } = null!;

    public Guid? RetailPointId { get; set; }
    public RetailPoint? RetailPoint { get; set; }

    public Guid? CustomerId { get; set; }
    public Counterparty? Customer { get; set; }

    public Guid? CashierUserId { get; set; }

    public Guid CurrencyId { get; set; }
    public Currency Currency { get; set; } = null!;

    public decimal Subtotal { get; set; }       // sum of LineTotal before discount
    public decimal DiscountTotal { get; set; }
    public decimal Total { get; set; }          // = Subtotal - DiscountTotal

    public PaymentMethod Payment { get; set; } = PaymentMethod.Cash;
    public decimal PaidCash { get; set; }
    public decimal PaidCard { get; set; }

    public string? Notes { get; set; }
    public DateTime? PostedAt { get; set; }
    public Guid? PostedByUserId { get; set; }

    public ICollection<RetailSaleLine> Lines { get; set; } = new List<RetailSaleLine>();
}

public class RetailSaleLine : TenantEntity
{
    public Guid RetailSaleId { get; set; }
    public RetailSale RetailSale { get; set; } = null!;

    public Guid ProductId { get; set; }
    public Product Product { get; set; } = null!;

    public decimal Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public decimal Discount { get; set; }
    public decimal LineTotal { get; set; }      // = Quantity * UnitPrice - Discount
    public decimal VatPercent { get; set; }     // snapshot
    public int SortOrder { get; set; }
}
