using System.ComponentModel.DataAnnotations;

namespace MilitaryTrainingManagement.Models.Entities;

/// <summary>
/// 物资配额实体
/// </summary>
public class MaterialAllocation
{
    public int Id { get; set; }

    /// <summary>
    /// 类别
    /// </summary>
    [Required(ErrorMessage = "类别为必填项")]
    [MaxLength(100)]
    public string Category { get; set; } = string.Empty;

    /// <summary>
    /// 弹药/器材名称
    /// </summary>
    [Required(ErrorMessage = "物资名称为必填项")]
    [MaxLength(200)]
    public string MaterialName { get; set; } = string.Empty;

    /// <summary>
    /// 计量单位
    /// </summary>
    [Required(ErrorMessage = "计量单位为必填项")]
    [MaxLength(50)]
    public string Unit { get; set; } = string.Empty;

    /// <summary>
    /// 总训练指标
    /// </summary>
    [Range(0.01, double.MaxValue, ErrorMessage = "总配额必须大于0")]
    public decimal TotalQuota { get; set; }

    /// <summary>
    /// 创建单位ID
    /// </summary>
    public int CreatedByUnitId { get; set; }

    /// <summary>
    /// 创建单位
    /// </summary>
    public OrganizationalUnit CreatedByUnit { get; set; } = null!;

    /// <summary>
    /// 分配记录
    /// </summary>
    public List<AllocationDistribution> Distributions { get; set; } = new();

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 验证分配配额总和是否不超过总配额
    /// </summary>
    public bool ValidateDistributionQuotas()
    {
        if (Distributions == null || !Distributions.Any())
            return true;
        
        return Distributions.Sum(d => d.UnitQuota) <= TotalQuota;
    }
}
