// EN: Entity type configuration for Therapist.
// VI: Cấu hình entity type cho Therapist.

using BookingService.Domain.AggregatesModel.TherapistAggregate;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace BookingService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity type configuration for Therapist aggregate root.
/// VI: Cấu hình entity type cho aggregate root Therapist.
/// </summary>
public class TherapistEntityTypeConfiguration : IEntityTypeConfiguration<Therapist>
{
    public void Configure(EntityTypeBuilder<Therapist> builder)
    {
        builder.ToTable("therapists");

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

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

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

        builder.Property(t => t.Specialties)
            .HasField("_specialties")
            .HasColumnName("specialties")
            .HasColumnType("text[]");

        builder.Property(t => t.WorkingHours)
            .HasField("_workingHours")
            .HasColumnName("working_hours")
            .HasColumnType("jsonb")
            .IsRequired();

        builder.Property(t => t.IsActive)
            .HasField("_isActive")
            .HasColumnName("is_active")
            .IsRequired();

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

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

        // EN: Ignore domain events from persistence / VI: Bỏ qua domain events khi persist
        builder.Ignore(t => t.DomainEvents);

        // EN: Indexes / VI: Indexes
        builder.HasIndex(t => t.ShopId)
            .HasDatabaseName("ix_therapists_shop_id");

        builder.HasIndex(t => new { t.ShopId, t.IsActive })
            .HasDatabaseName("ix_therapists_shop_active");
    }
}
