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

namespace FnbEngine.Infrastructure.EntityConfigurations;

public class TableEntityTypeConfiguration : IEntityTypeConfiguration<Table>
{
    public void Configure(EntityTypeBuilder<Table> builder)
    {
        builder.ToTable("tables");

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

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

        // EN: Map read-only public properties to their backing fields
        // so EF Core can translate LINQ queries (e.g. t.ShopId == value).
        // VI: Map các property public chỉ đọc sang backing field tương ứng
        // để EF Core có thể dịch LINQ query (ví dụ: t.ShopId == value).
        builder.Property(t => t.ShopId)
            .HasField("_shopId")
            .HasColumnName("shop_id")
            .IsRequired();

        builder.Property(t => t.TableNumber)
            .HasField("_tableNumber")
            .HasColumnName("table_number")
            .HasMaxLength(20)
            .IsRequired();

        builder.Property(t => t.Capacity)
            .HasField("_capacity")
            .HasColumnName("capacity")
            .IsRequired();

        builder.Property(t => t.Zone)
            .HasField("_zone")
            .HasColumnName("zone")
            .HasMaxLength(100);

        builder.Property(t => t.StatusId)
            .HasColumnName("status_id")
            .IsRequired();

        builder.Property(t => t.PositionX)
            .HasField("_positionX")
            .HasColumnName("position_x");

        builder.Property(t => t.PositionY)
            .HasField("_positionY")
            .HasColumnName("position_y");

        builder.Property(t => t.QrToken)
            .HasField("_qrToken")
            .HasColumnName("qr_token")
            .HasMaxLength(64);

        builder.Property(t => t.HourlyRate)
            .HasField("_hourlyRate")
            .HasColumnName("hourly_rate")
            .HasPrecision(18, 2)
            .HasDefaultValue(0m);

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

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

        // EN: Indexes / VI: Các index
        builder.HasIndex(t => t.ShopId)
            .HasDatabaseName("ix_tables_shop_id");

        builder.HasIndex(t => new { t.ShopId, t.TableNumber })
            .HasDatabaseName("ix_tables_shop_table_number")
            .IsUnique();

        builder.HasIndex(t => t.QrToken)
            .HasDatabaseName("ix_tables_qr_token")
            .IsUnique()
            .HasFilter("qr_token IS NOT NULL");

        // EN: Ignore the Status navigation (enumeration loaded via StatusId)
        // VI: Bỏ qua navigation Status (enumeration được load qua StatusId)
        builder.Ignore(t => t.Status);
    }
}
