using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ReAct_PME.Application;
using Services;

namespace ReAct_PME_API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ListeMailsController(IListeMailsService service) : ControllerBase
    {
        [HttpGet]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult> GetAll()
        {
            var lst = await service.GetAll_Async();
            return Ok(lst);
        }

        [HttpGet("{ID}")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult<Services.EmailMessage>>? GetById(string ID)
        {
            var item = await service.GetById_Async(ID);
            if (item == null)
            {
                return NotFound();
            }
            return Ok(item);
        }

        [HttpPost("saveitem")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult>? SaveItem([FromBody] Services.EmailMessage item)
        {
            var b = await service.SaveItem(item);
            if (!b)
            {
                return NotFound();
            }
            return Ok(item);
        }

        [HttpPost("marquerlu")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult>? MarquerLu([FromBody] Services.EmailMessage item)
        {
            var b = await service.MarquerLu(item);
            if (!b)
            {
                return NotFound();
            }
            return Ok(item);
        }

        [HttpGet("send")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult> GetSendAll()
        {
            var lst = await service.GetSendAll_Async();
            return Ok(lst);
        }

        [HttpGet("send/{ID}")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult<Services.ThreadItem>>? GetSendById(string ID)
        {
            var item = await service.GetByIdSend_Async(ID);
            if (item == null)
            {
                return NotFound();
            }
            return Ok(item);
        }

        [HttpPost("sendsaveitem")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult>? SaveSendItem([FromBody] Services.ThreadItemMini item)
        {
            var b = await service.SaveSendItem(item);
            if (!b)
            {
                return NotFound();
            }
            return Ok(item);
        }

        [HttpPost("sendarchiverestore")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult>? ArchiverRestaurerItem([FromBody] Services.ThreadItemMini item)
        {
            var b = await service.ArchiverRestaurerItem(item);
            if (!b)
            {
                return NotFound();
            }
            return Ok(item);
        }

        [HttpPost("generateagain")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult>? GenerateAgain([FromBody]string[] lst)
        {
            var context = lst[0];
            var query = lst[1];
            var item = await service.GenerateAgain(context, query);
            if (item == null)
            {
                return NotFound();
            }
            return Ok(item);
        }

        [HttpPost("EnvoyerMail")]
        //[Authorize(Roles = "Admin,Manager,User")]
        [AllowAnonymous]
        public async Task<ActionResult>? EnvoyerMail([FromBody] MessageToSend data)
        {
            var item = await service.EnvoyerMailAsync(data);
            if (item == null)
            {
                return NotFound();
            }
            return Ok(item);
        }
    }
}
