using AipGateway.AIP;
using AipGateway.API.Application.Modules;
using AipGateway.API.Domain.IRepositories.IGenericRepositories;
using AipGateway.API.Domain.Models;
using AipGateway.API.Domain.Models.Request;
using AipGateway.API.Domain.Models.Response;
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/file")]
    [Produces("application/json")]
    public class ApiFileController : BaseModule
    {
        private readonly ILogger<ApiFileController> _log;
        private readonly IApiFileService _apiFileService;
        public ApiFileController(
            ILogger<ApiFileController> logger, 
            IUnitOfWork unitOfWork, 
            IWebHostEnvironment webHostEnvironment,
            IApiFileService apiFileService)
            : base(unitOfWork, webHostEnvironment)
        {
            _log = logger;
            _apiFileService = apiFileService;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: ApiFileController");
        }

        [HttpPost("info")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseInfo>))]
        public async Task<IResult> GetInfo([FromBody] RequestFile req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_INFO, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiFileService.GetInfo(req);
                var data = new SuccessResponseModel<ResponseInfo>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("set-label")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseFile>))]
        public async Task<IResult> SetLabel([FromBody] RequestFileSet req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_SET_LABEL, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiFileService.SetLabel(req);
                var data = new SuccessResponseModel<ResponseFile>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("delete-label")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseFile>))]
        public async Task<IResult> DelLabel([FromBody] RequestFileDel req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_DELETE_LABEL, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiFileService.DelLabel(req);
                var data = new SuccessResponseModel<ResponseFile>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("set-protection")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseFile>))]
        public async Task<IResult> SetProtection([FromBody] RequestFileSet req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_SET_PROTECTION, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiFileService.SetProtection(req);
                var data = new SuccessResponseModel<ResponseFile>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("delete-protection")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseFile>))]
        public async Task<IResult> DelProtection([FromBody] RequestFileDel req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_DELETE_PROTECTIN, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiFileService.DelProtection(req);
                var data = new SuccessResponseModel<ResponseFile>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }


        [HttpPost("set-label-protection")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseFile>))]
        public async Task<IResult> SetLabelProtection([FromBody] RequestFileAllSet req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_SET_LABEL_PROTECTION, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiFileService.SetLabelProtection(req);
                var data = new SuccessResponseModel<ResponseFile>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

        [HttpPost("delete-label-protection")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ResponseFile>))]
        public async Task<IResult> DelLabelProtection([FromBody] RequestFileDel req)
        {
            GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_FILE_DELETE_LABEL_PROTECTION, req.apiKey);
            req.apiGuid = GlobalConstants.GetApiGuid(HttpContext);
            HttpContext.Items[GlobalConstants.API_REQUEST] = (RequestBase)req;
            return await CreateResponseAsync(async () =>
            {
                int authError = ContainerService.ValidationApiAuthorization(HttpContext, req.apiKey);
                if (authError != 0)
                {
                    throw ContainerService.CreateValidationApiAuthorizationAsync(HttpContext, authError);
                }
                var response = await _apiFileService.DelLabelProtection(req);
                var data = new SuccessResponseModel<ResponseFile>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                };
                var result = Results.Ok(data);
                HttpContext.Items.Add(GlobalConstants.API_RESULT, (ResponseBase)data.Result);
                return result;
            });
        }

    }
}
