using AipGateway.API.Application.Modules;
using AipGateway.API.Domain.Entities;
using AipGateway.API.Domain.IRepositories.IGenericRepositories;
using AipGateway.API.Domain.Models;
using AipGateway.API.Extensions;
using AipGateway.API.Services;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace AipGateway.API.Controllers
{
    [ApiController]
    [Route("v1/aip-api/db")]
    [Produces("application/json")]
    public class ApiDbController : BaseModule
    {
        private readonly ILogger<ApiDbController> _log;
        private readonly IApiDbService _apiDbService;
        public ApiDbController(
            ILogger<ApiDbController> logger, 
            IUnitOfWork unitOfWork, 
            IWebHostEnvironment webHostEnvironment,
            IApiDbService apiDbService)
            : base(unitOfWork, webHostEnvironment)
        {
            _log = logger;
            _apiDbService = apiDbService;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: ApiDbController");
        }

        [HttpPost("reload")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ApiResponseDto>))]
        public async Task<IResult> ReloadDatabase(string apiKey)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_RELOAD, apiKey);
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiDbService.ReloadDatabase();
                var result = Results.Ok(new SuccessResponseModel<ApiResponseDto>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                });
                HttpContext.Items[GlobalConstants.API_RESULT] = result;
                return result;
            });
        }

        [HttpGet("linked-systems")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<List<LinkedSystemDto>>))]
        public async Task<IResult> GetLinkedSystems(string apiKey)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_SYSTEMS, apiKey);
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiDbService.GetLinkedSystems();
                var result = Results.Ok(new SuccessResponseModel<List<LinkedSystemDto>>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                });
                HttpContext.Items[GlobalConstants.API_RESULT] = result;
                return result;
            });
        }

        [HttpGet("linked-servers")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<List<LinkedServerDto>>))]
        public async Task<IResult> GetLinkedServers(string apiKey)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_SERVERS, apiKey);
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiDbService.GetLinkedServers();
                var result = Results.Ok(new SuccessResponseModel<List<LinkedServerDto>>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                });
                HttpContext.Items[GlobalConstants.API_RESULT] = result;
                return result;
            });
        }

        [HttpGet("linked-api-keys")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<List<LinkedApiKeyDto>>))]
        public async Task<IResult> GetLinkedApiKeys(string apiKey)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_API_KEYS, apiKey);
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiDbService.GetLinkedApiKeys();
                var result = Results.Ok(new SuccessResponseModel<List<LinkedApiKeyDto>>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                });
                HttpContext.Items[GlobalConstants.API_RESULT] = result;
                return result;
            });
        }

        [HttpGet("linked-decrypt-keys")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<List<LinkedDecryptKeyDto>>))]
        public async Task<IResult> GetLinkedDecryptKeys(string apiKey)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DB_LINKED_DECRYPT_KEYS, apiKey);
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiDbService.GetLinkedDecryptKeys();
                var result = Results.Ok(new SuccessResponseModel<List<LinkedDecryptKeyDto>>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                });
                HttpContext.Items[GlobalConstants.API_RESULT] = result;
                return result;
            });
        }

    }
}
