using Common.Dtos.Conversation;
using Common.Dtos;
using Common.Enums;
using System.ComponentModel.Design;
using System.Net.Http.Json;

namespace HushianWebApp.Service
{
    public class ConversationService
    {
        private readonly BaseController _baseController;
        const string BaseRoute = "v1/Conversation/";

        public ConversationService(BaseController baseController)
        {
            _baseController = baseController;
        }

        public async Task <List<Read_ConversationDto>> MyConversationIsFinished()
        {
            var response = await _baseController.Post($"{BaseRoute}MyConversation", ConversationStatus.Finished);
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<List<Read_ConversationDto>>();
            
            return new() ;
        }
        public async Task<List<Read_ConversationDto>> MyConversationIsInProgress()
        {
            var response = await _baseController.Post($"{BaseRoute}MyConversation", ConversationStatus.InProgress);
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<List<Read_ConversationDto>>();

            return new();
        }
        public async Task<List<Read_ConversationDto>> ConversationAwaitingOurResponse()
        {
            var response = await _baseController.Get($"{BaseRoute}ConversationAwaitingOurResponse");
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<List<Read_ConversationDto>>();

            return new();
        }
        public async Task<bool> MarkAsReadConversationItemAsync(int ID)
        {
            var response = await _baseController.Put($"{BaseRoute}MarkAsReadConversationItem/{ID}");
            return response.IsSuccessStatusCode;

        }
        public async Task<List<Read_ConversationResponseDto>> GetConversationItems(int ConversationID)
        {
            var response = await _baseController.Get($"{BaseRoute}ConversationItems/{ConversationID}");
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<List<Read_ConversationResponseDto>>();

            return new();
        }
        public async Task<Read_ConversationResponseDto?> ADDConversationItem(int ConversationID,string text, ConversationType type)
        {
            var response = await _baseController.Post($"{BaseRoute}ADDConversationResponse",new ADD_ConversationResponseDto()
            {
                ConversationID = ConversationID,
                Text=text,
                Type=type
            }); 
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<Read_ConversationResponseDto>();
            return null;

        }
        public async Task<bool> ConversationIsFinish(int ConversationID)
        {
            var response = await _baseController.Put($"{BaseRoute}ConversationIsFinish/{ConversationID}");
            return response.IsSuccessStatusCode;
        }
        public async Task<int> GetCountQueueCompany(int CompanyID,int GroupID=0)
        {
            var response = await _baseController.Get($"{BaseRoute}CountQueueCompany/{CompanyID}?GroupID={GroupID}");
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<int>();

            return -1;
        }
        public async Task<List<Read_ConversationDto>> MyConversationUserSide(int CompanyID)
        {
            var response = await _baseController.Get($"{BaseRoute}ConversationFromUserSide/{CompanyID}");
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<List<Read_ConversationDto>>();

            return new();
        }
        public async Task<Read_ConversationDto> NewConversationFromCurrentUser(ADD_ConversationDto conversation)
        {
            var response = await _baseController.Post($"{BaseRoute}NewConversationFromCurrentUser", conversation);
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<Read_ConversationDto>();

            return null;
        }

        // AI conversation endpoints
        public async Task<List<aiResponseDto>> GetAiCurrentResponses(int companyId,string aiKeyUser)
        {
            var response = await _baseController.Get($"{BaseRoute}ai/CurrentResponse/{companyId}/{aiKeyUser}");
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<List<aiResponseDto>>();

            return new();
        }

        public async Task<aiResponseDto?> AiNewResponse(aiNewResponseDto dto)
        {
            var response = await _baseController.Post($"{BaseRoute}ai/NewResponse", dto);
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<aiResponseDto>();

            return null;
        }
    }
}
