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

namespace foodmarket.Domain.Purchases;

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

public class Supply : TenantEntity
{
    /// <summary>Human-readable document number, unique per organization (e.g. "П-2026-000001").</summary>
    public string Number { get; set; } = "";

    public DateTime Date { get; set; } = DateTime.UtcNow;
    public SupplyStatus Status { get; set; } = SupplyStatus.Draft;

    public Guid SupplierId { get; set; }
    public Counterparty Supplier { get; set; } = null!;

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

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

    public string? Notes { get; set; }

    /// <summary>Sum of line totals. Computed on save.</summary>
    public decimal Total { get; set; }

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

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

public class SupplyLine : TenantEntity
{
    public Guid SupplyId { get; set; }
    public Supply Supply { 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 LineTotal { get; set; }

    public int SortOrder { get; set; }

    /// <summary>Если true — пользователь вручную задал розничную цену для
    /// этой строки (через UI приёмки). При Posting автонаценка по Group.MarkupPercent
    /// для этой строки пропускается.</summary>
    public bool RetailPriceManuallyOverridden { get; set; }

    /// <summary>Розничная цена, которую пользователь вписал в колонке «Розничная»
    /// строки приёмки. Применяется к Product.Prices[default] при Posting.</summary>
    public decimal? RetailPriceOverride { get; set; }
}
