using FsCheck;
using FsCheck.Xunit;
using MilitaryTrainingManagement.Models.Entities;
using MilitaryTrainingManagement.Models.Enums;

namespace MilitaryTrainingManagement.Tests.Properties;

/// <summary>
/// 组织层级属性测试
/// **功能：military-training-material-management，属性10：组织层级分类**
/// **验证需求：需求1.2**
/// </summary>
public class OrganizationalLevelPropertyTests
{
    /// <summary>
    /// 属性10：组织层级分类
    /// *对于任何*组织单位，应被分类为四个有效层级中的一个：师团、团、营或连
    /// **验证需求：需求1.2**
    /// </summary>
    [Property(MaxTest = 100)]
    public bool OrganizationalUnit_Level_MustBeOneOfFourValidLevels(OrganizationalLevel level)
    {
        // 创建组织单位
        var unit = new OrganizationalUnit
        {
            Id = 1,
            Name = "测试单位",
            Level = level,
            CreatedAt = DateTime.UtcNow
        };

        // 验证层级是四个有效值之一
        var validLevels = new[]
        {
            OrganizationalLevel.Division,   // 师团
            OrganizationalLevel.Regiment,   // 团
            OrganizationalLevel.Battalion,  // 营
            OrganizationalLevel.Company     // 连
        };

        return validLevels.Contains(unit.Level);
    }

    /// <summary>
    /// 属性10扩展：组织层级数值必须在1-4范围内
    /// **验证需求：需求1.2**
    /// </summary>
    [Property(MaxTest = 100)]
    public bool OrganizationalLevel_NumericValue_MustBeInValidRange(OrganizationalLevel level)
    {
        var numericValue = (int)level;
        // 层级数值必须在1（师团）到4（连）之间
        return numericValue >= 1 && numericValue <= 4;
    }

    /// <summary>
    /// 属性10扩展：组织层级层次结构正确性
    /// 师团(1) > 团(2) > 营(3) > 连(4)
    /// **验证需求：需求1.2**
    /// </summary>
    [Property(MaxTest = 100)]
    public bool OrganizationalLevel_Hierarchy_IsCorrectlyOrdered(
        OrganizationalLevel level1, 
        OrganizationalLevel level2)
    {
        // 如果level1的数值小于level2，则level1是更高层级
        var value1 = (int)level1;
        var value2 = (int)level2;

        if (value1 < value2)
        {
            // level1是上级，level2是下级
            return IsHigherLevel(level1, level2);
        }
        else if (value1 > value2)
        {
            // level2是上级，level1是下级
            return IsHigherLevel(level2, level1);
        }
        else
        {
            // 相同层级
            return level1 == level2;
        }
    }

    private static bool IsHigherLevel(OrganizationalLevel higher, OrganizationalLevel lower)
    {
        return (int)higher < (int)lower;
    }
}
