using e_suite.API.Common.repository;
using e_suite.Database.Audit;
using e_suite.Database.Core;
using e_suite.Database.Core.Extensions;
using e_suite.Database.Core.Tables.UserManager;
using eSuite.Core.Miscellaneous;

namespace e_suite.Modules.SSOManager.repository;

public class SsoManagerRepository : RepositoryBase, ISsoManagerRepository
{
    public SsoManagerRepository(IEsuiteDatabaseDbContext databaseDbContext) : base(databaseDbContext)
    {
    }

    public IQueryable<SsoProvider> GetSsoProviders()
    {
        return DatabaseDbContext.SsoProviders;
    }

    public async Task<SsoProvider?> GetSsoProviderAsync(IGeneralIdRef generalIdRef, CancellationToken cancellationToken)
    {
        return await DatabaseDbContext.SsoProviders.FindByGeneralIdRefAsync(generalIdRef, cancellationToken);
    }

    public async Task CreateNewSsoProviderAsync(
        AuditUserDetails auditUserDetails,
        SsoProvider newSsoProvider,
        CancellationToken cancellationToken
    )
    {
        await DatabaseDbContext.SsoProviders.AddAsync(newSsoProvider, cancellationToken);
        await DatabaseDbContext.SaveChangesAsync(auditUserDetails, cancellationToken);
    }

    public async Task EditNewSsoProviderAsync(
        AuditUserDetails auditUserDetails,
        SsoProvider ssoProvider,
        CancellationToken cancellationToken
    )
    {
        await DatabaseDbContext.SaveChangesAsync(auditUserDetails, cancellationToken);
    }
}