using MilitaryTrainingManagement.Models.Entities;
using MilitaryTrainingManagement.Models.Enums;

namespace MilitaryTrainingManagement.Services.Interfaces;

/// <summary>
/// 人员管理服务接口
/// </summary>
public interface IPersonnelService
{
    Task<Personnel?> GetByIdAsync(int id);
    Task<Personnel?> GetByIdNumberAsync(string idNumber);
    Task<IEnumerable<Personnel>> GetByUnitAsync(int unitId, bool includeSubordinates = false);
    Task<Personnel> CreateAsync(Personnel personnel);
    Task<Personnel> UpdateAsync(Personnel personnel);
    Task<Personnel> ApproveAsync(int personnelId, int approvedByUnitId, PersonnelLevel? level = null);
    Task<Personnel> RejectAsync(int personnelId, int reviewedByUserId, int reviewedByUnitId, string? comments = null);
    Task DeleteAsync(int id);
    
    /// <summary>
    /// 提交人员数据（包含文件上传和验证）
    /// </summary>
    Task<Personnel> SubmitPersonnelAsync(Personnel personnel, IFormFile? photo, IFormFile? supportingDocument);
    
    /// <summary>
    /// 验证人员数据完整性
    /// </summary>
    Task<PersonnelValidationResult> ValidatePersonnelDataAsync(Personnel personnel);
    
    /// <summary>
    /// 获取审批路由的目标组织单位
    /// </summary>
    Task<OrganizationalUnit> GetApprovalTargetUnitAsync(int submittingUnitId);
    
    /// <summary>
    /// 检查人员是否可以修改（未被审批）
    /// </summary>
    Task<bool> CanModifyPersonnelAsync(int personnelId);
    
    /// <summary>
    /// 请求修改已审批的人员记录
    /// </summary>
    Task<ApprovalRequest> RequestPersonnelModificationAsync(int personnelId, int requestedByUserId, string reason, Personnel modifiedPersonnel);
    
    /// <summary>
    /// 根据组织层级获取可见的人员列表
    /// </summary>
    Task<IEnumerable<Personnel>> GetVisiblePersonnelAsync(int unitId, OrganizationalLevel userLevel);
    
    /// <summary>
    /// 实现跨层级人员转移
    /// </summary>
    Task<Personnel> TransferPersonnelAsync(int personnelId, int targetUnitId, int approvedByUnitId);
    
    /// <summary>
    /// 获取待审批的人员列表
    /// </summary>
    Task<IEnumerable<Personnel>> GetPendingApprovalPersonnelAsync(int unitId);
    
    /// <summary>
    /// 批量审批人员
    /// </summary>
    Task<IEnumerable<Personnel>> BatchApprovePersonnelAsync(int[] personnelIds, int approvedByUnitId, PersonnelLevel level);
    
    /// <summary>
    /// 批量拒绝人员
    /// </summary>
    Task<IEnumerable<Personnel>> BatchRejectPersonnelAsync(int[] personnelIds, int reviewedByUserId, int reviewedByUnitId, string reason);
    
    /// <summary>
    /// 获取人员审批历史
    /// </summary>
    Task<IEnumerable<PersonnelApprovalHistory>> GetPersonnelApprovalHistoryAsync(int personnelId);
    
    /// <summary>
    /// 验证审批权限
    /// </summary>
    Task<bool> CanApprovePersonnelAsync(int userId, int personnelId);
    
    /// <summary>
    /// 发起向上申报请求
    /// </summary>
    Task<Personnel> RequestUpgradeAsync(int personnelId, int requestByUnitId);
    
    /// <summary>
    /// 审批向上申报请求
    /// </summary>
    Task<Personnel> ApproveUpgradeAsync(int personnelId, int approvedByUnitId);
    
    /// <summary>
    /// 拒绝向上申报请求
    /// </summary>
    Task<Personnel> RejectUpgradeAsync(int personnelId, int rejectedByUnitId, string comments);
    
    /// <summary>
    /// 师部直接升级团级人才为师级
    /// </summary>
    Task<Personnel> DirectUpgradeAsync(int personnelId, int divisionUnitId);
    
    /// <summary>
    /// 重新提交被拒绝的人才审批
    /// </summary>
    Task<Personnel> ResubmitPersonnelAsync(int personnelId);
}

/// <summary>
/// 人员验证结果
/// </summary>
public class PersonnelValidationResult
{
    public bool IsValid { get; set; }
    public List<string> Errors { get; set; } = new();
    
    public static PersonnelValidationResult Success() => new() { IsValid = true };
    
    public static PersonnelValidationResult Failure(params string[] errors) => new()
    {
        IsValid = false,
        Errors = errors.ToList()
    };
}
