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

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

        ModelContext dbContext;
        IConfiguration configuration;

        IUserBusiness userBusiness;

        public UserController(
            IConfiguration _configuration,
            IUserBusiness userBusiness,
            ModelContext dbContext
        )
        {
            configuration = _configuration;
            this.userBusiness = userBusiness;
            this.dbContext = dbContext;
        }

        private string GetParameter(string key)
        {
            return configuration.GetSection(key).Value ?? "";
        }

        /// <summary>
        /// Request OTP via email
        /// POST /apis/auth/request-otp
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.RequestOtpUrl)]
        public async Task<IActionResult> RequestOtp([FromBody] RequestOtpReq request)
        {
            return await userBusiness.RequestOtp(HttpContext.Request, request);
        }

        /// <summary>
        /// Resend OTP via email
        /// POST /apis/auth/resend-otp
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.ResendOtpUrl)]
        public async Task<IActionResult> ResendOtp([FromBody] RequestOtpReq request)
        {
            return await userBusiness.ResendOtp(HttpContext.Request, request);
        }

        /// <summary>
        /// Verify OTP and complete login
        /// POST /apis/auth/verify-otp
        /// </summary>
        [HttpPost]
        [Route(ApiUrlConstant.VerifyOtpUrl)]
        public async Task<IActionResult> VerifyOtp([FromBody] VerifyOtpReq request)
        {
            return await userBusiness.VerifyOtp(HttpContext.Request, request);
        }

        /// <summary>
        /// Get Google Login URL
        /// POST /apis/auth/google-login
        /// </summary>
        /// <param name="request">Contains optional 'lang' parameter for response message language</param>
        /// <returns>Returns JSON object with 'url' property containing the Google OAuth2 authorization URL</returns>
        [ProducesResponseType(typeof(object), 200)]
        [HttpPost]
        [Route(ApiUrlConstant.GoogleLoginUrl)]
        public async Task<IActionResult> GoogleLogin([FromBody] GoogleLoginReq request)
        {
            return await userBusiness.GoogleLogin(
                HttpContext.Request,
                request ?? new GoogleLoginReq()
            );
        }

        /// <summary>
        /// Google OAuth Callback (Exchange code for token)
        /// POST /apis/auth/google-callback
        /// </summary>
        /// <param name="request">Contains the authorization 'code' (required) and 'redirectUri' (optional)</param>
        /// <returns>Returns user profile and JWT tokens (accessToken, refreshToken)</returns>
        [ProducesResponseType(typeof(object), 200)]
        [HttpPost]
        [Route(ApiUrlConstant.GoogleCallbackUrl)]
        public async Task<IActionResult> GoogleCallback([FromBody] GoogleCallbackReq request)
        {
            return await userBusiness.GoogleCallback(HttpContext.Request, request);
        }

        /// <summary>
        /// Google OAuth Callback (Exchange code for token)
        /// POST /apis/auth/google-callback
        /// </summary>
        /// <param name="request">Contains the authorization 'code' (required) and 'redirectUri' (optional)</param>
        /// <returns>Returns user profile and JWT tokens (accessToken, refreshToken)</returns>
        [ProducesResponseType(typeof(object), 200)]
        [HttpPost]
        [Route(ApiUrlConstant.PartnerLoginUrl)]
        public async Task<IActionResult> PartnerLogin([FromBody] PartnerLoginReq request)
        {
            return await userBusiness.PartnerLogin(HttpContext.Request, request);
        }
    }
}
