using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ZhonTai.Admin.Core.Repositories;
using NPP.SmartSchedue.Api.Contracts.Services.Integration.Output;

namespace NPP.SmartSchedue.Api.Contracts.Domain.Integration
{
    /// <summary>
    /// 智能整合记录仓储接口
    /// 
    /// 业务思考：
    /// 1. 提供丰富的查询接口，支持多维度的历史数据分析
    /// 2. 考虑查询性能，提供必要的分页和索引优化
    /// 3. 支持统计分析功能，为业务决策提供数据支持
    /// 4. 考虑数据量增长，提供数据归档和清理机制
    /// </summary>
    public interface IIntegrationRecordRepository : IRepositoryBase<IntegrationRecordEntity>
    {
        #region 基础查询方法

        /// <summary>
        /// 根据批次编码获取整合记录
        /// 业务场景：根据批次编码快速定位特定的整合操作
        /// </summary>
        /// <param name="batchCode">批次编码</param>
        /// <returns>整合记录，如果不存在返回null</returns>
        Task<IntegrationRecordEntity?> GetByBatchCodeAsync(string batchCode);

        /// <summary>
        /// 获取指定时间范围内的整合记录
        /// 业务场景：时间范围查询，支持日报、周报、月报等业务需求
        /// </summary>
        /// <param name="startTime">开始时间（包含）</param>
        /// <param name="endTime">结束时间（包含）</param>
        /// <param name="pageIndex">页码（从1开始）</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>整合记录分页列表</returns>
        Task<(List<IntegrationRecordEntity> Records, long Total)> GetByTimeRangeAsync(
            DateTime startTime, 
            DateTime endTime, 
            int pageIndex = 1, 
            int pageSize = 20);

        /// <summary>
        /// 根据操作人员获取整合记录
        /// 业务场景：查看特定操作员的整合历史，用于工作量统计和绩效分析
        /// </summary>
        /// <param name="operatorUserId">操作员用户ID</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>整合记录分页列表</returns>
        Task<(List<IntegrationRecordEntity> Records, long Total)> GetByOperatorAsync(
            long operatorUserId, 
            int pageIndex = 1, 
            int pageSize = 20);

        /// <summary>
        /// 根据项目编号获取相关整合记录
        /// 业务场景：项目维度的整合历史查询和分析
        /// </summary>
        /// <param name="projectNumber">项目编号</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>整合记录分页列表</returns>
        Task<(List<IntegrationRecordEntity> Records, long Total)> GetByProjectNumberAsync(
            string projectNumber, 
            int pageIndex = 1, 
            int pageSize = 20);

        #endregion

        #region 复合查询方法

        /// <summary>
        /// 多维度综合查询整合记录
        /// 业务场景：复杂查询场景，支持多个条件的组合查询
        /// </summary>
        /// <param name="operatorUserId">操作员ID（可选）</param>
        /// <param name="projectNumber">项目编号（可选）</param>
        /// <param name="integrationType">整合类型（可选）</param>
        /// <param name="startTime">开始时间（可选）</param>
        /// <param name="endTime">结束时间（可选）</param>
        /// <param name="minSuccessRate">最小成功率（可选）</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>整合记录分页列表</returns>
        Task<(List<IntegrationRecordEntity> Records, long Total)> GetByMultipleConditionsAsync(
            long? operatorUserId = null,
            string? projectNumber = null,
            string? integrationType = null,
            DateTime? startTime = null,
            DateTime? endTime = null,
            decimal? minSuccessRate = null,
            int pageIndex = 1,
            int pageSize = 20);

        /// <summary>
        /// 获取包含指定任务的整合记录
        /// 业务场景：根据任务ID反向查询相关的整合历史
        /// </summary>
        /// <param name="taskId">任务ID</param>
        /// <returns>包含该任务的整合记录列表</returns>
        Task<List<IntegrationRecordEntity>> GetRecordsContainingTaskAsync(long taskId);

        #endregion

        #region 统计分析方法

        /// <summary>
        /// 获取指定时间段的整合统计数据
        /// 业务场景：生成统计报表，分析整合效果和趋势
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>统计数据</returns>
        Task<IntegrationStatistics> GetIntegrationStatisticsAsync(DateTime startTime, DateTime endTime);

        /// <summary>
        /// 获取操作员整合统计数据
        /// 业务场景：操作员工作量和效率分析
        /// </summary>
        /// <param name="operatorUserId">操作员ID</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>操作员统计数据</returns>
        Task<OperatorIntegrationStats> GetOperatorStatisticsAsync(
            long operatorUserId, 
            DateTime startTime, 
            DateTime endTime);

        /// <summary>
        /// 获取策略效果分析数据
        /// 业务场景：分析不同分配策略的效果，为策略优化提供数据支持
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>策略效果统计数据</returns>
        Task<List<StrategyEffectivenessStats>> GetStrategyEffectivenessAsync(
            DateTime startTime, 
            DateTime endTime);

        /// <summary>
        /// 获取整合性能趋势数据
        /// 业务场景：监控系统性能，识别性能瓶颈和优化机会
        /// </summary>
        /// <param name="days">最近天数</param>
        /// <returns>性能趋势数据</returns>
        Task<List<IntegrationPerformanceTrend>> GetPerformanceTrendAsync(int days = 30);

        #endregion

        #region 业务专用方法

        /// <summary>
        /// 检查批次编码是否已存在
        /// 业务场景：防止批次编码重复，确保数据唯一性
        /// </summary>
        /// <param name="batchCode">批次编码</param>
        /// <returns>是否存在</returns>
        Task<bool> BatchCodeExistsAsync(string batchCode);

        /// <summary>
        /// 获取最近的整合记录
        /// 业务场景：快速查看最新的整合状态，支持监控面板显示
        /// </summary>
        /// <param name="count">获取记录数量</param>
        /// <returns>最近的整合记录列表</returns>
        Task<List<IntegrationRecordEntity>> GetRecentRecordsAsync(int count = 10);

        /// <summary>
        /// 获取失败率高的整合记录
        /// 业务场景：识别问题操作，支持质量改进和问题排查
        /// </summary>
        /// <param name="minFailureRate">最小失败率阈值（百分比）</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="count">最大返回数量</param>
        /// <returns>高失败率的整合记录</returns>
        Task<List<IntegrationRecordEntity>> GetHighFailureRateRecordsAsync(
            decimal minFailureRate, 
            DateTime startTime, 
            DateTime endTime, 
            int count = 50);

        /// <summary>
        /// 软删除过期的测试数据
        /// 业务场景：定期清理测试数据，保持生产环境数据整洁
        /// </summary>
        /// <param name="beforeDate">删除此日期之前的测试数据</param>
        /// <returns>删除的记录数量</returns>
        Task<int> SoftDeleteExpiredTestDataAsync(DateTime beforeDate);

        #endregion
    }

    #region 统计数据模型

    /// <summary>
    /// 整合统计数据
    /// </summary>
    public class IntegrationStatistics
    {
        /// <summary>
        /// 统计时间范围开始
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 统计时间范围结束
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 整合操作总次数
        /// </summary>
        public int TotalIntegrations { get; set; }

        /// <summary>
        /// 涉及任务总数
        /// </summary>
        public long TotalTasks { get; set; }

        /// <summary>
        /// 成功分配任务数
        /// </summary>
        public long SuccessfulTasks { get; set; }

        /// <summary>
        /// 失败分配任务数
        /// </summary>
        public long FailedTasks { get; set; }

        /// <summary>
        /// 平均成功率
        /// </summary>
        public decimal AverageSuccessRate { get; set; }

        /// <summary>
        /// 平均执行耗时（毫秒）
        /// </summary>
        public long AverageElapsedTime { get; set; }

        /// <summary>
        /// 平均人员公平性评分
        /// </summary>
        public decimal AveragePersonnelFairnessScore { get; set; }

        /// <summary>
        /// 平均设备利用率
        /// </summary>
        public decimal AverageEquipmentUtilizationRate { get; set; }

        /// <summary>
        /// 活跃操作员数量
        /// </summary>
        public int ActiveOperatorCount { get; set; }

        /// <summary>
        /// 涉及项目数量
        /// </summary>
        public int InvolvedProjectCount { get; set; }
    }

    /// <summary>
    /// 操作员整合统计数据
    /// </summary>
    public class OperatorIntegrationStats
    {
        /// <summary>
        /// 操作员用户ID
        /// </summary>
        public long OperatorUserId { get; set; }

        /// <summary>
        /// 操作员用户名
        /// </summary>
        public string OperatorUserName { get; set; } = string.Empty;

        /// <summary>
        /// 操作员真实姓名
        /// </summary>
        public string OperatorRealName { get; set; } = string.Empty;

        /// <summary>
        /// 执行整合次数
        /// </summary>
        public int IntegrationCount { get; set; }

        /// <summary>
        /// 处理任务总数
        /// </summary>
        public long TotalTasks { get; set; }

        /// <summary>
        /// 平均成功率
        /// </summary>
        public decimal AverageSuccessRate { get; set; }

        /// <summary>
        /// 总耗时（毫秒）
        /// </summary>
        public long TotalElapsedTime { get; set; }

        /// <summary>
        /// 平均每次整合耗时
        /// </summary>
        public long AverageIntegrationTime { get; set; }

        /// <summary>
        /// 最后操作时间
        /// </summary>
        public DateTime LastOperationTime { get; set; }
    }

    /// <summary>
    /// 策略效果统计数据
    /// </summary>
    public class StrategyEffectivenessStats
    {
        /// <summary>
        /// 人员分配策略
        /// </summary>
        public string PersonnelStrategy { get; set; } = string.Empty;

        /// <summary>
        /// 设备分配策略
        /// </summary>
        public string EquipmentStrategy { get; set; } = string.Empty;

        /// <summary>
        /// 使用次数
        /// </summary>
        public int UsageCount { get; set; }

        /// <summary>
        /// 平均成功率
        /// </summary>
        public decimal AverageSuccessRate { get; set; }

        /// <summary>
        /// 平均人员公平性评分
        /// </summary>
        public decimal AveragePersonnelFairnessScore { get; set; }

        /// <summary>
        /// 平均设备利用率
        /// </summary>
        public decimal AverageEquipmentUtilizationRate { get; set; }

        /// <summary>
        /// 平均执行耗时
        /// </summary>
        public long AverageElapsedTime { get; set; }
    }

    /// <summary>
    /// 整合性能趋势数据
    /// </summary>
    public class IntegrationPerformanceTrend
    {
        /// <summary>
        /// 日期
        /// </summary>
        public DateTime Date { get; set; }

        /// <summary>
        /// 当日整合次数
        /// </summary>
        public int IntegrationCount { get; set; }

        /// <summary>
        /// 当日平均成功率
        /// </summary>
        public decimal AverageSuccessRate { get; set; }

        /// <summary>
        /// 当日平均耗时（毫秒）
        /// </summary>
        public long AverageElapsedTime { get; set; }

        /// <summary>
        /// 当日处理任务总数
        /// </summary>
        public long TotalTasks { get; set; }
    }

    #endregion
}