using Google.Apis.Auth.OAuth2;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Serilog;
using System.Net.Http.Headers;
using System.Text;
using WorkFlowCheck.BL.Services.Interfaces;
using WorkFlowCheck.Common.DTO;

namespace WorkFlowCheck.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FCMController : ControllerBase
    {
        private readonly IMessageService _messageService;
        public FCMController(IMessageService messageService)
        {
            _messageService = messageService;
        }

        [HttpPost("SendFCMMessage")]
        public async Task<ApiResponseDTO<bool>> SendFCMMessage([FromBody] FCMMessageDTO fCMMessageDTO)
        {
            var retVal = new ApiResponseDTO<bool>()
            {
                IsSuccess = true
            };

            try
            {
                retVal.IsSuccess = await _messageService.SendFCMMessage(fCMMessageDTO);
            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }

        [HttpPost("SendFCMMessageToLastToken")]
        public async Task<ApiResponseDTO<bool>> SendFCMMessageToLastToken([FromBody] FCMMessageDTO fCMMessageDTO)
        {
            var retVal = new ApiResponseDTO<bool>()
            {
                IsSuccess = true
            };

            try
            {
                retVal.IsSuccess = await _messageService.SendFCMMessageToLastToken(fCMMessageDTO);
            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }

        [HttpPost("SendFCMToken")]
        public async Task<ApiResponseDTO<bool>> SendFCMToken([FromBody] FCMMessageDTO fCMMessageDTO)
        {
            var retVal = new ApiResponseDTO<bool>()
            {
                IsSuccess = true
            };

            try
            {
                await _messageService.SendFCMTokenAsync(fCMMessageDTO.Token);
            }
            catch (Exception ex)
            {
                retVal.IsSuccess = false;
                Log.Error(ex.Message);
            }
            return retVal;
        }
    }
}
