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

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

        public MealController(IConfiguration configuration, IHostEnvironment env)
        {
            _configuration = configuration;
            _env = env;
        }
        [HttpGet]
        public IActionResult View(int id, bool edit = false)
        {
            var service = new MenuService(_configuration, _env);
            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 = new MenuService(_configuration, _env);
                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 = new MenuService(_configuration, _env);
            var meal = id.HasValue ? service.GetMealById(id.Value) : new Meal();
            return View("Meal", meal);
        }

        [HttpPost]
        public IActionResult SaveMeal(Meal meal, IFormFile ImageFile)
        {
            var service = new MenuService(_configuration, _env);

            if (ImageFile != null && ImageFile.Length > 0)
            {
                var fileName = Path.GetFileNameWithoutExtension(ImageFile.FileName);
                var extension = Path.GetExtension(ImageFile.FileName);
                var uniqueName = $"{fileName}_{Guid.NewGuid()}{extension}";
                var imagePath = Path.Combine("wwwroot/images/meals", uniqueName);

                using (var stream = new FileStream(imagePath, FileMode.Create))
                {
                    ImageFile.CopyTo(stream);
                }

                // Spara relativ sökväg för visning
                meal.ImageUrl = $"/images/meals/{uniqueName}";
            }

            service.SaveOrUpdateMeal(meal);

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



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