using CloudBuilder.Core.DatabaseAccessor;
using CloudBuilder.Core.Policy;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Transactions;
using CloudBuilder.Core.Service;
using CloudBuilder.Security.Service;
            using CloudBuilder.Core.DatabaseAccessor.Entity;
            using Microsoft.AspNetCore.Http;
            using CloudBuilder.Core.Policy;
            using CloudBuilder.Core.DependencyInjection.Request;
            using CloudBuilder.Security.Data;
            using CloudBuilder.Core.Authorization;
            
namespace CloudBuilder.Web.Entry.Controllers.Security
{
[Authorize(AuthorizePolicy.PERMISSION)]
[ApiController]
[Route("[controller]/[action]")]
public class AuthController : ControllerBase
{
private readonly IApplicationService service;
private readonly IAuthService authService;

public AuthController(IApplicationService service, IAuthService authService)
{
this.service = service;
this.authService= authService;
}

        [AllowAnonymous]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Login(LoginData data)       
                    {
                    try
                    {            

                    var result = authService.Login(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityUser", operate: "LoginAgent")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic LoginAgent(LoginData data)       
                    {
                    try
                    {            

                    var result = authService.LoginAgent(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [AllowAnonymous]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic Register(RegisterData data)       
                    {
                    try
                    {            

                    var result = authService.Register(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [PermissionOperate(name: "SecurityUser", operate: "Update")]
                            [HttpPost]
                            [UnitOfWork]
                            public dynamic AmendPassword(LoginData data)       
                    {
                    try
                    {            

                    var result = authService.AmendPassword(data);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [AllowAnonymous]
                            [HttpGet]
                            public dynamic GetRsaPublicKey()
                    {
                    try
                    {

                    var result = authService.GetRsaPublicKey();
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic RefreshToken(JwtInfo jwtInfo)       
                    {
                    try
                    {            

                    var result = authService.RefreshToken(jwtInfo);
                    return result;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }           
                    }

                            [AllowAnonymous]
                            [HttpPost]
                            public dynamic Logout()
                    {
                    try
                    {

            authService.Logout();
   
                    return string.Empty;
                    }
                    catch (ValidatedException vex)
                    {
                    return vex;
                    }            
                    catch (Exception ex)
                    {
                    return new ServiceErrorException(ex);
                    }                 
                    }

                    
}
}