using AipGateway.API.Domain.Entities;
using AipGateway.API.Domain.IRepositories.IGenericRepositories;
using AipGateway.API.Domain.Models;
using Microsoft.EntityFrameworkCore;
using AipGateway.API.Infrastructure.Persistence;
using Microsoft.Extensions.Logging;

namespace AipGateway.API.Infrastructure.DataAccess
{
    public class UnitOfWorkRepository : IUnitOfWork
    {
        private readonly ILogger<UnitOfWorkRepository> _log;
        private readonly ApplicationDbContext _dbContext;
        private readonly ConnectionInfo _connectionInfo;

        private IGenericRepository<TbLinkedSystem>? _linkedSystemRepository;
        private IGenericRepository<TbLinkedServer>? _linkedServerRepository;
        private IGenericRepository<TbLinkedApiKey>? _linkedApiKeyRepository;
        private IGenericRepository<TbLinkedDecryptKey>? _linkedDescryptRepository;


        private IGenericRepository<TbAipLabel>? _aipAipLabelRepository;
        private IGenericRepository<TbAipPolicy>? _aipAipPolicyRepository;
        private IGenericRepository<TbAipProtection>? _aipAipProtectionRepository;

        private IGenericRepository<TbAipApiCallLog>? _aipApiCallLogRepository;
        private IGenericRepository<TbAipFileJobLog>? _aipFileJobLogRepository;

        public UnitOfWorkRepository(ILogger<UnitOfWorkRepository> logger, ApplicationDbContext appDbContext, ConnectionInfo connectionInfo)
        {
            _log = logger;
            _dbContext = appDbContext;
            _connectionInfo = connectionInfo;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: UnitOfWorkRepository");
        }

        public IGenericRepository<TbLinkedSystem> LinkedSystemRepository { 
            get
            {
                if (_linkedSystemRepository == null)
                    _linkedSystemRepository = new GenericRepository<TbLinkedSystem>(_dbContext, _connectionInfo);
                return _linkedSystemRepository;
            }
        }
        public IGenericRepository<TbLinkedServer> LinkedServerRepository {
            get 
            {
                if (_linkedServerRepository == null)
                    _linkedServerRepository = new GenericRepository<TbLinkedServer>(_dbContext, _connectionInfo);
                return _linkedServerRepository;
            } 
        }
        public IGenericRepository<TbLinkedApiKey> LinkedApiKeyRepository
        {
            get
            {
                if (_linkedApiKeyRepository == null)
                    _linkedApiKeyRepository = new GenericRepository<TbLinkedApiKey>(_dbContext, _connectionInfo);
                return _linkedApiKeyRepository;
            }
        }
        public IGenericRepository<TbLinkedDecryptKey> LinkedDecryptKeyRepository
        {
            get
            {
                if (_linkedDescryptRepository == null)
                    _linkedDescryptRepository = new GenericRepository<TbLinkedDecryptKey>(_dbContext, _connectionInfo);
                return _linkedDescryptRepository;
            }
        }


        public IGenericRepository<TbAipLabel> AipLabelRepository
        {
            get
            {
                if (_aipAipLabelRepository == null)
                    _aipAipLabelRepository = new GenericRepository<TbAipLabel>(_dbContext, _connectionInfo);
                return _aipAipLabelRepository;
            }
        }
        public IGenericRepository<TbAipPolicy> AipPolicyRepository
        {
            get
            {
                if (_aipAipPolicyRepository == null)
                    _aipAipPolicyRepository = new GenericRepository<TbAipPolicy>(_dbContext, _connectionInfo);
                return _aipAipPolicyRepository;
            }
        }
        public IGenericRepository<TbAipProtection> AipProtectionRepository
        {
            get
            {
                if (_aipAipProtectionRepository == null)
                    _aipAipProtectionRepository = new GenericRepository<TbAipProtection>(_dbContext, _connectionInfo);
                return _aipAipProtectionRepository;
            }
        }


        public IGenericRepository<TbAipApiCallLog> AipApiCallLogRepository
        {
            get
            {
                if (_aipApiCallLogRepository == null)
                    _aipApiCallLogRepository = new GenericRepository<TbAipApiCallLog>(_dbContext, _connectionInfo);
                return _aipApiCallLogRepository;
            }
        }

        public IGenericRepository<TbAipFileJobLog> AipFileJobLogRepository
        {
            get
            {
                if (_aipFileJobLogRepository == null)
                    _aipFileJobLogRepository = new GenericRepository<TbAipFileJobLog>(_dbContext, _connectionInfo);
                return _aipFileJobLogRepository;
            }
        }

        public int Complete()
        {
            return _dbContext.SaveChanges();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing) _dbContext.Dispose();
        }
    }
}
