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

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using InventoryService.Domain.AggregatesModel.InventoryAggregate;

namespace InventoryService.Infrastructure.EntityConfigurations;

public class InventoryItemEntityTypeConfiguration : IEntityTypeConfiguration<InventoryItem>
{
    public void Configure(EntityTypeBuilder<InventoryItem> builder)
    {
        builder.ToTable("inventory_items");

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

        builder.Property<Guid>("_productId").HasColumnName("product_id").IsRequired();
        builder.Property<Guid>("_shopId").HasColumnName("shop_id").IsRequired();
        builder.Property<int>("_quantity").HasColumnName("quantity").IsRequired();
        builder.Property<int>("_reservedQuantity").HasColumnName("reserved_quantity").IsRequired();
        builder.Property<int>("_reorderLevel").HasColumnName("reorder_level").HasDefaultValue(10);
        builder.Property<DateTime?>("_updatedAt").HasColumnName("updated_at");

        // Owned InventoryTransaction collection
        builder.OwnsMany(i => i.Transactions, txn =>
        {
            txn.ToTable("inventory_transactions");
            txn.WithOwner().HasForeignKey("InventoryItemId");
            txn.Property<Guid>("InventoryItemId").HasColumnName("inventory_item_id");
            txn.HasKey(t => t.Id);
            txn.Property(t => t.Id).HasColumnName("id").ValueGeneratedNever();
            txn.Property(t => t.TypeId).HasColumnName("type_id").IsRequired();
            txn.Property<int>("_quantity").HasColumnName("quantity").IsRequired();
            txn.Property<Guid?>("_referenceId").HasColumnName("reference_id");
            txn.Property<string?>("_notes").HasColumnName("notes").HasMaxLength(500);
            txn.Property<DateTime>("_createdAt").HasColumnName("created_at").IsRequired();

            txn.Ignore(t => t.Type);
            txn.Ignore(t => t.Quantity);
            txn.Ignore(t => t.ReferenceId);
            txn.Ignore(t => t.Notes);
            txn.Ignore(t => t.CreatedAt);
        });

        builder.HasIndex("_productId").HasDatabaseName("ix_inventory_product_id");
        builder.HasIndex("_shopId").HasDatabaseName("ix_inventory_shop_id");

        builder.Ignore(i => i.ProductId);
        builder.Ignore(i => i.ShopId);
        builder.Ignore(i => i.Quantity);
        builder.Ignore(i => i.ReservedQuantity);
        builder.Ignore(i => i.AvailableQuantity);
        builder.Ignore(i => i.ReorderLevel);
        builder.Ignore(i => i.UpdatedAt);
    }
}
