using DeepDrftModels.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace DeepDrftData.Data.Configurations;

/// <summary>
/// EF configuration for the append-only <c>share_event</c> log (Phase 16 §4.2). Plain immutable-fact
/// entity. Indexed on the target key so per-target share tallies stay cheap.
/// </summary>
public class ShareEventConfiguration : IEntityTypeConfiguration<ShareEvent>
{
    public void Configure(EntityTypeBuilder<ShareEvent> builder)
    {
        builder.ToTable("share_event");

        builder.HasKey(e => e.Id);
        builder.Property(e => e.Id).HasColumnName("id");

        builder.Property(e => e.TargetType)
            .IsRequired()
            .HasConversion<string>()
            .HasMaxLength(20)
            .HasColumnName("target_type");

        builder.Property(e => e.TargetKey)
            .IsRequired()
            .HasMaxLength(100)
            .HasColumnName("target_key");

        builder.Property(e => e.Channel)
            .IsRequired()
            .HasConversion<string>()
            .HasMaxLength(20)
            .HasColumnName("channel");

        builder.Property(e => e.AnonId)
            .HasMaxLength(64)
            .HasColumnName("anon_id");

        builder.Property(e => e.CreatedAt)
            .IsRequired()
            .HasColumnName("created_at");

        builder.HasIndex(e => e.TargetKey).HasDatabaseName("IX_share_event_target_key");
        builder.HasIndex(e => e.AnonId).HasDatabaseName("IX_share_event_anon_id");
    }
}
