using Lada.Framework.Data.Domains.Premises;
using Lada.Framework.Data.Mappings;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Lada.Framework.Data.Mappings.EntityMappings.Premises
{
    public class PremiseScoreMap : IEntityMappingConfiguration<PremiseScore>
    {
        public void Map(EntityTypeBuilder<PremiseScore> builder)
        {
            builder.ToTable("PremiseScores");

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

            // Properties
            builder.Property(x => x.PremiseId).IsRequired();
            builder.Property(x => x.CriteriaId).IsRequired();
            builder.Property(x => x.TextValue).HasMaxLength(500);
            builder.Property(x => x.NumericValue).HasColumnType("decimal(18,2)");
            builder.Property(x => x.Score).IsRequired();
            builder.Property(x => x.Note).HasMaxLength(1000);

            // Relationships
            builder.HasOne(x => x.Premise)
                .WithMany(x => x.Scores)
                .HasForeignKey(x => x.PremiseId)
                .OnDelete(DeleteBehavior.Cascade);

            builder.HasOne(x => x.Criteria)
                .WithMany()
                .HasForeignKey(x => x.CriteriaId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.HasOne(x => x.SelectedOption)
                .WithMany()
                .HasForeignKey(x => x.SelectedOptionId)
                .OnDelete(DeleteBehavior.SetNull);

            // Unique index: mỗi mặt bằng chỉ 1 điểm/tiêu chí
            builder.HasIndex(x => new { x.PremiseId, x.CriteriaId })
                .IsUnique()
                .HasDatabaseName("IX_PremiseScores_PremiseId_CriteriaId");
        }
    }
}
