using e_suite.Database.Audit;
using e_suite.Database.Core.Extensions;
using e_suite.Database.Core.Models;
using e_suite.Database.Core.Tables.Workflow;
using e_suite.Utilities.Pagination;
using eSuite.Core.Miscellaneous;

namespace e_suite.API.Common;

public class GetWorkflowTemplateVersion : IGeneralId
{
    public GetWorkflowTemplateVersion()
    {

    }

    public GetWorkflowTemplateVersion(WorkflowVersion workflowVersion)
    {
        Id = workflowVersion.Id;
        Guid = workflowVersion.Guid;
        Deleted = workflowVersion.Deleted;
        ActivityNameTemplate = workflowVersion.ActivityNameTemplate;
        Description = workflowVersion.Description;
        DomainId = workflowVersion.Domain.ToGeneralIdRef()!;
        LastUpdated = workflowVersion.LastUpdated;
        Version = workflowVersion.Version;
        WorkflowId = workflowVersion.Workflow.ToGeneralIdRef()!;
        WorkflowName = workflowVersion.Workflow.Name;
        Tasks = workflowVersion.Tasks;
    }

    public List<TaskDefinition> Tasks { get; set; } = [];

    public string WorkflowName { get; set; } = string.Empty;

    public GeneralIdRef WorkflowId { get; set; } = null!;

    public long Version { get; set; }

    public DateTimeOffset LastUpdated { get; set; }

    public GeneralIdRef DomainId { get; set; } = null!;

    public string Description { get; set; } = string.Empty;

    public string ActivityNameTemplate { get; set; } = string.Empty;

    public bool Deleted { get; set; }

    public long Id { get; set; }
    public Guid Guid { get; set; }
}

public class PatchWorkflowTemplateVersion
{
    
    public string? WorkflowName { get; set; }
    
    public GeneralIdRef? DomainId { get; set; }

    public string? Description { get; set; }

    public string? ActivityNameTemplate { get; set; }
}

public class CreateWorkflowTemplateVersion
{
    public List<TaskDefinition> Tasks { get; set; } = [];
    
    public required string WorkflowName { get; set; }
    
    public required GeneralIdRef DomainId { get; set; }

    public string Description { get; set; } = String.Empty;

    public required string ActivityNameTemplate { get; set; } = String.Empty;
}

public class TaskMetadata
{
    public string TaskType { get; set; }
    public string DisplayName { get; set; }
    public List<string> Capabilities { get; set; } = [];
}

public interface IWorkflowTemplateManager
{
    Task<PaginatedData<GetWorkflowTemplate>> GetWorkflowTemplatesAsync(Paging paging, CancellationToken cancellationToken);

    Task<PaginatedData<GetWorkflowTemplateVersion>> GetWorkflowTemplateVersionsAsync(Paging paging, CancellationToken cancellationToken);
    Task<GetWorkflowTemplateVersion?> GetWorkflowTemplateAsync(GeneralIdRef generalIdRef, CancellationToken cancellationToken);
    Task EditTemplateVersion(AuditUserDetails auditUserDetails, GetWorkflowTemplateVersion template, CancellationToken cancellationToken);
    Task PatchTemplateVersion(AuditUserDetails auditUserDetails, IGeneralIdRef templateId, PatchWorkflowTemplateVersion patchTemplate, CancellationToken cancellationToken);
    Task PostTemplateVersion(AuditUserDetails auditUserDetails, CreateWorkflowTemplateVersion template, CancellationToken cancellationToken);
    Task DeleteTemplateVersion(AuditUserDetails auditUserDetails, IGeneralIdRef templateId, CancellationToken cancellationToken);
    Task<List<TaskMetadata>> GetAllowedTaskMetadataList(string taskType, CancellationToken cancellationToken);
}