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

public class IdempotencyRecordConfiguration : IEntityTypeConfiguration<IdempotencyRecord>
{
    public void Configure(EntityTypeBuilder<IdempotencyRecord> builder)
    {
        builder.ToTable("idempotency_records");
        builder.HasKey(r => r.Id);
        builder.Property(r => r.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
        builder.Property(r => r.IdempotencyKey).HasColumnName("idempotency_key").HasMaxLength(100).IsRequired();
        builder.Property(r => r.OperationName).HasColumnName("operation_name").HasMaxLength(100).IsRequired();
        builder.Property(r => r.ResourceId).HasColumnName("resource_id").IsRequired();
        builder.Property(r => r.HttpStatusCode).HasColumnName("http_status_code").IsRequired();
        builder.Property(r => r.ResponseBodyJson).HasColumnName("response_body_json").HasColumnType("jsonb").IsRequired();
        builder.Property(r => r.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");
        builder.Property(r => r.ExpiresAt).HasColumnName("expires_at").IsRequired();

        builder.HasIndex(r => new { r.IdempotencyKey, r.OperationName })
            .IsUnique()
            .HasDatabaseName("ix_idempotency_records_key_operation");

        builder.HasIndex(r => r.ExpiresAt)
            .HasDatabaseName("ix_idempotency_records_expires_at");
    }
}