using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Aberwyn.Data;
using Aberwyn.Models;

namespace Aberwyn.Models
{
    public class MenuViewModel
    {
        public List<Meal> Meals { get; set; }  // List of all available meals
        public List<WeeklyMenu> WeeklyMenus { get; set; }  // List of weekly menu entries
        public int WeekNumber { get; set; }  // Week number for the menu
        public int Year { get; set; }  // Year for the menu

    }
    public class WeeklyMenuDto  
    {
    public int Id { get; set; }

    public int DayOfWeek { get; set; }
    public int WeekNumber { get; set; }
    public int Year { get; set; }

    public int? BreakfastMealId { get; set; }
    public int? LunchMealId { get; set; }
    public int? DinnerMealId { get; set; }

    public string? BreakfastMealName { get; set; }
    public string? LunchMealName { get; set; }
    public string? DinnerMealName { get; set; }

    public byte[]? DinnerMealThumbnail { get; set; }
}

public class WeeklyMenu
{
    public int Id { get; set; }
    public int DayOfWeek { get; set; }
    public int? BreakfastMealId { get; set; }
    public int? LunchMealId { get; set; }
    public int? DinnerMealId { get; set; }
    public string? Cook { get; set; }
    public DateTime Date { get; set; }
    public int WeekNumber { get; set; }
    public int Year { get; set; }
    public DateTime CreatedAt { get; set; }
    [NotMapped] public byte[]? BreakfastThumbnail { get; set; }
    [NotMapped] public byte[]? LunchThumbnail { get; set; }
    [NotMapped] public byte[]? DinnerThumbnail { get; set; }

        [NotMapped] public string? BreakfastMealName { get; set; }
    [NotMapped] public string? LunchMealName { get; set; }
    [NotMapped] public string? DinnerMealName { get; set; }


}



    public class RecentMenuEntry
    {
        public DateTime Date { get; set; }
        public int WeekNumber { get; set; }   // Lägg till vecka
        public int Year { get; set; }         // Lägg till år
        public string BreakfastMealName { get; set; }
        public string LunchMealName { get; set; }
        public string DinnerMealName { get; set; }
    }
    public class Meal
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public string Name { get; set; } // Behåll som obligatorisk

        public string? Description { get; set; }
        public string? ProteinType { get; set; }
        public int? MealCategoryId { get; set; }

        [ForeignKey("MealCategoryId")]
        public MealCategory? Category { get; set; }
        public string? CarbType { get; set; }
        public string? RecipeUrl { get; set; }
        public string? ImageUrl { get; set; }
    
        public bool IsAvailable { get; set; }
        public DateTime CreatedAt { get; set; }
        public byte[]? ThumbnailData { get; set; }
        public bool IsPublished { get; set; } = false;
        public byte[]? ImageData { get; set; }         
        public string? ImageMimeType { get; set; }     
        public string? Instructions { get; set; }  

        public List<Ingredient> Ingredients { get; set; } = new();
    }

    public class Ingredient
    {
        public int Id { get; set; }
        public int MealId { get; set; }
        public string Quantity { get; set; }
        public string Item { get; set; }
    }

    public class MealDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public bool IsAvailable { get; set; }
        public string? ImageUrl { get; set; }
        public string? ImageData { get; set; } // base64
        public string? ImageMimeType { get; set; }

        public static MealListDto FromMeal(Meal meal, bool includeThumbnail = false)
        {
            return new MealListDto
            {
                Id = meal.Id,
                Name = meal.Name,
                Description = meal.Description,
                ThumbnailData = includeThumbnail && meal.ThumbnailData != null
                    ? Convert.ToBase64String(meal.ThumbnailData)
                    : null
            };
        }

    }
    public class MealListDto
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public string? Description { get; set; }
    public string? ThumbnailData { get; set; }

    public List<IngredientDto> Ingredients { get; set; } = new();

    public static MealListDto FromMeal(Meal meal, bool includeThumbnail = false)
    {
        return new MealListDto
        {
            Id = meal.Id,
            Name = meal.Name,
            Description = meal.Description,
            ThumbnailData = includeThumbnail && meal.ThumbnailData != null
                ? Convert.ToBase64String(meal.ThumbnailData)
                : null,
            Ingredients = meal.Ingredients?.Select(i => new IngredientDto
            {
                Item = i.Item
            }).ToList() ?? new List<IngredientDto>()
        };
    }

    public class IngredientDto
    {
        public string Item { get; set; } = "";
    }
}

    public class MealCategory
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        public string Name { get; set; }

        public string? Slug { get; set; }
        public string? Description { get; set; }
        public string? Icon { get; set; }
        public string? Color { get; set; }
        public bool IsActive { get; set; } = true;
        public int DisplayOrder { get; set; } = 0;
        [NotMapped] // krävs om du inte har kolumnen i databasen
        public int MealCount { get; set; }

        public List<Meal> Meals { get; set; } = new();
    }
    public class MealWish
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; } 
        public string? Recipe { get; set; } 
        public int? LinkedMealId { get; set; } 
        [ForeignKey("LinkedMealId")]
        public Meal? LinkedMeal { get; set; }
        [Required]
        public string RequestedByUserId { get; set; }
        [ForeignKey(nameof(RequestedByUserId))]
        public ApplicationUser? RequestedByUser { get; set; }
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
        public bool IsArchived { get; set; } = false; 
        public bool IsImported { get; set; } = false;
    }


    public class CreateMealWishDto
    {
        public string Name { get; set; }
        public string? Recipe { get; set; }
    }
    public class MealWishDto
    {
        public int Id { get; set; }
        public string Name { get; set; } = "";
        public string? Recipe { get; set; }
        public bool IsArchived { get; set; }
        public bool IsImported { get; set; }

        public static MealWishDto FromEntity(MealWish wish)
        {
            return new MealWishDto
            {
                Id = wish.Id,
                Name = wish.Name,
                Recipe = wish.Recipe,
                IsArchived = wish.IsArchived,
                IsImported = wish.IsImported
            };
        }
    }

    public class MealRating
    {
        public int Id { get; set; }
        public int MealId { get; set; }
        public string UserId { get; set; }
        public int Rating { get; set; }
        public DateTime CreatedAt { get; set; }

        public Meal Meal { get; set; }
    }
    public class MealRatingDto
    {
        public int MealId { get; set; }
        public int Rating { get; set; }
    }


}

