using DocumentFormat.OpenXml.Spreadsheet;

namespace IRaCIS.Core.Application.Helper;


public static class CacheKeys
{
    //项目缓存
    public static string Trial(string trialIdStr) => $"TrialId:{trialIdStr}";

    public static string TrialVisitTaskMaxCode(Guid trialId) => $"TrialVisitTaskMaxCode:{trialId}";

    //检查编号递增锁
    public static string TrialStudyMaxCode(Guid trialId) => $"TrialStudyMaxCode:{trialId}";

    public static string TrialStudyUidUploading(Guid trialId, string studyUid) => $"TrialStudyUid:{trialId}_{studyUid}";
    //CRC上传影像提交锁key
    public static string TrialStudyUidDBLock(Guid trialId, string studyUid) => $"TrialStudyUidDBLock:{trialId}_{studyUid}";

    public static string TrialTaskStudyUidUploading(Guid trialId, Guid visiTaskId, string studyUid) => $"TrialStudyUid:{trialId}_{visiTaskId}_{studyUid}";
    //影像后处理上传提交锁key
    public static string TrialTaskStudyUidDBLock(Guid trialId, Guid visiTaskId, string studyUid) => $"TrialTaskStudyUidDBLock:{trialId}_{visiTaskId}_{studyUid}";
    //系统匿名化
    public static string SystemAnonymization => $"SystemAnonymization";
    //前端国际化
    public static string FrontInternational => $"FrontInternationalList";

    //登录挤账号
    public static string UserToken(Guid userId) => $"UserToken:{userId}";

    //超时没请求接口自动退出
    public static string UserAutoLoginOut(Guid userId) => $"UserAutoLoginOut:{userId}";


    public static string UserDisable(Guid userId) => $"UserDisable:{userId}";

    public static string UserRoleDisable(Guid userRoleId) => $"UserRoleDisable:{userRoleId}";

    /// <summary>
    /// 用户登录错误 限制登录
    /// </summary>
    /// <param name="userName"></param>
    /// <returns></returns>
    public static string UserLoginError(string userName) => $"login-failures:{userName}";

    /// <summary>
    /// 跳过阅片
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public static string SkipReadingCacheKey(Guid userId) => $"{userId}SkipReadingCache";


    /// <summary>
    /// 开始阅片时间
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public static string StartReadingTimeKey(Guid userId) => $"{userId}StartReadingTime";

    /// <summary>
    /// 开始休息时间
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public static string StartRestTime(Guid userId) => $"{userId}StartRestTime";

    //每个用户 每个浏览器独立时间
    public static string UserMFAVerifyPass(Guid userId,string browserFingerprint) => $"UserMFAVerifyPass:{userId}:{browserFingerprint}";

    public static string UserMFATag(Guid userId) => $"UserMFAVerifyPass:{userId}";

    public static string TrialDataStoreType(Guid trialId) => $"TrialDataStoreType:{trialId}";


    public static string UserQCSkipTask(Guid userId) => $"UserSKipQCTask:{userId}";
}

public static class CacheHelper
{
    public static async Task<string?> GetTrialStatusAsync(Guid trialId, IRepository<Trial> _trialRepository)
    {
        var statusStr = await _trialRepository.Where(t => t.Id == trialId, ignoreQueryFilters: true).Select(t => t.TrialStatusStr).FirstOrDefaultAsync();

        return statusStr;
    }




    public static async Task<List<SystemAnonymization>> GetSystemAnonymizationListAsync(IRepository<SystemAnonymization> _systemAnonymizationRepository)
    {
        var list = await _systemAnonymizationRepository.Where(t => t.IsEnable).ToListAsync();

        return list;
    }
}
