using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class ExternalResourceIdentifierConfiguration : IEntityTypeConfiguration<ExternalResourceIdentifier>
{
    public void Configure(EntityTypeBuilder<ExternalResourceIdentifier> builder)
    {
        builder.ToTable("external_resource_identifiers");
        builder.HasKey(e => e.Id);
        builder.Property(e => e.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(e => e.ResourceType).HasColumnName("resource_type").HasMaxLength(20).IsRequired()
            .HasConversion(v => v.ToDbString(), v => ExternalResourceTypeExtensions.FromDbString(v));
        builder.Property(e => e.InternalId).HasColumnName("internal_id").IsRequired();
        builder.Property(e => e.System).HasColumnName("system").HasMaxLength(500).IsRequired();
        builder.Property(e => e.Value).HasColumnName("value").HasMaxLength(200).IsRequired();
        builder.Property(e => e.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");

        builder.HasIndex(e => new { e.ResourceType, e.System, e.Value }).IsUnique();
        builder.HasIndex(e => new { e.ResourceType, e.InternalId });
    }
}