using System;
using Common;
using Common.Constant;
using Common.Http;
using Esim.Apis.Business;
using Esim.Apis.DTO.Content;
using Database.Database;
using Microsoft.AspNetCore.Mvc;

namespace RevoSystem.Apis.Controllers
{
    [ApiController]
    public class ContentController : Controller
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
            typeof(ContentController)
        );

        IContentBusiness contentBusiness;

        public ContentController(IContentBusiness contentBusiness)
        {
            this.contentBusiness = contentBusiness;
        }

        /// <summary>
        /// Load banners
        /// POST /apis/content/banner
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.BannerLoadUrl)]
        public async Task<IActionResult> BannerLoad([FromBody] BannerLoadReq request)
        {
            return await contentBusiness.BannerLoad(HttpContext.Request, request);
        }

        /// <summary>
        /// Load customer reviews
        /// POST /apis/content/review
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.CustomerReviewLoadUrl)]
        public async Task<IActionResult> CustomerReviewLoad([FromBody] CustomerReviewLoadReq request)
        {
            return await contentBusiness.CustomerReviewLoad(HttpContext.Request, request);
        }

        /// <summary>
        /// Create customer review
        /// POST /apis/content/review/create
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.CustomerReviewCreateUrl)]
        public async Task<IActionResult> CustomerReviewCreate([FromBody] CustomerReviewCreateReq request)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values
                    .SelectMany(v => v.Errors)
                    .Select(e => e.ErrorMessage)
                    .ToList();
                
                string lang = Common.Logic.CommonLogic.GetLanguage(HttpContext.Request, request?.lang);
                return DotnetLib.Http.HttpResponse.BuildResponse(
                    log, HttpContext.Request.Path, Newtonsoft.Json.JsonConvert.SerializeObject(request),
                    CommonErrorCode.RequiredFieldMissing,
                    string.Join("; ", errors),
                    new { }
                );
            }
            
            return await contentBusiness.CustomerReviewCreate(HttpContext.Request, request);
        }

        /// <summary>
        /// Load FAQ categories
        /// POST /apis/content/faq-category
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.FaqCategoryLoadUrl)]
        public async Task<IActionResult> FaqCategoryLoad([FromBody] FaqCategoryLoadReq request)
        {
            return await contentBusiness.FaqCategoryLoad(HttpContext.Request, request);
        }

        /// <summary>
        /// Load FAQs
        /// POST /apis/content/faq
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.FaqLoadUrl)]
        public async Task<IActionResult> FaqLoad([FromBody] FaqLoadReq request)
        {
            return await contentBusiness.FaqLoad(HttpContext.Request, request);
        }

        /// <summary>
        /// Load device eSIM compatibility list
        /// POST /apis/content/device-compatibility
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.DeviceCompatibilityLoadUrl)]
        public async Task<IActionResult> DeviceCompatibilityLoad([FromBody] DeviceCompatibilityReq request)
        {
            return await contentBusiness.DeviceCompatibilityLoad(HttpContext.Request, request);
        }

        /// <summary>
        /// Get device brands and categories list (for filters)
        /// GET /apis/content/device-metadata
        /// </summary>
        [HttpGet]
        [Route(ApiUrlConstant.DeviceMetadataUrl)]
        public async Task<IActionResult> GetDeviceBrandsAndCategories()
        {
            return await contentBusiness.GetDeviceBrandsAndCategories(HttpContext.Request);
        }

    }
}
