using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ReAct_PME.Application;
using ReAct_PME.Domain;

namespace ReAct_PME_API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ChatRoomController(IChatRoomService service, IDT_CONVERSATIONService dt_conversationService, IDT_CONVERSATION_LIGNEService dt_conversation_ligneService, IDT_WORK_FORCEService dt_work_forceService) : ControllerBase
    {
        [HttpGet("llm/test")]
        public async Task<ActionResult> TestRequest()
        {
            await Task.Delay(1);
            return Ok(true);
        }

        [HttpPost("llm")]
        public async Task<ActionResult> EnvoiRequete([FromBody] List<string> requete)
        {
            var rep = await service.EnvoiRequete_LLM(requete, dt_conversationService, dt_conversation_ligneService, dt_work_forceService);
            return Ok(rep);
        }

        [HttpPost("rag")]
        public async Task<ActionResult> EnvoiRequete_RAG([FromBody] List<string> requete)
        {
            var rep = await service.EnvoiRequete_RAG(requete, dt_conversationService, dt_conversation_ligneService, dt_work_forceService);
            return Ok(rep);
        }

        [HttpGet("rag/listeDomaines")]
        public async Task<ActionResult> ListeDomaines_RAG()
        {
            var rep = await service.ListeDomaines_RAG();
            return Ok(rep);
        }

        [HttpGet("conversation/{idConv}")]
        public async Task<ActionResult> GetConversation(string idConv)
        {
            Guid Id = Guid.Parse(idConv);
            var item = await service.GetConversation(Id, dt_conversationService);
            return Ok(item);
        }

        [HttpGet("conversations/{idWF}/{typeLLM}")]
        public async Task<ActionResult> GetConversations(string idWF, int typeLLM) 
        {
            await Task.Delay(1);
            List<ConversationDto>? lst = new();
            Guid Id = Guid.Parse(idWF);
            lst = await service.GetConversations(Id, dt_conversationService);
            if (lst != null && lst.Count > 0)
            {
                if (typeLLM == 1)
                {
                    lst = lst.Where(c => c.TypeConv.ToUpper() == "LLM").ToList();
                }
                else if (typeLLM == 2)
                {
                    lst = lst.Where(c => c.TypeConv.ToUpper() == "RAG").ToList();
                }
                else if (typeLLM == 3)
                {
                    lst = lst.Where(c => c.TypeConv.ToUpper() == "AUDIT").ToList();
                }
                else if (typeLLM == 4)
                {
                    lst = lst.Where(c => c.TypeConv.ToUpper() == "IMAGE").ToList();
                }
            }
            return Ok(lst);
        }

        [HttpPost("conversation/create")]
        public async Task<IActionResult> CreateConversation()
        {
            await Task.Delay(1);
            ConversationDto? lst = new();
            return Ok(lst);
        }

        [HttpDelete("conversation/{id}")]
        public async Task<IActionResult> DeleteConversation(string id)
        {
            await Task.Delay(1);
            List<ConversationDto>? lst = new();
            return Ok(lst);
        }

        [HttpGet("messages/{conversationId}")]
        public async Task<IActionResult> GetMessages(string conversationId)
        {
            await Task.Delay(1);
            List<MessageDto>? lst = new();
            return Ok(lst);
        }

        [HttpPost("sendMessage")]
        public async Task<IActionResult> SendMessage([FromBody] SendMessageRequest req)
        {
            if (req.Mode.ToUpper() == "LLM")
            {
                var rep = await service.EnvoiRequete_LLM(req, dt_conversationService, dt_conversation_ligneService, dt_work_forceService);
                return Ok(rep);
            }
            else if (req.Mode.ToUpper() == "RAG")
            {
                var rep = await service.EnvoiRequete_RAG(req, dt_conversationService, dt_conversation_ligneService, dt_work_forceService);
                return Ok(rep);
            }
            return BadRequest("Invalid mode specified.");
        }

        [HttpGet("ollama/models")]
        public async Task<IActionResult> GetModels()
        {
            await Task.Delay(1);
            List<string>? lst = new();
            return Ok(lst);
        }

        [HttpGet("rag/domains")]
        public async Task<IActionResult> GetDomains()
        {
            await Task.Delay(1);
            List<string>? lst = new();
            return Ok(lst);
        }

        [HttpPost("rag/search")]
        public async Task<IActionResult> RagSearch(string query)
        {
            await Task.Delay(1);
            List<string>? lst = new();
            return Ok(lst);
        }

        [HttpGet("status/ollama")]
        public async Task<IActionResult> OllamaStatus()
        {
            await Task.Delay(1);
            bool lst = true;
            return Ok(lst);
        }

        [HttpPost("documents/upload")]
        public async Task<IActionResult> UploadDocument(IFormFile file)
        {
            await Task.Delay(1);
            List<ConversationDto>? lst = new();
            return Ok(lst);
        }

        [HttpDelete("documents/{id}")]
        public async Task<IActionResult> DeleteDocument(string id)
        {
            await Task.Delay(1);
            List<ConversationDto>? lst = new();
            return Ok(lst);
        }
    }
}
