// EN: Product entity configuration.
// VI: Cấu hình entity Product.

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using CatalogService.Domain.AggregatesModel.ProductAggregate;
using System.Text.Json;

namespace CatalogService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for Product entity.
/// VI: Cấu hình EF Core cho Product entity.
/// </summary>
public class ProductEntityTypeConfiguration : IEntityTypeConfiguration<Product>
{
    public void Configure(EntityTypeBuilder<Product> builder)
    {
        builder.ToTable("products");

        builder.HasKey(p => p.Id);

        builder.Property(p => p.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        builder.Property<Guid>("_shopId")
            .HasColumnName("shop_id")
            .IsRequired();

        builder.Property<string>("_name")
            .HasColumnName("name")
            .HasMaxLength(255)
            .IsRequired();

        builder.Property<string?>("_description")
            .HasColumnName("description")
            .HasMaxLength(2000);

        builder.Property<decimal>("_price")
            .HasColumnName("price")
            .HasColumnType("decimal(18,2)")
            .IsRequired();

        builder.Property(p => p.TypeId)
            .HasColumnName("type_id")
            .IsRequired();

        // EN: JSONB configuration for polymorphic attributes
        // VI: Cấu hình JSONB cho attributes đa hình
        builder.Property<JsonDocument?>("_attributes")
            .HasColumnName("attributes")
            .HasColumnType("jsonb");

        builder.Property<string?>("_imageUrl")
            .HasColumnName("image_url")
            .HasMaxLength(500);

        builder.Property<string?>("_sku")
            .HasColumnName("sku")
            .HasMaxLength(100);

        builder.Property<bool>("_isActive")
            .HasColumnName("is_active")
            .HasDefaultValue(true);

        builder.Property<DateTime>("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        builder.Property<DateTime?>("_updatedAt")
            .HasColumnName("updated_at");

        // EN: Indexes
        // VI: Indexes
        builder.HasIndex("_shopId").HasDatabaseName("ix_products_shop_id");
        builder.HasIndex(p => p.TypeId).HasDatabaseName("ix_products_type_id");
        builder.HasIndex("_sku").HasDatabaseName("ix_products_sku");
        builder.HasIndex("_isActive").HasDatabaseName("ix_products_is_active");

        // EN: Ignore calculated properties
        // VI: Bỏ qua các properties được tính toán
        builder.Ignore(p => p.ShopId);
        builder.Ignore(p => p.Name);
        builder.Ignore(p => p.Description);
        builder.Ignore(p => p.Price);
        builder.Ignore(p => p.Type);
        builder.Ignore(p => p.Attributes);
        builder.Ignore(p => p.ImageUrl);
        builder.Ignore(p => p.Sku);
        builder.Ignore(p => p.IsActive);
        builder.Ignore(p => p.CreatedAt);
        builder.Ignore(p => p.UpdatedAt);
    }
}
