using e_suite.Database.Core;
using e_suite.Database.Core.Tables.Miscellaneous;

namespace e_suite.Service.EFlowSync.Repository;

public enum externalSystems
{
    eflow = 1
}

public class EFlowSyncRepository : RepositoryBase, IEFlowSyncRepository
{
    public EFlowSyncRepository(IEsuiteDatabaseDbContext esuiteDatabaseDbContext) : base(esuiteDatabaseDbContext)
    {
    }

    //public async Task<IEnumerable<PrintSpecificationCategoryInformation>> GetPrintSpecListForDomain(Guid domainGuid)
    //{
    //    var parameters = new { domainGuid = domainGuid.ToString() };
    //    var getPrintSpecificationCategoryInformation = await File.ReadAllTextAsync("Resources/Scripts/GetPrintSpecificationCategoryInformation.sql");
    //    return await DbLoggerCategory.Database.Connection.QueryAsync<PrintSpecificationCategoryInformation>(getPrintSpecificationCategoryInformation, parameters);
    //}
    public Task<string?> GetEFlowExternalId(string entityName, string primaryKeyJson)
    {
        return Task.FromResult(DatabaseDbContext.ExternalKeys
            .Where(x => x.ExternalSystemId == (long)externalSystems.eflow
                        && x.EntityName == entityName
                        && x.PrimaryKey == primaryKeyJson).Select(x => x.ExternalId).SingleOrDefault());
    }

    public async Task AddEflowExternalId(string entityName, string primaryKeyJson, string childId, CancellationToken cancellationToken)
    {
        var externalKey = new ExternalKey
        {
            ExternalSystemId = (long)externalSystems.eflow,
            EntityName = entityName,
            PrimaryKey = primaryKeyJson,
            ExternalId = childId
        };

        await DatabaseDbContext.ExternalKeys.AddAsync(externalKey, cancellationToken);
        await DatabaseDbContext.NoAuditSaveChangesAsync(cancellationToken);
    }
}