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

namespace foodmarket.Domain.Purchases;

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

/// <summary>Возврат поставщику. Отгрузка товара обратно поставщику (брак, излишек,
/// неликвид). При проведении создаёт <see cref="Inventory.StockMovement"/> с типом
/// <see cref="Inventory.MovementType.SupplierReturn"/> и отрицательным Quantity.
/// Опционально ссылается на исходную приёмку через <see cref="ReferenceSupplyId"/>.</summary>
public class SupplierReturn : TenantEntity
{
    public string Number { get; set; } = "";
    public DateTime Date { get; set; } = DateTime.UtcNow;
    public SupplierReturnStatus Status { get; set; } = SupplierReturnStatus.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!;

    /// <summary>Опциональная ссылка на исходную приёмку.</summary>
    public Guid? ReferenceSupplyId { get; set; }
    public Supply? ReferenceSupply { get; set; }

    public string? Notes { get; set; }

    public decimal Total { get; set; }

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

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

public class SupplierReturnLine : TenantEntity
{
    public Guid SupplierReturnId { get; set; }
    public SupplierReturn SupplierReturn { 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; }
}
