using Microsoft.EntityFrameworkCore;
using Recepie.Models;

namespace Recepie.Data
{
    public class RecipeContext : DbContext
    {
        public RecipeContext(DbContextOptions<RecipeContext> options) : base(options)
        {
        }

        public DbSet<Recipe> Recipes { get; set; }
        public DbSet<Ingredient> Ingredients { get; set; }
        public DbSet<RecipeIngredientLink> RecipeIngredientLinks { get; set; }
        public DbSet<RecipeStep> RecipeSteps { get; set; }
        // public DbSet<RecipeImage> RecipeImages { get; set; }  // Temporarily disabled for main page performance

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Recipe>(entity =>
            {
                // Map to existing lowercase table name
                entity.ToTable("recepie");

                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).HasColumnName("id");

                // Map our model properties to your database columns
                entity.Property(e => e.Title).HasColumnName("name").IsRequired();
                entity.Property(e => e.Description).HasColumnName("description");
                entity.Property(e => e.Difficulty).HasColumnName("difficulty");
                entity.Property(e => e.Url).HasColumnName("url");
                entity.Property(e => e.Time).HasColumnName("time");

                // Index for better search performance
                entity.HasIndex(e => e.Title);
            });

            modelBuilder.Entity<Ingredient>(entity =>
            {
                entity.ToTable("ingredient");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.Name).HasColumnName("name");
            });

            modelBuilder.Entity<RecipeIngredientLink>(entity =>
            {
                entity.ToTable("recepieIngredientLink");
                entity.HasKey(e => new { e.RecipeId, e.IngredientId });
                entity.Property(e => e.RecipeId).HasColumnName("recepieId");
                entity.Property(e => e.IngredientId).HasColumnName("ingredientId");
                entity.Property(e => e.Amount).HasColumnName("amount");
                entity.Property(e => e.Measurement).HasColumnName("measurement");

                // Configure relationships
                entity.HasOne(e => e.Recipe)
                    .WithMany(r => r.RecipeIngredients)
                    .HasForeignKey(e => e.RecipeId);

                entity.HasOne(e => e.Ingredient)
                    .WithMany()
                    .HasForeignKey(e => e.IngredientId);
            });

            modelBuilder.Entity<RecipeStep>(entity =>
            {
                entity.ToTable("recepieSteps");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Id).HasColumnName("id");
                entity.Property(e => e.RecipeId).HasColumnName("recepieId");
                entity.Property(e => e.StepNumber).HasColumnName("stepNumber");
                entity.Property(e => e.Text).HasColumnName("text");

                // Configure relationship
                entity.HasOne(e => e.Recipe)
                    .WithMany(r => r.RecipeSteps)
                    .HasForeignKey(e => e.RecipeId);
            });

            // Temporarily disabled RecipeImage configuration for performance
            /*
            modelBuilder.Entity<RecipeImage>(entity =>
            {
                entity.ToTable("recepieImage");
                entity.HasKey(e => e.RecipeId);
                entity.Property(e => e.RecipeId).HasColumnName("recepieId");
                entity.Property(e => e.ImageData).HasColumnName("image");

                // Configure relationship
                entity.HasOne(e => e.Recipe)
                    .WithOne(r => r.RecipeImage)
                    .HasForeignKey<RecipeImage>(e => e.RecipeId);
            });
            */
        }
    }
}