using AipGateway.AIP;
using AipGateway.API.Application.Modules;
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/aip")]
    [Produces("application/json")]
    public class ApiAipController : BaseModule
    {
        private readonly ILogger<ApiAipController> _log;
        private readonly IApiAipService _apiAipService;
        public ApiAipController(
            ILogger<ApiAipController> logger, 
            IUnitOfWork unitOfWork, 
            IWebHostEnvironment webHostEnvironment,
            IApiAipService apiAipService)
            : base(unitOfWork, webHostEnvironment)
        {
            _log = logger;
            _apiAipService = apiAipService;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: ApiAipController");
        }

        [HttpPost("download")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ApiResponseDto>))]
        public async Task<IResult> DownloadAipInfo(string apiKey)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_DOWNLOAD, apiKey);
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiAipService.DownloadAipInfo();
                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("labels")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<List<AipLabel>>))]
        public async Task<IResult> GetAipFileLabels(string apiKey)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_LABELS, apiKey);
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiAipService.GetLabels();
                var result = Results.Ok(new SuccessResponseModel<List<AipLabel>>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                });
                HttpContext.Items[GlobalConstants.API_RESULT] = result;
                return result;
            });
        }

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

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

    }
}
