using e_suite.API.Common.repository;
using e_suite.Database.Audit;
using e_suite.Database.Core;
using e_suite.Database.Core.Tables.Contacts;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

namespace e_suite.Modules.WorkflowTemplatesManager.Repository;

public class WorkflowTemplateRepository : RepositoryBase, IWorkflowTemplateRepository
{
    public WorkflowTemplateRepository(IEsuiteDatabaseDbContext databaseDbContext) : base(databaseDbContext)
    {
    }
    
    public IQueryable<e_suite.Database.Core.Tables.Workflow.Workflow> GetWorkflows()
    {
        return DatabaseDbContext.Workflows
            .Include( x => x.Versions);
    }

    public IQueryable<e_suite.Database.Core.Tables.Workflow.WorkflowVersion> GetWorkflowVersions()
    {   
        return DatabaseDbContext.WorkflowVersions
            .Include( x => x.Domain)
            .Include( x => x.Workflow);
    }
    
    public async Task EditWorkflowVersionAsync(AuditUserDetails auditUserDetails, e_suite.Database.Core.Tables.Workflow.WorkflowVersion workflowVersion, CancellationToken cancellationToken)
    {
        await DatabaseDbContext.SaveChangesAsync(auditUserDetails, cancellationToken);
    }
    
    public async Task AddWorkflow(AuditUserDetails auditUserDetails, Database.Core.Tables.Workflow.Workflow workflow, CancellationToken cancellationToken)
    {
        DatabaseDbContext.Workflows.Add(workflow);
        await DatabaseDbContext.SaveChangesAsync(auditUserDetails, cancellationToken);
    }

    public async Task AddWorkflowVersion(
        AuditUserDetails auditUserDetails,
        Database.Core.Tables.Workflow.WorkflowVersion workflowVersion,
        CancellationToken cancellationToken
    )
    {
        DatabaseDbContext.WorkflowVersions.Add(workflowVersion);
        await DatabaseDbContext.SaveChangesAsync(auditUserDetails, cancellationToken);
    }

    public async Task EditWorkflowAsync(AuditUserDetails auditUserDetails, Database.Core.Tables.Workflow.Workflow workflow, CancellationToken cancellationToken)
    {
        await DatabaseDbContext.SaveChangesAsync(auditUserDetails, cancellationToken);
    }
}