using Microsoft.EntityFrameworkCore;
using LittleShop.Models;

namespace LittleShop.Data;

public class LittleShopContext : DbContext
{
    public LittleShopContext(DbContextOptions<LittleShopContext> options) : base(options)
    {
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<ProductPhoto> ProductPhotos { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderItem> OrderItems { get; set; }
    public DbSet<CryptoPayment> CryptoPayments { get; set; }
    public DbSet<ShippingRate> ShippingRates { get; set; }
    public DbSet<Bot> Bots { get; set; }
    public DbSet<BotMetric> BotMetrics { get; set; }
    public DbSet<BotSession> BotSessions { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<CustomerMessage> CustomerMessages { get; set; }
    public DbSet<PushSubscription> PushSubscriptions { get; set; }
    public DbSet<Review> Reviews { get; set; }

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

        // User entity
        modelBuilder.Entity<User>(entity =>
        {
            entity.HasIndex(e => e.Username).IsUnique();
        });

        // Category entity
        modelBuilder.Entity<Category>(entity =>
        {
            entity.HasMany(c => c.Products)
                  .WithOne(p => p.Category)
                  .HasForeignKey(p => p.CategoryId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        // Product entity
        modelBuilder.Entity<Product>(entity =>
        {
            entity.HasMany(p => p.Photos)
                  .WithOne(pp => pp.Product)
                  .HasForeignKey(pp => pp.ProductId)
                  .OnDelete(DeleteBehavior.Cascade);

            entity.HasMany(p => p.OrderItems)
                  .WithOne(oi => oi.Product)
                  .HasForeignKey(oi => oi.ProductId)
                  .OnDelete(DeleteBehavior.Restrict);
        });

        // Order entity
        modelBuilder.Entity<Order>(entity =>
        {
            entity.HasOne(o => o.Customer)
                  .WithMany(c => c.Orders)
                  .HasForeignKey(o => o.CustomerId)
                  .OnDelete(DeleteBehavior.Restrict)
                  .IsRequired(false); // Make customer optional for transition

            entity.HasMany(o => o.Items)
                  .WithOne(oi => oi.Order)
                  .HasForeignKey(oi => oi.OrderId)
                  .OnDelete(DeleteBehavior.Cascade);

            entity.HasMany(o => o.Payments)
                  .WithOne(cp => cp.Order)
                  .HasForeignKey(cp => cp.OrderId)
                  .OnDelete(DeleteBehavior.Cascade);

            entity.HasIndex(e => e.CustomerId);
            entity.HasIndex(e => e.IdentityReference);
            entity.HasIndex(e => e.CreatedAt);
        });

        // OrderItem entity
        modelBuilder.Entity<OrderItem>(entity =>
        {
            entity.HasKey(oi => oi.Id);
        });

        // CryptoPayment entity
        modelBuilder.Entity<CryptoPayment>(entity =>
        {
            entity.HasIndex(e => e.BTCPayInvoiceId);
            entity.HasIndex(e => e.WalletAddress);
        });

        // Bot entity
        modelBuilder.Entity<Bot>(entity =>
        {
            entity.HasIndex(e => e.BotKey).IsUnique();
            entity.HasIndex(e => e.Name);
            entity.HasIndex(e => e.Status);
            
            entity.HasMany(b => b.Metrics)
                  .WithOne(m => m.Bot)
                  .HasForeignKey(m => m.BotId)
                  .OnDelete(DeleteBehavior.Cascade);
            
            entity.HasMany(b => b.Sessions)
                  .WithOne(s => s.Bot)
                  .HasForeignKey(s => s.BotId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // BotMetric entity
        modelBuilder.Entity<BotMetric>(entity =>
        {
            entity.HasIndex(e => new { e.BotId, e.RecordedAt });
            entity.HasIndex(e => e.MetricType);
        });

        // BotSession entity
        modelBuilder.Entity<BotSession>(entity =>
        {
            entity.HasIndex(e => new { e.BotId, e.SessionIdentifier });
            entity.HasIndex(e => e.StartedAt);
            entity.HasIndex(e => e.LastActivityAt);
        });

        // Customer entity
        modelBuilder.Entity<Customer>(entity =>
        {
            entity.HasIndex(e => e.TelegramUserId).IsUnique();
            entity.HasIndex(e => e.TelegramUsername);
            entity.HasIndex(e => e.Email);
            entity.HasIndex(e => e.CreatedAt);
            entity.HasIndex(e => e.LastActiveAt);
            entity.HasIndex(e => e.DataRetentionDate);

            entity.HasMany(c => c.Orders)
                  .WithOne(o => o.Customer)
                  .HasForeignKey(o => o.CustomerId)
                  .OnDelete(DeleteBehavior.Restrict);

            entity.HasMany(c => c.Messages)
                  .WithOne(m => m.Customer)
                  .HasForeignKey(m => m.CustomerId)
                  .OnDelete(DeleteBehavior.Cascade);
        });

        // CustomerMessage entity
        modelBuilder.Entity<CustomerMessage>(entity =>
        {
            entity.HasOne(m => m.Customer)
                  .WithMany(c => c.Messages)
                  .HasForeignKey(m => m.CustomerId)
                  .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(m => m.Order)
                  .WithMany()
                  .HasForeignKey(m => m.OrderId)
                  .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(m => m.AdminUser)
                  .WithMany()
                  .HasForeignKey(m => m.AdminUserId)
                  .OnDelete(DeleteBehavior.SetNull);

            entity.HasOne(m => m.ParentMessage)
                  .WithMany(m => m.Replies)
                  .HasForeignKey(m => m.ParentMessageId)
                  .OnDelete(DeleteBehavior.Restrict);

            entity.HasIndex(e => new { e.CustomerId, e.CreatedAt });
            entity.HasIndex(e => e.OrderId);
            entity.HasIndex(e => e.Status);
            entity.HasIndex(e => e.Direction);
            entity.HasIndex(e => e.Type);
            entity.HasIndex(e => e.ScheduledFor);
            entity.HasIndex(e => e.ThreadId);
            entity.HasIndex(e => e.ParentMessageId);
        });

        // PushSubscription entity
        modelBuilder.Entity<PushSubscription>(entity =>
        {
            entity.HasOne(ps => ps.User)
                  .WithMany()
                  .HasForeignKey(ps => ps.UserId)
                  .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(ps => ps.Customer)
                  .WithMany()
                  .HasForeignKey(ps => ps.CustomerId)
                  .OnDelete(DeleteBehavior.Cascade);

            entity.HasIndex(e => e.Endpoint).IsUnique();
            entity.HasIndex(e => e.UserId);
            entity.HasIndex(e => e.CustomerId);
            entity.HasIndex(e => e.SubscribedAt);
            entity.HasIndex(e => e.IsActive);
        });

        // Review entity
        modelBuilder.Entity<Review>(entity =>
        {
            entity.HasOne(r => r.Product)
                  .WithMany(p => p.Reviews)
                  .HasForeignKey(r => r.ProductId)
                  .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(r => r.Customer)
                  .WithMany()
                  .HasForeignKey(r => r.CustomerId)
                  .OnDelete(DeleteBehavior.Cascade);

            entity.HasOne(r => r.Order)
                  .WithMany()
                  .HasForeignKey(r => r.OrderId)
                  .OnDelete(DeleteBehavior.Restrict);

            entity.HasOne(r => r.ApprovedByUser)
                  .WithMany()
                  .HasForeignKey(r => r.ApprovedByUserId)
                  .OnDelete(DeleteBehavior.SetNull);

            // Indexes for performance
            entity.HasIndex(e => e.ProductId);
            entity.HasIndex(e => e.CustomerId);
            entity.HasIndex(e => e.OrderId);
            entity.HasIndex(e => e.Rating);
            entity.HasIndex(e => e.IsApproved);
            entity.HasIndex(e => e.IsActive);
            entity.HasIndex(e => e.CreatedAt);
            
            // Composite indexes for common queries
            entity.HasIndex(e => new { e.ProductId, e.IsApproved, e.IsActive });
            entity.HasIndex(e => new { e.CustomerId, e.ProductId }).IsUnique(); // One review per customer per product
        });
    }
}