using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer;
using Nop.Plugin.Misc.FruitBankPlugin.Services;
using Nop.Services.Security;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Mvc.Filters;
using System;
using System.IO;
using System.Threading.Tasks;

namespace Nop.Plugin.Misc.FruitBank.Controllers
{
    [AuthorizeAdmin]
    [Area(AreaNames.ADMIN)]
    [AutoValidateAntiforgeryToken]
    public class FruitBankAudioController : BasePluginController
    {
        private readonly IPermissionService _permissionService;
        private readonly OpenAIApiService _aiApiService;
        private readonly FruitBankDbContext _dbContext;

        public FruitBankAudioController(
            IPermissionService permissionService,
            OpenAIApiService aiApiService,
            FruitBankDbContext fruitBankDbContext)
        {
            _permissionService = permissionService;
            _aiApiService = aiApiService;
            _dbContext = fruitBankDbContext;
        }

        /// <summary>
        /// Display the voice recorder page
        /// </summary>
        public async Task<IActionResult> VoiceRecorder()
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
                return AccessDeniedView();

            return View("~/Plugins/Misc.FruitBankPlugin/Areas/Admin/Views/Extras/VoiceRecorder.cshtml");
        }

        /// <summary>
        /// Endpoint to receive voice recording
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> ReceiveVoiceRecording(IFormFile audioFile)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermission.Security.ACCESS_ADMIN_PANEL))
                return Json(new { success = false, message = "Access denied" });

            if (audioFile == null || audioFile.Length == 0)
            {
                return Json(new { success = false, message = "No audio file received" });
            }

            string productDataSummary = string.Empty;
            string orderDataSummary = string.Empty;

            var products = await _dbContext.Products.GetAll().ToListAsync();
            foreach (var product in products)
            {
                //let's prepare basic product and stock information for AI analysis
                if (product == null) continue;

                var productName = product.Name;
                var stockQuantity = product.StockQuantity;

                productDataSummary += $"Product: {productName}, Stock Quantity: {stockQuantity}\n";

            }

            var orders = await _dbContext.OrderDtos.GetAll(true).ToListAsync();

            foreach (var order in orders)
            {
                //let's prepare basic order information for AI analysis
                if (order == null) continue;
                var orderId = order.Id;
                var customerName = order.Customer.Company;
                var totalAmount = order.OrderTotal;
                var dateofReceipt = order.DateOfReceipt;
                var isMeasurable = order.IsMeasurable;
                var itemsInOrder = order.OrderItemDtos.Count;
                orderDataSummary += $"Order ID: {orderId}, Customer: {customerName}, Total Amount: {totalAmount}, Date of Receipt: {dateofReceipt}, Is Measurable: {isMeasurable}, Items in Order: {itemsInOrder}\n";
            }


            Console.WriteLine("Product Data Summary: " + productDataSummary);

            try
            {
                // Generate a unique filename
                var fileName = $"voice_recording_{DateTime.Now:yyyyMMdd_HHmmss}.webm";

                // Define the path where you want to save the file
                var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", "voice");

                // Create directory if it doesn't exist
                if (!Directory.Exists(uploadsFolder))
                {
                    Directory.CreateDirectory(uploadsFolder);
                }

                var filePath = Path.Combine(uploadsFolder, fileName);

                // Save the file locally
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await audioFile.CopyToAsync(stream);
                }

                // Transcribe the audio using OpenAI Whisper API
                string transcribedText;
                using (var audioStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    transcribedText = await _aiApiService.TranscribeAudioAsync(audioStream, fileName, "hu"); // Hungarian language
                }

                if (string.IsNullOrEmpty(transcribedText))
                {
                    return Json(new
                    {
                        success = false,
                        message = "Failed to transcribe audio"
                    });
                }

                string SystemMessage = "You are an assistant that helps with analyzing data for Fruitbank, a fruit trading company based in Hungary. " +
                    "Provide insights and suggestions based on the provided data." +
                    $"Products information: {productDataSummary}" +
                    $"Orders information: {orderDataSummary}";


                var AIResponse = await _aiApiService.GetSimpleResponseAsync(SystemMessage, $"Transcribed Text: {transcribedText}");



                return Json(new
                {
                    success = true,
                    message = $"Audio transcribed successfully",
                    transcription = AIResponse,
                    filePath = filePath,
                    fileSize = audioFile.Length
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = $"Error processing audio: {ex.Message}"
                });
            }
        }
    }
}