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

namespace MiGu.DB.Domains.Transport;

// 运输规则/任务/预占实体。状态字段为枚举；复数 *Statuses 辅助类供 Server 解析 DTO 字符串。

public sealed class WmsTransportRule : AggregateRoot
{
    [MaxLength(64)] public string Code { get; set; } = "";
    [MaxLength(128)] public string Name { get; set; } = "";
    public WmsTransportTriggerType TriggerType { get; set; } = WmsTransportTriggerType.MaterialCall;
    public bool Enabled { get; set; } = true;
    public int Priority { get; set; }
    public string SourceSelectorJson { get; set; } = "{}";
    public string TargetSelectorJson { get; set; } = "{}";
    public string TaskOptionsJson { get; set; } = "{}";
}

public sealed class WmsTransportTask : AggregateRoot
{
    [MaxLength(64)] public string BusinessType { get; set; } = "";
    public Guid? RuleId { get; set; }
    public Guid SourceStorageId { get; set; }
    public Guid TargetStorageId { get; set; }
    public Guid ContainerId { get; set; }
    public Guid? MaterialId { get; set; }
    public decimal? Quantity { get; set; }
    public WmsTransportTaskStatus Status { get; set; } = WmsTransportTaskStatus.Pending;
    [MaxLength(64)] public string DispatchMissionId { get; set; } = "";
    [MaxLength(64)] public string DeliveryId { get; set; } = "";
    [MaxLength(32)] public string DispatchStatus { get; set; } = "";
    [MaxLength(500)] public string Reason { get; set; } = "";
    public string SnapshotJson { get; set; } = "{}";
    [MaxLength(1000)] public string ErrorMessage { get; set; } = "";
    public int TaskPriority { get; set; }
}

public sealed class WmsTransportReservation : AggregateRoot
{
    public Guid TaskId { get; set; }
    public Guid ContainerId { get; set; }
    public Guid SourceStorageId { get; set; }
    public Guid TargetStorageId { get; set; }
    public WmsReservationStatus Status { get; set; } = WmsReservationStatus.Active;
    public DateTimeOffset? ExpiresAt { get; set; }
}

public sealed class WmsTransportTaskHistory : Entity<Guid>
{
    public Guid TaskId { get; set; }
    [MaxLength(32)] public string FromStatus { get; set; } = "";
    [MaxLength(32)] public string ToStatus { get; set; } = "";
    [MaxLength(128)] public string Operator { get; set; } = "";
    public DateTimeOffset OperatedAt { get; set; }
    [MaxLength(500)] public string Reason { get; set; } = "";
    [MaxLength(1000)] public string ErrorMessage { get; set; } = "";
    public string SnapshotJson { get; set; } = "{}";
}

public static class WmsTransportTriggerTypes
{
    // 复数辅助类：与枚举分离，避免与属性/类型同名冲突，并提供 ParseOr / All
    public const WmsTransportTriggerType MaterialCall = WmsTransportTriggerType.MaterialCall;
    public const WmsTransportTriggerType FinishedGoodsOffline = WmsTransportTriggerType.FinishedGoodsOffline;
    public const WmsTransportTriggerType AutoTransfer = WmsTransportTriggerType.AutoTransfer;
    public static readonly HashSet<WmsTransportTriggerType> All = new()
        { MaterialCall, FinishedGoodsOffline, AutoTransfer };

    public static bool IsDefined(string? value) =>
        Enum.TryParse<WmsTransportTriggerType>(value, true, out var e) && All.Contains(e);

    public static WmsTransportTriggerType ParseOr(string? value, WmsTransportTriggerType fallback = WmsTransportTriggerType.MaterialCall) =>
        Enum.TryParse<WmsTransportTriggerType>(value, true, out var e) && All.Contains(e) ? e : fallback;
}

public static class WmsTransportTaskStatuses
{
    public const WmsTransportTaskStatus Pending = WmsTransportTaskStatus.Pending;
    public const WmsTransportTaskStatus Reserved = WmsTransportTaskStatus.Reserved;
    public const WmsTransportTaskStatus Dispatched = WmsTransportTaskStatus.Dispatched;
    public const WmsTransportTaskStatus InTransit = WmsTransportTaskStatus.InTransit;
    public const WmsTransportTaskStatus Completed = WmsTransportTaskStatus.Completed;
    public const WmsTransportTaskStatus Failed = WmsTransportTaskStatus.Failed;
    public const WmsTransportTaskStatus Cancelled = WmsTransportTaskStatus.Cancelled;

    public static readonly HashSet<WmsTransportTaskStatus> Active = new()
        { Pending, Reserved, Dispatched, InTransit };
    public static readonly HashSet<WmsTransportTaskStatus> All = new()
        { Pending, Reserved, Dispatched, InTransit, Completed, Failed, Cancelled };
}

public static class WmsReservationStatuses
{
    public const WmsReservationStatus Active = WmsReservationStatus.Active;
    public const WmsReservationStatus Released = WmsReservationStatus.Released;
    public const WmsReservationStatus Expired = WmsReservationStatus.Expired;
}

public static class WmsDispatchStatuses
{
    public const WmsDispatchStatus Dispatched = WmsDispatchStatus.Dispatched;
    public const WmsDispatchStatus Failed = WmsDispatchStatus.Failed;
}
