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

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using CatalogService.Domain.AggregatesModel.ProductAggregate;

namespace CatalogService.Infrastructure.EntityConfigurations;

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

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

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

        builder.Property(c => c.ShopId)
            .HasField("_shopId")
            .HasColumnName("shop_id")
            .IsRequired();

        builder.Property(c => c.Name)
            .HasField("_name")
            .HasColumnName("name")
            .HasMaxLength(200)
            .IsRequired();

        builder.Property(c => c.Description)
            .HasField("_description")
            .HasColumnName("description")
            .HasMaxLength(1000);

        builder.Property(c => c.ParentId)
            .HasField("_parentId")
            .HasColumnName("parent_id");

        builder.Property(c => c.ImageUrl)
            .HasField("_imageUrl")
            .HasColumnName("image_url")
            .HasMaxLength(500);

        builder.Property(c => c.DisplayOrder)
            .HasField("_displayOrder")
            .HasColumnName("display_order")
            .HasDefaultValue(0);

        builder.Property(c => c.IsActive)
            .HasField("_isActive")
            .HasColumnName("is_active")
            .HasDefaultValue(true);

        builder.Property(c => c.CreatedAt)
            .HasField("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        builder.Property(c => c.UpdatedAt)
            .HasField("_updatedAt")
            .HasColumnName("updated_at");

        // EN: Indexes
        // VI: Indexes
        builder.HasIndex(c => c.ShopId).HasDatabaseName("ix_categories_shop_id");
        builder.HasIndex(c => c.ParentId).HasDatabaseName("ix_categories_parent_id");
        builder.HasIndex(c => c.DisplayOrder).HasDatabaseName("ix_categories_display_order");
    }
}
