using AipGateway.API.Application.Modules;
using AipGateway.API.Domain.Entities;
using AipGateway.API.Domain.IRepositories.IGenericRepositories;
using AipGateway.API.Domain.Models;
using AutoMapper;

namespace AipGateway.API.Services.impl
{
    public class ApiDbService : IApiDbService
    {
        private readonly ILogger<ApiFileService> _log;
        private readonly IServiceProvider _provider;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IMapper _mapper;

        public ApiDbService(ILogger<ApiFileService> logger, IServiceProvider provider, IUnitOfWork unitOfWork, IMapper mapper)
        {
            _log = logger;
            _provider = provider;
            _unitOfWork = unitOfWork;
            _mapper = mapper;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: ApiDbService");
        }

        public async Task<ApiResponseDto> ReloadDatabase()
        {
            try
            {
                //AipFileApiService? aipFileApiService = ContainerService.provider.GetService<AipFileApiService>();
                AipFileApiService? aipFileApiService = _provider.GetService<AipFileApiService>();
                if (aipFileApiService == null)
                {
                    return new ApiResponseDto
                    {
                        errorCode = 1,
                        errorMessage = "AIP File 관리 서비스를 찾을 수 없습니다.",
                        effectCount = 0,
                    };
                }

                var task = Task.Run(() =>
                {
                    aipFileApiService.LoadLinkedApiKeys();
                    aipFileApiService.LoadLinkedDecryptKeys();
                    return ContainerService.apiKeyMap.Count + ContainerService.decryptKeyMap.Count;
                });
                int result = await task;
                return new ApiResponseDto
                {
                    errorCode = 0,
                    errorMessage = GlobalConstants.API_RESULT_SUCCESS,
                    effectCount = result,
                };
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<List<LinkedSystemDto>> GetLinkedSystems()
        {
            try
            {
                var result = await _unitOfWork.LinkedSystemRepository.GetAllAsync();
                return _mapper.Map<List<LinkedSystemDto>>(result);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<List<LinkedServerDto>> GetLinkedServers()
        {
            try
            {
                var result = await _unitOfWork.LinkedServerRepository.GetAllAsync();
                return _mapper.Map<List<LinkedServerDto>>(result);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<List<LinkedApiKeyDto>> GetLinkedApiKeys()
        {
            try
            {
                var result = await _unitOfWork.LinkedApiKeyRepository.GetAllAsync();
                return _mapper.Map<List<LinkedApiKeyDto>>(result);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public async Task<List<LinkedDecryptKeyDto>> GetLinkedDecryptKeys()
        {
            try
            {
                var result = await _unitOfWork.LinkedDecryptKeyRepository.GetAllAsync();
                return _mapper.Map<List<LinkedDecryptKeyDto>>(result);
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}
