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

namespace CMSMicroservice.Infrastructure.Persistence.Configurations.Geography;

public class CountryConfiguration : IEntityTypeConfiguration<Country>
{
    public void Configure(EntityTypeBuilder<Country> builder)
    {
        builder.ToTable("Countries", "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.Iso3)
            .IsRequired()
            .HasMaxLength(3);

        builder.Property(c => c.Iso2)
            .IsRequired()
            .HasMaxLength(2);

        builder.Property(c => c.NumericCode)
            .IsRequired()
            .HasMaxLength(10);

        builder.Property(c => c.PhoneCode)
            .IsRequired()
            .HasMaxLength(10);

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

        builder.Property(c => c.Currency)
            .IsRequired()
            .HasMaxLength(10);

        builder.Property(c => c.CurrencyName)
            .IsRequired()
            .HasMaxLength(100)
            .HasColumnType("nvarchar(100)")
            ;

        builder.Property(c => c.CurrencySymbol)
            .IsRequired()
            .HasMaxLength(10);

        builder.Property(c => c.Tld)
            .IsRequired()
            .HasMaxLength(10);

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

        builder.Property(c => c.Region)
            .IsRequired()
            .HasMaxLength(100)
            .HasColumnType("nvarchar(100)")
            ;

        builder.Property(c => c.Subregion)
            .IsRequired()
            .HasMaxLength(100)
            .HasColumnType("nvarchar(100)")
           ;

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

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

        builder.Property(c => c.Emoji)
            .IsRequired()
            .HasMaxLength(10);

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

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

        // Relationships
        builder.HasMany(c => c.States)
            .WithOne(s => s.Country)
            .HasForeignKey(s => s.CountryId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
