using Common.Dtos;
using Common.Dtos.Conversation;
using Common.Enums;
using Hushian.Application.Constants;
using Hushian.Application.Services;
using Hushian.Domain.Entites;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hushian.WebApi.Controllers.v1
{
    [Route("api/v1/[controller]")]
    [ApiController]
  //  [Authorize]
    public class ConversationController : ControllerBase
    {
        private readonly ConversationService _conversationService;
        private readonly CompanyService _companyService;
        private readonly ExperService _experService;
        private readonly AIService _aIService;
        public ConversationController(ConversationService conversationService, CompanyService companyService, ExperService experService, AIService aIService)
        {
            _conversationService = conversationService;
            _companyService = companyService;
            _experService = experService;
            _aIService = aIService;
        }

        //*
        [HttpPost("MyConversation")]
        [Authorize(Roles = "Company,Exper")]
        public async Task<ActionResult> MyConversation([FromBody]ConversationStatus status)
        {
            if (User.IsInRole("Exper"))
            {
                string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                int ExperID = Convert.ToInt32(strExperID);
                var response = await _conversationService.GetConversationByExperID(ExperID, status);
                return Ok(response);
            }
            else if (User.IsInRole("Company"))
            {
                string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                int CompanyID = Convert.ToInt32(strCompanyID);

                var response = await _conversationService.GetConversationByCompanyID(CompanyID, status);
                return Ok(response);
            }
            return Forbid();

        }
        //*
        [HttpGet("ConversationAwaitingOurResponse")]
        [Authorize(Roles = "Company,Exper")]
        public async Task<ActionResult> ConversationAwaitingOurResponse()
        {
            int CompanyID = 0;
            if (User.IsInRole("Exper"))
            {
                string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                int ExperID = Convert.ToInt32(strExperID);

                CompanyID = await _experService.GetCompanyIDExper(ExperID);
            }
            else if (User.IsInRole("Company"))
            {
                string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                CompanyID = Convert.ToInt32(strCompanyID);
            }

            var response = await _conversationService.ConversationAwaitingOurResponse(CompanyID);
            return Ok(response);
        }
        //*
        [HttpGet("ConversationItems/{ConversationID}")]
        [Authorize(Roles = "Company,Exper,User")]
        public async Task<ActionResult> GetConversationItems(int ConversationID)
        {
            return Ok(await _conversationService.GetConversationItems(ConversationID));
        }
        //*
        [HttpPost("NewConversationFromCurrentUser")]
        [Authorize(Roles = "User")]
        public async Task<ActionResult> NewConversationFromCurrentUser(ADD_ConversationDto conversation)
        {
            conversation.UserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => Convert.ToInt32( s.Value)).First();
            var response = await _conversationService.NewConversation(conversation);
            return response.Success ? Ok(response.Value)
                : BadRequest(response.Errors);
        }
        //*
        [HttpPost("ADDConversationResponse")]
        [Authorize(Roles = "Company,User,Exper")]
        public async Task<ActionResult> ADDConversationResponse([FromBody] ADD_ConversationResponseDto ConversationItem)
        {

            int? ExperID = null;
            if (User.IsInRole("Exper"))
            {
                string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                ExperID = Convert.ToInt32(strExperID);
                ConversationItem.Type = ConversationType.EU;
            }

            else if (User.IsInRole("User"))
            {
                ConversationItem.Type = ConversationType.UE;
            }
            else if (User.IsInRole("Company"))
            {
                ConversationItem.Type = ConversationType.CU;

            }
            else return Unauthorized();


            var Response = await _conversationService.NewConversationResponse(ConversationItem, ExperID);
            return Response.Success ? Ok(Response.Value)
               : BadRequest(Response.Errors);
        }
        //---------------------------------------------------------------------------------------------------------------------
        [HttpPut("MarkAsReadConversationItem/{ConversationItemID}")]
        [Authorize(Roles = "Company,User,Exper")]
        public async Task<ActionResult> MarkAsReadConversationItem(int ConversationItemID)
        {
            int? ExperID = null;
            ConversationType Type = ConversationType.UE;
            if (User.IsInRole("Exper"))
            {
                string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                ExperID = Convert.ToInt32(strExperID);
                Type = ConversationType.EU;
            }

            else if (User.IsInRole("User"))
            {
                Type = ConversationType.UE;
            }
            else if (User.IsInRole("Company"))
            {
                Type = ConversationType.CU;
            }
            else return Unauthorized();

            return await _conversationService.MarkAsReadConversationItem(ConversationItemID, Type, ExperID) ? NoContent()
         : BadRequest(new List<string>() { "خطا در بروزرسانی گفتگو" });

        }
        [HttpGet("ConversationIsFinish/{ConversationID}")]
        [Authorize(Roles = "Company,Exper")]
        public async Task<ActionResult> ConversationIsFinish(int ConversationID)
        {
            return await _conversationService.FinishConversation(ConversationID) ? NoContent()
                : BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
        }
        [HttpGet("CountQueueCompany/{CompanyID}")]
        [Authorize]
        public async Task<ActionResult<int>> GetCountQueueCompany(int CompanyID, int GroupID)
        {
            return Ok(await _conversationService.GetCountQueueCompany(CompanyID, GroupID));
        }
        [HttpGet("ConversationFromUserSide/{CompanyID}")]
        [Authorize(Roles = "User")]
        public async Task<ActionResult> GetConversationFromUserSide(int CompanyID)
        {
            string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int UserID = Convert.ToInt32(strUserID);
            var response = await _conversationService.GEtConversation(UserID,CompanyID );
            return Ok(response) ;
        }
        [HttpPost("ai/NewResponse")]
        //[Authorize(Roles = "User")]
        public async Task<ActionResult> ainewresponse(aiNewResponseDto dto)
        {
            //string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            //dto.userId = Convert.ToInt32(strUserID);
            var response = await _aIService.NewRequest(dto);
            return response.Success ? Ok(response.Value):BadRequest(response.Errors);
        }
        [HttpGet("ai/CurrentResponse/{companyID}/{aiKeyUser}")]
        //[Authorize(Roles = "User")]
        public async Task<ActionResult> aiCurrentResponse(int companyID,string aiKeyUser)
        {
            //string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            //var UserId = Convert.ToInt32(strUserID);
            var response = await _aIService.GetCurrent(companyID, aiKeyUser);
            return  Ok(response);
        }
    }
}
