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

namespace Baya.Infrastructure.Persistence.Configuration.GeographyConfig;

internal sealed class CityConfig : IEntityTypeConfiguration<City>
{
    public void Configure(EntityTypeBuilder<City> builder)
    {
        builder.ToTable("Cities", "geo");

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

        // Ordered cascading lookup: cities for a province in sort order.
        builder.HasIndex(c => new { c.ProvinceId, c.SortOrder });

        builder.HasOne(c => c.Province)
            .WithMany(p => p.Cities)
            .HasForeignKey(c => c.ProvinceId)
            .IsRequired();

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

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