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

namespace CMSMicroservice.Infrastructure.Persistence.Configurations.Geography;

public class CityConfiguration : IEntityTypeConfiguration<City>
{
    public void Configure(EntityTypeBuilder<City> builder)
    {
        builder.ToTable("Cities", "GMS");

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

        builder.Property(c => c.Id)
            .ValueGeneratedOnAdd();

        builder.Property(c => c.ExternalId)
            .IsRequired();

        builder.Property(c => c.Name)
            .IsRequired()
            .HasMaxLength(500)
            .HasColumnType("nvarchar(500)")
            ;

        builder.Property(c => c.Latitude)
            .IsRequired()
            .HasMaxLength(50);

        builder.Property(c => c.Longitude)
            .IsRequired()
            .HasMaxLength(50);

        builder.Property(c => c.Native)
            .IsRequired()
            .HasMaxLength(500)
            .HasColumnType("nvarchar(500)")
            
            .HasDefaultValue("");

        builder.Property(c => c.IsDeleted)
            .IsRequired()
            .HasDefaultValue(false);

        builder.Property(c => c.StateId)
            .IsRequired();

        // Index
        builder.HasIndex(c => c.StateId)
            .HasDatabaseName("IX_Cities_StateId");

        // Relationships
        builder.HasOne(c => c.State)
            .WithMany(s => s.Cities)
            .HasForeignKey(c => c.StateId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
