using Baya.Domain.Entities.Geography;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Baya.Infrastructure.Persistence.Configuration.GeographyConfig;

internal sealed class DistrictConfig : IEntityTypeConfiguration<District>
{
    public void Configure(EntityTypeBuilder<District> builder)
    {
        builder.ToTable("Districts", "geo");

        builder.Property(d => d.NameFa).HasMaxLength(150).IsRequired();
        builder.Property(d => d.NameEn).HasMaxLength(150).IsRequired();
        builder.Property(d => d.SortOrder).HasDefaultValue(0);
        builder.Property(d => d.IsActive).HasDefaultValue(true);

        builder.HasIndex(d => new { d.CityId, d.SortOrder });

        builder.HasOne(d => d.City)
            .WithMany(c => c.Districts)
            .HasForeignKey(d => d.CityId)
            .IsRequired();

        builder.HasQueryFilter(d => d.DeletedAt == null);

        builder.HasData(GeographySeed.Districts());
    }
}
