using Hotline.Caching.Interfaces;
using Hotline.FlowEngine.Workflows;
using Hotline.Share.Dtos.FlowEngine.Workflow;
using Hotline.Share.Enums.FlowEngine;
using Microsoft.AspNetCore.Http;
using Shouldly;
using XF.Domain.Exceptions;

namespace Hotline.Tests.Domain.Workflow;

public class WorkflowDomainServiceTest
{
    private readonly IWorkflowDomainService _workflowDomainService;
    private readonly IWfModuleCacheManager _wfModuleCacheManager;

    public WorkflowDomainServiceTest(
        IWorkflowDomainService workflowDomainService,
        IWfModuleCacheManager wfModuleCacheManager)
    {
        _workflowDomainService = workflowDomainService;
        _wfModuleCacheManager = wfModuleCacheManager;
    }
    
    public async Task StartAsyncTest(string definitionModuleCode)
    {
        var wfModule = await _wfModuleCacheManager.GetWorkflowModuleAsync(definitionModuleCode, default);
        if (wfModule == null)
            throw UserFriendlyException.SameMessage("无效流程模块编码");
        if (wfModule.Definition is null)
            throw new UserFriendlyException($"{definitionModuleCode} 未配置流程模板", "未配置流程模板");

        var definition = wfModule.Definition;
        if (definition == null)
            throw new UserFriendlyException("无效模板编码");
        if (definition.Status is not EDefinitionStatus.Enable)
            throw new UserFriendlyException("该模板不可用");

        //如果发起会签需检查是否支持发起会签
        var startStepDefine = definition.FindStartStepDefine();
        var nextStep = definition.FindStepDefine(startStepDefine.NextSteps.First().Code);

        //StartWorkflowDto dto, string externalId,DateTime? expiredTime = null,
        var dto = new StartWorkflowDto
        {
            DefinitionModuleCode = definitionModuleCode,
            Title = "测试流程开启",
            NextStepCode = nextStep.Code,
            NextStepName = nextStep.Name,
            
        };
        var orderId = Ulid.NewUlid().ToString();
        var expiredTime = DateTime.Now;
        var (workflow, startStep) = await _workflowDomainService.StartAsync(dto, orderId, expiredTime, cancellationToken: default);
        workflow.ExternalId.ShouldBe(orderId);
        startStep.StepExpiredTime.ShouldBe(expiredTime);
        startStep.FlowAssignType.ShouldNotBe(null);
        startStep.Status.ShouldBe(EWorkflowStepStatus.WaitForAccept);
    }

    public async Task HandleAsyncTest(string definitionModuleCode)
    {
        
    }
}