using DevExpress.Office.Crypto;
using DevExpress.Xpo.DB;
using DevExpress.XtraPrinting;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text.Json;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.PageModels;
using TIAMWebApp.Server.Models;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using TIAM.Database.DataLayers.Users;
using AyCode.Utils.Helpers;
using TIAM.Entities.Users;
using TIAMWebApp.Server.ModelsTIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Utility;
using TIAM.Entities.Auctions;
using AyCode.Interfaces.Messages;
using AyCode.Entities.Messages;
using TIAMWebApp.Shared.Application.Models.ClientSide.Messages;
using AyCode.Models.Messages;
using AyCode.Models.Enums;
using TIAM.Database.DataLayers.Admins;

namespace TIAMWebApp.Server.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/v1/[controller]")]
    public class MessageAPIController : ControllerBase
    {
        private AdminDal _adminDal;
        private readonly IConfiguration _configuration;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private IMessageSenderService _messageSenderService;


        private readonly ILogger<MessageAPIController> _logger;

        public MessageAPIController(ILogger<MessageAPIController> logger, IConfiguration configuration, IWebHostEnvironment webHostEnvironment, IMessageSenderService messageSenderService, AdminDal adminDal)
        {
            _adminDal = adminDal;
            _logger = logger;
            _configuration = configuration;
            _webHostEnvironment = webHostEnvironment;

            _messageSenderService = messageSenderService;
        }

        [AllowAnonymous]
        [HttpPost]
        [Route("SendEmail")]
        public async Task<IActionResult> SendEmail([FromBody] EmailMessageSenderModel SerializedMessageSenderModel)
        {

            if (SerializedMessageSenderModel != null)
            {

                if (SerializedMessageSenderModel.MessageType == MessageTypesEnum.email && SerializedMessageSenderModel.Message is EmailMessage)
                {

                        Console.WriteLine($"EmailMessage!!!");
                        
                        var result = await _messageSenderService.SendMessageAsync(SerializedMessageSenderModel.Message, (int)SerializedMessageSenderModel.MessageType);
                        //_adminDal.AddEmailMessageAsync((TIAM.Entities.Emails.EmailMessage)SerializedMessageSenderModel.Message);
                        Console.WriteLine("SendEmail result: " + result);
                        return Ok(result);
                  
                }
                else
                {
                    return BadRequest("Invalid request");
                    // Access BaseClass properties
                }


            }
            return BadRequest("Invalid request");

        }

    }
}