namespace Apollinare.Domain.Entities.Warehouse;

/// <summary>
/// Testata movimento di magazzino (carico, scarico, trasferimento, rettifica)
/// </summary>
public class StockMovement : BaseEntity
{
    /// <summary>
    /// Numero documento movimento
    /// </summary>
    public string DocumentNumber { get; set; } = string.Empty;

    /// <summary>
    /// Data movimento
    /// </summary>
    public DateTime MovementDate { get; set; }

    /// <summary>
    /// Tipo movimento
    /// </summary>
    public MovementType Type { get; set; }

    /// <summary>
    /// Causale movimento
    /// </summary>
    public int? ReasonId { get; set; }

    /// <summary>
    /// Magazzino di origine (per scarichi e trasferimenti)
    /// </summary>
    public int? SourceWarehouseId { get; set; }

    /// <summary>
    /// Magazzino di destinazione (per carichi e trasferimenti)
    /// </summary>
    public int? DestinationWarehouseId { get; set; }

    /// <summary>
    /// Riferimento documento esterno (DDT, fattura, ordine)
    /// </summary>
    public string? ExternalReference { get; set; }

    /// <summary>
    /// Tipo documento esterno
    /// </summary>
    public ExternalDocumentType? ExternalDocumentType { get; set; }

    /// <summary>
    /// ID fornitore (per carichi da acquisto)
    /// </summary>
    public int? SupplierId { get; set; }

    /// <summary>
    /// ID cliente (per scarichi per vendita)
    /// </summary>
    public int? CustomerId { get; set; }

    /// <summary>
    /// Stato del movimento
    /// </summary>
    public MovementStatus Status { get; set; } = MovementStatus.Draft;

    /// <summary>
    /// Data conferma movimento
    /// </summary>
    public DateTime? ConfirmedDate { get; set; }

    /// <summary>
    /// Utente che ha confermato
    /// </summary>
    public string? ConfirmedBy { get; set; }

    /// <summary>
    /// Valore totale movimento
    /// </summary>
    public decimal? TotalValue { get; set; }

    /// <summary>
    /// Note movimento
    /// </summary>
    public string? Notes { get; set; }

    // Navigation properties
    public WarehouseLocation? SourceWarehouse { get; set; }
    public WarehouseLocation? DestinationWarehouse { get; set; }
    public MovementReason? Reason { get; set; }
    public ICollection<StockMovementLine> Lines { get; set; } = new List<StockMovementLine>();
}

/// <summary>
/// Tipo di movimento magazzino
/// </summary>
public enum MovementType
{
    /// <summary>
    /// Carico (aumento giacenza)
    /// </summary>
    Inbound = 0,

    /// <summary>
    /// Scarico (diminuzione giacenza)
    /// </summary>
    Outbound = 1,

    /// <summary>
    /// Trasferimento tra magazzini
    /// </summary>
    Transfer = 2,

    /// <summary>
    /// Rettifica inventariale (positiva o negativa)
    /// </summary>
    Adjustment = 3,

    /// <summary>
    /// Produzione (carico da ciclo produttivo)
    /// </summary>
    Production = 4,

    /// <summary>
    /// Consumo (scarico per produzione)
    /// </summary>
    Consumption = 5,

    /// <summary>
    /// Reso a fornitore
    /// </summary>
    SupplierReturn = 6,

    /// <summary>
    /// Reso da cliente
    /// </summary>
    CustomerReturn = 7
}

/// <summary>
/// Stato del movimento
/// </summary>
public enum MovementStatus
{
    /// <summary>
    /// Bozza (non ancora confermato)
    /// </summary>
    Draft = 0,

    /// <summary>
    /// Confermato (giacenze aggiornate)
    /// </summary>
    Confirmed = 1,

    /// <summary>
    /// Annullato
    /// </summary>
    Cancelled = 2
}

/// <summary>
/// Tipo documento esterno collegato
/// </summary>
public enum ExternalDocumentType
{
    /// <summary>
    /// Ordine fornitore
    /// </summary>
    PurchaseOrder = 0,

    /// <summary>
    /// DDT entrata
    /// </summary>
    InboundDeliveryNote = 1,

    /// <summary>
    /// Fattura acquisto
    /// </summary>
    PurchaseInvoice = 2,

    /// <summary>
    /// Ordine cliente
    /// </summary>
    SalesOrder = 3,

    /// <summary>
    /// DDT uscita
    /// </summary>
    OutboundDeliveryNote = 4,

    /// <summary>
    /// Fattura vendita
    /// </summary>
    SalesInvoice = 5,

    /// <summary>
    /// Ordine di produzione
    /// </summary>
    ProductionOrder = 6,

    /// <summary>
    /// Documento inventario
    /// </summary>
    InventoryDocument = 7
}
