// EN: BaristaQueueItem entity configuration.
// VI: Cau hinh entity BaristaQueueItem.

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using FnbEngine.Domain.AggregatesModel.BaristaAggregate;

namespace FnbEngine.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity configuration for BaristaQueueItem.
/// VI: Cau hinh entity cho BaristaQueueItem.
/// </summary>
public class BaristaQueueItemEntityTypeConfiguration : IEntityTypeConfiguration<BaristaQueueItem>
{
    public void Configure(EntityTypeBuilder<BaristaQueueItem> builder)
    {
        builder.ToTable("barista_queue_items");

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

        builder.Property(bq => bq.Id)
            .HasColumnName("id")
            .IsRequired();

        // EN: Shop ID
        // VI: ID shop
        builder.Property("_shopId")
            .HasColumnName("shop_id")
            .IsRequired();

        // EN: Order ID
        // VI: ID don hang
        builder.Property("_orderId")
            .HasColumnName("order_id")
            .IsRequired();

        // EN: Order item ID
        // VI: ID item don hang
        builder.Property("_orderItemId")
            .HasColumnName("order_item_id")
            .IsRequired();

        // EN: Drink name
        // VI: Ten do uong
        builder.Property("_drinkName")
            .HasColumnName("drink_name")
            .HasMaxLength(200)
            .IsRequired();

        // EN: Customizations as JSON
        // VI: Tuy chinh dang JSON
        builder.Property("_customizations")
            .HasColumnName("customizations")
            .HasColumnType("jsonb");

        // EN: Priority (higher = more urgent)
        // VI: Do uu tien (cao = gap hon)
        builder.Property("_priority")
            .HasColumnName("priority")
            .IsRequired()
            .HasDefaultValue(0);

        // EN: Status ID (1=Queued, 2=Preparing, 3=Ready, 4=Delivered, 5=Cancelled)
        // VI: ID trang thai (1=Cho, 2=Dang pha, 3=San sang, 4=Da giao, 5=Da huy)
        builder.Property("_statusId")
            .HasColumnName("status_id")
            .IsRequired()
            .HasDefaultValue(1);

        // EN: Assigned barista name
        // VI: Ten barista duoc giao
        builder.Property("_assignedTo")
            .HasColumnName("assigned_to")
            .HasMaxLength(100);

        // EN: Estimated preparation time in minutes
        // VI: Thoi gian pha che du kien tinh bang phut
        builder.Property("_estimatedMinutes")
            .HasColumnName("estimated_minutes")
            .IsRequired()
            .HasDefaultValue(5);

        // EN: Timestamps
        // VI: Timestamps
        builder.Property("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        builder.Property("_startedAt")
            .HasColumnName("started_at");

        builder.Property("_completedAt")
            .HasColumnName("completed_at");

        // EN: Ignore domain events (not persisted)
        // VI: Bo qua domain events (khong luu)
        builder.Ignore(bq => bq.DomainEvents);

        // EN: Indexes for common queries
        // VI: Indexes cho cac query thuong dung

        // EN: Index for shop queue listing (active items sorted by priority)
        // VI: Index cho danh sach hang doi shop (item dang hoat dong sap xep theo uu tien)
        builder.HasIndex("_shopId", "_statusId", "_priority")
            .HasDatabaseName("ix_barista_queue_shop_status_priority");

        // EN: Index for order lookup
        // VI: Index cho tra cuu don hang
        builder.HasIndex("_orderId")
            .HasDatabaseName("ix_barista_queue_order_id");

        // EN: Index for created time sorting
        // VI: Index cho sap xep theo thoi gian tao
        builder.HasIndex("_createdAt")
            .HasDatabaseName("ix_barista_queue_created_at");
    }
}
