using System.ComponentModel.DataAnnotations;

namespace MilitaryTrainingManagement.Models.DTOs;

/// <summary>
/// 分配请求项
/// </summary>
public class DistributionRequestItem
{
    public int TargetUnitId { get; set; }
    public decimal UnitQuota { get; set; }
}

/// <summary>
/// 创建物资配额请求
/// </summary>
public class CreateAllocationRequest
{
    /// <summary>
    /// 类别
    /// </summary>
    [Required(ErrorMessage = "类别为必填项")]
    [MaxLength(100, ErrorMessage = "类别长度不能超过100个字符")]
    public string Category { get; set; } = string.Empty;

    /// <summary>
    /// 物资名称
    /// </summary>
    [Required(ErrorMessage = "物资名称为必填项")]
    [MaxLength(200, ErrorMessage = "物资名称长度不能超过200个字符")]
    public string MaterialName { get; set; } = string.Empty;

    /// <summary>
    /// 计量单位
    /// </summary>
    [Required(ErrorMessage = "计量单位为必填项")]
    [MaxLength(50, ErrorMessage = "计量单位长度不能超过50个字符")]
    public string Unit { get; set; } = string.Empty;

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

    /// <summary>
    /// 分配记录
    /// </summary>
    public List<DistributionRequestItem> Distributions { get; set; } = new();
    
    /// <summary>
    /// 转换为字典格式
    /// </summary>
    public Dictionary<int, decimal> GetDistributionsDictionary()
    {
        return Distributions
            .Where(d => d.TargetUnitId > 0)
            .ToDictionary(d => d.TargetUnitId, d => d.UnitQuota);
    }
}

/// <summary>
/// 更新物资配额请求
/// </summary>
public class UpdateAllocationRequest
{
    /// <summary>
    /// 类别
    /// </summary>
    [Required(ErrorMessage = "类别为必填项")]
    [MaxLength(100, ErrorMessage = "类别长度不能超过100个字符")]
    public string Category { get; set; } = string.Empty;

    /// <summary>
    /// 物资名称
    /// </summary>
    [Required(ErrorMessage = "物资名称为必填项")]
    [MaxLength(200, ErrorMessage = "物资名称长度不能超过200个字符")]
    public string MaterialName { get; set; } = string.Empty;

    /// <summary>
    /// 计量单位
    /// </summary>
    [Required(ErrorMessage = "计量单位为必填项")]
    [MaxLength(50, ErrorMessage = "计量单位长度不能超过50个字符")]
    public string Unit { get; set; } = string.Empty;

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

/// <summary>
/// 更新配额分配实际完成数量请求(上报消耗)
/// </summary>
public class UpdateDistributionRequest
{
    /// <summary>
    /// 实际完成数量
    /// </summary>
    [Required(ErrorMessage = "实际完成数量为必填项")]
    [Range(0, double.MaxValue, ErrorMessage = "实际完成数量不能为负数")]
    public decimal ActualCompletion { get; set; }

    /// <summary>
    /// 备注
    /// </summary>
    [MaxLength(500)]
    public string? Remarks { get; set; }
}

/// <summary>
/// 物资配额响应
/// </summary>
public class AllocationResponse
{
    public int Id { get; set; }
    public string Category { get; set; } = string.Empty;
    public string MaterialName { get; set; } = string.Empty;
    public string Unit { get; set; } = string.Empty;
    public decimal TotalQuota { get; set; }
    public int CreatedByUnitId { get; set; }
    public string CreatedByUnitName { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
    public List<AllocationDistributionResponse> Distributions { get; set; } = new();
    
    /// <summary>
    /// 用户可见范围内的上报总和（营部及以下级别使用）
    /// </summary>
    public decimal? VisibleActualCompletion { get; set; }
}

/// <summary>
/// 配额分配响应
/// </summary>
public class AllocationDistributionResponse
{
    public int Id { get; set; }
    public int TargetUnitId { get; set; }
    public string TargetUnitName { get; set; } = string.Empty;
    public decimal UnitQuota { get; set; }
    public decimal? ActualCompletion { get; set; }
    public decimal CompletionRate { get; set; }
    public DateTime? ReportedAt { get; set; }
    public string? ReportedByUserName { get; set; }
}
