using Microsoft.AspNetCore.Mvc;
using Aberwyn.Models;
using Aberwyn.Data;
using Microsoft.AspNetCore.Authorization;

namespace Aberwyn.Controllers
{
    public class MealController : Controller
    {
        private readonly IConfiguration _configuration;
        private readonly IHostEnvironment _env;
        private readonly MenuService _menuService;

        public MealController(IConfiguration configuration, IHostEnvironment env)
        {
            _configuration = configuration;
            _env = env;
        }
        [HttpGet]
        public IActionResult View(int id, bool edit = false)
        {
            var service = _menuService;
            var meal = service.GetMealById(id);

            ViewData["IsEditing"] = edit;    // → här

            if (meal == null)
            {
                return NotFound();
            }

            return View("View", meal);
        }

        [HttpGet]
        [Route("Meal/Tooltip/{id}")]
        public IActionResult Tooltip(int id)
        {
            try
            {
                var service = _menuService;
                var meal = service.GetMealById(id);

                if (meal == null)
                    return Content("<div>Hittade ingen rätt.</div>", "text/html");

                // Se till att Description aldrig är null
                var desc = meal.Description ?? "";

                // Gör en kort beskrivning
                var shortDesc = desc.Length > 100
                    ? desc.Substring(0, 100) + "…"
                    : desc;

                // Bygg upp en enkel HTML-snutt
                var html =
                    $"<div class='tooltip-content'>" +
                    $"  <strong>{meal.Name}</strong><br/>" +
                    (string.IsNullOrWhiteSpace(shortDesc)
                        ? "<em>Ingen beskrivning tillgänglig.</em>"
                        : $"<em>{shortDesc}</em>") +
                    "</div>";

                return Content(html, "text/html");
            }
            catch (Exception ex)
            {
                // För felsökning kan du logga ex.Message, men för tillfället returnera det så vi ser det i devtools
                return StatusCode(500, $"<pre>{ex.Message}</pre>");
            }
        }




        [HttpGet]
        public IActionResult Edit(int? id)
        {
            var service = _menuService;
            var meal = id.HasValue ? service.GetMealById(id.Value) : new Meal();
            return View("Meal", meal);
        }

        [HttpPost]
        public IActionResult SaveMeal(Meal meal, IFormFile ImageFile, string ExistingImageUrl)
        {
            var service = _menuService;

            if (ImageFile != null && ImageFile.Length > 0)
            {
                using var ms = new MemoryStream();
                ImageFile.CopyTo(ms);
                meal.ImageData = ms.ToArray();
                meal.ImageMimeType = ImageFile.ContentType;
            }
            else
            {
                var existingMeal = service.GetMealById(meal.Id);
                if (existingMeal != null)
                {
                    meal.ImageData = existingMeal.ImageData;
                    meal.ImageMimeType = existingMeal.ImageMimeType;
                }
            }

            // 🔁 Här är ändringen – använd metoden som även sparar ingredienser
            service.SaveOrUpdateMealWithIngredients(meal);

            return RedirectToAction("View", new { id = meal.Id });
        }






        [HttpPost]
        public IActionResult DeleteMeal(int id)
        {
            var service = _menuService;
            //service.DeleteMeal(id);
            return RedirectToAction("Edit"); // eller tillbaka till lista
        }
    }
}
