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

namespace CMSMicroservice.Infrastructure.Persistence.Configurations.Geography;

public class StateConfiguration : IEntityTypeConfiguration<State>
{
    public void Configure(EntityTypeBuilder<State> builder)
    {
        builder.ToTable("States", "GMS");

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

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

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

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

        builder.Property(s => s.StateCode)
            .IsRequired()
            .HasMaxLength(10);

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

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

        builder.Property(s => s.Type)
            .IsRequired()
            .HasMaxLength(100)
            .HasColumnType("nvarchar(100)")
         ;

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

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

        builder.Property(s => s.CountryId)
            .IsRequired();

        // Index
        builder.HasIndex(s => s.CountryId)
            .HasDatabaseName("IX_States_CountryId");

        // Relationships
        builder.HasOne(s => s.Country)
            .WithMany(c => c.States)
            .HasForeignKey(s => s.CountryId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasMany(s => s.Cities)
            .WithOne(c => c.State)
            .HasForeignKey(c => c.StateId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
