using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NPP.SmartSchedue.Api.Contracts.Domain.Notification;
using NPP.SmartSchedue.Api.Contracts.Core.Enums;
using ZhonTai.Admin.Core.Repositories;

namespace NPP.SmartSchedue.Api.Contracts.Core.Repositories;

/// <summary>
/// 通知任务仓储接口
/// </summary>
public interface INotificationTaskRepository : IRepositoryBase<NotificationTaskEntity>
{
    /// <summary>
    /// 根据通知设置ID获取任务列表
    /// </summary>
    /// <param name="notificationSettingId">通知设置ID</param>
    /// <returns></returns>
    Task<List<NotificationTaskEntity>> GetByNotificationSettingIdAsync(long notificationSettingId);

    /// <summary>
    /// 根据任务状态获取任务列表
    /// </summary>
    /// <param name="taskStatus">任务状态</param>
    /// <returns></returns>
    Task<List<NotificationTaskEntity>> GetByTaskStatusAsync(NotificationStatusEnum taskStatus);

    /// <summary>
    /// 根据启用状态获取任务列表
    /// </summary>
    /// <param name="enabled">是否启用</param>
    /// <returns></returns>
    Task<List<NotificationTaskEntity>> GetByEnabledAsync(bool enabled);

    /// <summary>
    /// 根据业务类型和业务ID获取任务列表
    /// </summary>
    /// <param name="businessType">业务类型</param>
    /// <param name="businessId">业务ID</param>
    /// <returns></returns>
    Task<List<NotificationTaskEntity>> GetByBusinessAsync(string businessType, long? businessId = null);

    /// <summary>
    /// 获取待执行的任务列表
    /// </summary>
    /// <param name="currentTime">当前时间</param>
    /// <returns></returns>
    Task<List<NotificationTaskEntity>> GetPendingTasksAsync(DateTime currentTime);

    /// <summary>
    /// 获取需要执行的定时任务列表（基于Cron表达式）
    /// </summary>
    /// <param name="currentTime">当前时间</param>
    /// <returns></returns>
    Task<List<NotificationTaskEntity>> GetCronTasksForExecutionAsync(DateTime currentTime);

    /// <summary>
    /// 更新任务执行状态
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <param name="taskStatus">任务状态</param>
    /// <param name="executionResult">执行结果</param>
    /// <param name="errorMessage">错误信息</param>
    /// <returns></returns>
    Task UpdateExecutionStatusAsync(long taskId, NotificationStatusEnum taskStatus, string executionResult = "", string errorMessage = "");

    /// <summary>
    /// 更新任务下次执行时间
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <param name="nextExecutionTime">下次执行时间</param>
    /// <returns></returns>
    Task UpdateNextExecutionTimeAsync(long taskId, DateTime? nextExecutionTime);

    /// <summary>
    /// 增加任务执行次数
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <param name="isSuccess">是否执行成功</param>
    /// <returns></returns>
    Task IncrementExecutionCountAsync(long taskId, bool isSuccess);

    /// <summary>
    /// 检查任务是否应该停止执行
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <returns></returns>
    Task<bool> ShouldStopExecutionAsync(long taskId);

    /// <summary>
    /// 获取过期的任务列表
    /// </summary>
    /// <param name="expiredBefore">过期时间</param>
    /// <returns></returns>
    Task<List<NotificationTaskEntity>> GetExpiredTasksAsync(DateTime expiredBefore);

    /// <summary>
    /// 清理过期的任务
    /// </summary>
    /// <param name="expiredBefore">过期时间</param>
    /// <returns></returns>
    Task<int> CleanupExpiredTasksAsync(DateTime expiredBefore);
}