using Common.Model;
using Common.Util;
using Microsoft.EntityFrameworkCore;

namespace Common.Database
{
    public class ApiaryContext : DbContext
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<Order> Orders { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=C:\\Users\\Amanda Vince\\source\\repos\\Apiary\\Common\\DB\\apiary.sqlite");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
#pragma warning disable CA1416 // Validate platform compatibility
            modelBuilder.Entity<Product>()
                .Property(p => p.Image)
                .HasConversion<ImageToBytesConverter>();
#pragma warning restore CA1416 // Validate platform compatibility


            modelBuilder.Entity<Order>()
                .HasMany(o => o.Products)
                .WithOne(p => p.Order)
                .HasForeignKey(p => p.OrderId)
                .OnDelete(DeleteBehavior.NoAction);

            modelBuilder.Entity<Order>().HasData(
                new Order { ID = 1, OrderDate = DateTime.Parse("2026-03-17") },
                new Order { ID = 2, OrderDate = DateTime.Parse("2026-03-17") },
                new Order { ID = 3, OrderDate = DateTime.Parse("2026-03-17") },
                new Order { ID = 4, OrderDate = DateTime.Parse("2026-03-17") },
                new Order { ID = 5, OrderDate = DateTime.Parse("2026-03-17") },
                new Order { ID = 6, OrderDate = DateTime.Parse("2026-03-17") },
                new Order { ID = 7, OrderDate = DateTime.Parse("2026-03-17") },
                new Order { ID = 8, OrderDate = DateTime.Parse("2026-03-17") },
                new Order { ID = 9, OrderDate = DateTime.Parse("2026-03-17") }
            );

            modelBuilder.Entity<Product>().HasData(
                new Product
                {
                    ID = 1,
                    Name = "Multi-Purpose Marine Rope",
                    Description = "Heavy-duty rope suitable for boating, camping, and general use.",
                    Price = 19.99f,
                    PackagingDate = DateTime.ParseExact("2025-01-15", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-04-13", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 2
                }, 
                new Product
                {
                    ID = 2,
                    Name = "Workstation Laptop Stand",
                    Description = "Adjustable stand to improve ergonomics while working on a laptop.",
                    Price = 49.99f,
                    PackagingDate = DateTime.ParseExact("2025-04-24", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-06-07", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 3
                },
                new Product
                {
                    ID = 3,
                    Name = "Basil Pesto Pasta",
                    Description = "Pasta tossed with fresh basil pesto, simple and delicious.",
                    Price = 6.49f,
                    PackagingDate = DateTime.ParseExact("2026-01-10", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-04-03", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 1
                },
                new Product
                {
                    ID = 4,
                    Name = "Apple Cinnamon Instant Oatmeal",
                    Description = "Quick oatmeal packets infused with apple and cinnamon, perfect for breakfast.",
                    Price = 3.49f,
                    PackagingDate = DateTime.ParseExact("2025-10-28", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-01-22", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 2
                },
                new Product
                {
                    ID = 5,
                    Name = "Sturdy Bookends",
                    Description = "Stylish bookends to keep books organized on shelves.",
                    Price = 22.99f,
                    PackagingDate = DateTime.ParseExact("2025-09-27", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-05-27", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 2
                },
                new Product
                {
                    ID = 6,
                    Name = "Overnight Duffle Bag",
                    Description = "Spacious duffle bag for weekend getaways.",
                    Price = 34.99f,
                    PackagingDate = DateTime.ParseExact("2025-03-28", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-03-28", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 1
                },
                new Product
                {
                    ID = 7,
                    Name = "Coconut Macaroons",
                    Description = "Chewy cookies made with coconut, perfect for sweet cravings.",
                    Price = 4.99f,
                    PackagingDate = DateTime.ParseExact("2025-10-13", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-06-03", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 4
                },
                new Product
                {
                    ID = 8,
                    Name = "Electric Ice Cream Maker",
                    Description = "Make delicious ice cream at home with this user-friendly machine.",
                    Price = 59.99f,
                    PackagingDate = DateTime.ParseExact("2025-01-15", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-03-18", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 4
                },
                new Product
                {
                    ID = 9,
                    Name = "Reusable Food Storage Bags",
                    Description = "Eco-friendly silicone bags for food storage and snacks.",
                    Price = 19.99f,
                    PackagingDate = DateTime.ParseExact("2025-03-09", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-10-08", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 4
                },
                new Product
                {
                    ID = 10,
                    Name = "Oatmeal Raisin Cookies",
                    Description = "Delicious cookies packed with oats and raisins.",
                    Price = 3.49f,
                    PackagingDate = DateTime.ParseExact("2025-02-11", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    ExpirationDate = DateTime.ParseExact("2026-08-17", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
                    OrderId = 4
                }
           );
        }
    }
}
