using System.ComponentModel.DataAnnotations;
using MiGu.DB.Kernel.Entities;

namespace MiGu.DB.Domains.Wms;

// WMS 聚合实体。状态/类型字段为枚举（库内 TEXT，见全局 HasConversion）；DTO 仍用字符串，由 *Statuses 辅助类解析。

public sealed class Warehouse : AggregateRoot
{
    [MaxLength(64)] public string Code { get; set; } = "";
    [MaxLength(128)] public string Name { get; set; } = "";
    [MaxLength(64)] public string Type { get; set; } = "Default";
    public bool Enabled { get; set; } = true;
    public int SortOrder { get; set; }
}

public sealed class WarehouseArea : AggregateRoot
{
    public Guid WarehouseId { get; set; }
    [MaxLength(64)] public string Code { get; set; } = "";
    [MaxLength(128)] public string Name { get; set; } = "";
    [MaxLength(64)] public string Type { get; set; } = "Storage";
    public AreaLayoutMode LayoutMode { get; set; } = AreaLayoutMode.Flat;
    [MaxLength(32)] public string State { get; set; } = "Default";
    public bool Enabled { get; set; } = true;
    public int SortOrder { get; set; }
}

public sealed class Storage : AggregateRoot
{
    public Guid AreaId { get; set; }
    [MaxLength(64)] public string Code { get; set; } = "";
    [MaxLength(128)] public string Name { get; set; } = "";
    [MaxLength(64)] public string StorageType { get; set; } = StorageTypeCodes.Storage;
    // 属性名与枚举类型同名合法；默认值引用枚举成员 LocationKind.Station（勿再引入同名静态类）
    public LocationKind LocationKind { get; set; } = LocationKind.Station;
    public int ColumnNo { get; set; }
    public int LevelNo { get; set; } = 1;
    public int DepthNo { get; set; } = 1;
    [MaxLength(64)] public string SiteId { get; set; } = "";
    [MaxLength(64)] public string SiteCode { get; set; } = "";
    [MaxLength(128)] public string Barcode { get; set; } = "";
    public int Capacity { get; set; }
    public StorageStatus Status { get; set; } = StorageStatus.Empty;
    [MaxLength(64)] public string Usage { get; set; } = "";
    public int Priority { get; set; }
    [MaxLength(64)] public string ZoneCode { get; set; } = "";
    public bool AllowInbound { get; set; } = true;
    public bool AllowOutbound { get; set; } = true;
    public bool Enabled { get; set; } = true;
}

public sealed class Container : AggregateRoot
{
    public Guid? AreaId { get; set; }
    [MaxLength(64)] public string Code { get; set; } = "";
    [MaxLength(128)] public string Name { get; set; } = "";
    [MaxLength(64)] public string ContainerType { get; set; } = "Box";
    public ContainerStatus Status { get; set; } = ContainerStatus.EmptyMaterial;
    [MaxLength(128)] public string Barcode { get; set; } = "";
    public double Length { get; set; }
    public double Width { get; set; }
    public double Height { get; set; }
    public bool Enabled { get; set; } = true;
}

public sealed class MaterialType : AggregateRoot
{
    [MaxLength(64)] public string Code { get; set; } = "";
    [MaxLength(128)] public string Name { get; set; } = "";
    [MaxLength(128)] public string Spec { get; set; } = "";
    [MaxLength(32)] public string Unit { get; set; } = "pcs";
    [MaxLength(64)] public string Category { get; set; } = "";
    [MaxLength(64)] public string BarcodePrefix { get; set; } = "";
    public bool Enabled { get; set; } = true;
}

public sealed class Material : AggregateRoot
{
    [MaxLength(64)] public string Code { get; set; } = "";
    [MaxLength(128)] public string Name { get; set; } = "";
    [MaxLength(64)] public string TypeCode { get; set; } = "";
    [MaxLength(128)] public string Barcode { get; set; } = "";
    [MaxLength(128)] public string Spec { get; set; } = "";
    [MaxLength(32)] public string Unit { get; set; } = "pcs";
    [MaxLength(64)] public string Category { get; set; } = "";
    public MaterialLifecycle LifecycleStatus { get; set; } = MaterialLifecycle.Active;
    public DateTimeOffset? UnboundAt { get; set; }
    public bool Enabled { get; set; } = true;
}

public sealed class ContainerLocation : AggregateRoot
{
    public Guid ContainerId { get; set; }
    public ContainerLocationType LocationType { get; set; } = ContainerLocationType.Storage;
    [MaxLength(64)] public string LocationId { get; set; } = "";
    /// <summary>LocationType=Storage 时的库位 Id；由 LocationId 回填，供库存 join 下推。</summary>
    public Guid? StorageId { get; set; }
    [MaxLength(64)] public string LocationCode { get; set; } = "";
    [MaxLength(128)] public string LocationName { get; set; } = "";
    public ContainerLocationStatus Status { get; set; } = ContainerLocationStatus.Active;
    public DateTimeOffset EnteredAt { get; set; }
}

public sealed class ContainerMaterial : AggregateRoot
{
    public Guid ContainerId { get; set; }
    public Guid MaterialId { get; set; }
    public decimal Quantity { get; set; } = 1;
    [MaxLength(64)] public string BatchNo { get; set; } = "";
    [MaxLength(64)] public string SerialNo { get; set; } = "";
    public ContainerMaterialStatus Status { get; set; } = ContainerMaterialStatus.Bound;
    public DateTimeOffset BoundAt { get; set; }
    public DateTimeOffset LoadedAt { get; set; }
    public DateTimeOffset? UnloadedAt { get; set; }
}

public sealed class StockEvent : Kernel.Entities.Entity<Guid>
{
    public StockEventType EventType { get; set; }
    public Guid? MaterialId { get; set; }
    [MaxLength(64)] public string MaterialCode { get; set; } = "";
    [MaxLength(128)] public string MaterialName { get; set; } = "";
    [MaxLength(128)] public string MaterialBarcode { get; set; } = "";
    [MaxLength(64)] public string MaterialTypeCode { get; set; } = "";
    public Guid? ContainerId { get; set; }
    [MaxLength(64)] public string ContainerCode { get; set; } = "";
    [MaxLength(128)] public string ContainerName { get; set; } = "";
    public Guid? StorageId { get; set; }
    [MaxLength(64)] public string StorageCode { get; set; } = "";
    [MaxLength(128)] public string StorageName { get; set; } = "";
    [MaxLength(64)] public string AreaCode { get; set; } = "";
    public Guid? FromStorageId { get; set; }
    [MaxLength(64)] public string FromStorageCode { get; set; } = "";
    [MaxLength(128)] public string FromStorageName { get; set; } = "";
    public Guid? ToStorageId { get; set; }
    [MaxLength(64)] public string ToStorageCode { get; set; } = "";
    [MaxLength(128)] public string ToStorageName { get; set; } = "";
    [MaxLength(64)] public string RefType { get; set; } = "Manual";
    public Guid? RefId { get; set; }
    [MaxLength(64)] public string RefCode { get; set; } = "";
    [MaxLength(128)] public string Operator { get; set; } = "";
    public DateTimeOffset OperatedAt { get; set; }
    [MaxLength(500)] public string Reason { get; set; } = "";
}

public sealed class ContainerLocationHistory : HistoryEntity
{
    [MaxLength(32)] public string FromLocationType { get; set; } = "";
    [MaxLength(64)] public string FromLocationId { get; set; } = "";
    [MaxLength(32)] public string ToLocationType { get; set; } = "";
    [MaxLength(64)] public string ToLocationId { get; set; } = "";
}

public sealed class ContainerMaterialHistory : HistoryEntity
{
    public Guid? MaterialId { get; set; }
    public decimal QuantityDelta { get; set; }
}

public static class StorageTypeCodes
{
    public const string Storage = "Storage";
    public const string LineSide = "LineSide";
    public const string OfflinePoint = "OfflinePoint";
    public const string Buffer = "Buffer";
    public const string FinishedGoods = "FinishedGoods";
}

public static class WmsDefaults
{
    public const string DefaultWarehouseCode = "DEFAULT";
    public const string DefaultWarehouseName = "默认仓库";
}
