using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WhatsAppService.Domain.AggregatesModel.SampleAggregate;

namespace WhatsAppService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: EF Core configuration for Sample entity.
/// VI: Cấu hình EF Core cho entity Sample.
/// </summary>
public class SampleEntityTypeConfiguration : IEntityTypeConfiguration<Sample>
{
    public void Configure(EntityTypeBuilder<Sample> builder)
    {
        // EN: Table name / VI: Tên bảng
        builder.ToTable("samples");

        // EN: Primary key / VI: Khóa chính
        builder.HasKey(s => s.Id);

        // EN: Ignore domain events (not persisted)
        // VI: Bỏ qua domain events (không lưu)
        builder.Ignore(s => s.DomainEvents);

        // EN: Properties / VI: Các thuộc tính
        builder.Property(s => s.Id)
            .HasColumnName("id")
            .IsRequired();

        builder.Property<string>("_name")
            .HasColumnName("name")
            .HasMaxLength(200)
            .IsRequired();

        builder.Property<string?>("_description")
            .HasColumnName("description")
            .HasMaxLength(1000);

        builder.Property<DateTime>("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        builder.Property<DateTime?>("_updatedAt")
            .HasColumnName("updated_at");

        // EN: Status relationship / VI: Quan hệ với Status
        builder.Property(s => s.StatusId)
            .HasColumnName("status_id")
            .IsRequired();

        builder.HasOne(s => s.Status)
            .WithMany()
            .HasForeignKey(s => s.StatusId)
            .OnDelete(DeleteBehavior.Restrict);

        // EN: Indexes / VI: Các index
        builder.HasIndex("_name");
        builder.HasIndex(s => s.StatusId);
        builder.HasIndex("_createdAt");
    }
}
