// 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(i => i.ProductId).HasField("_productId").HasColumnName("product_id").IsRequired();
        builder.Property(i => i.ShopId).HasField("_shopId").HasColumnName("shop_id").IsRequired();
        builder.Property<string?>("_name").HasColumnName("name").HasMaxLength(200);
        builder.Property(i => i.ItemTypeId).HasColumnName("item_type_id").IsRequired().HasDefaultValue(2);
        builder.Property<string>("_unit").HasColumnName("unit").HasMaxLength(20).IsRequired().HasDefaultValue("pcs");
        builder.Property<decimal>("_costPerUnit").HasColumnName("cost_per_unit").HasPrecision(18, 4).HasDefaultValue(0m);
        builder.Property<string?>("_supplierName").HasColumnName("supplier_name").HasMaxLength(200);
        builder.Property<DateTime?>("_expiryDate").HasColumnName("expiry_date");
        builder.Property(i => i.Quantity).HasField("_quantity").HasColumnName("quantity").IsRequired();
        builder.Property(i => i.ReservedQuantity).HasField("_reservedQuantity").HasColumnName("reserved_quantity").IsRequired();
        builder.Property(i => i.ReorderLevel).HasField("_reorderLevel").HasColumnName("reorder_level").HasDefaultValue(10);
        builder.Property(i => i.UpdatedAt).HasField("_updatedAt").HasColumnName("updated_at");

        // EN: Ignore computed/navigation properties not stored directly.
        // VI: Bỏ qua các property tính toán/navigation không lưu trực tiếp.
        builder.Ignore(i => i.CreatedAt);
        builder.Ignore(i => i.Name);
        builder.Ignore(i => i.ItemType);
        builder.Ignore(i => i.Unit);
        builder.Ignore(i => i.CostPerUnit);
        builder.Ignore(i => i.SupplierName);
        builder.Ignore(i => i.ExpiryDate);

        // 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<string?>("_invoiceImageUrl").HasColumnName("invoice_image_url").HasMaxLength(1000);
            txn.Property<decimal?>("_unitCost").HasColumnName("unit_cost").HasPrecision(18, 4);
            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.InvoiceImageUrl);
            txn.Ignore(t => t.UnitCost);
            txn.Ignore(t => t.CreatedAt);
        });

        builder.HasIndex(i => i.ProductId).HasDatabaseName("ix_inventory_product_id");
        builder.HasIndex(i => i.ShopId).HasDatabaseName("ix_inventory_shop_id");

        // EN: AvailableQuantity is computed (Quantity - ReservedQuantity), not stored.
        // VI: AvailableQuantity được tính toán (Quantity - ReservedQuantity), không lưu.
        builder.Ignore(i => i.AvailableQuantity);
    }
}
