using ColaFlow.Modules.Identity.Domain.Aggregates.Users;
using ColaFlow.Modules.Identity.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace ColaFlow.Modules.Identity.Infrastructure.Persistence.Configurations;

public class EmailVerificationTokenConfiguration : IEntityTypeConfiguration<EmailVerificationToken>
{
    public void Configure(EntityTypeBuilder<EmailVerificationToken> builder)
    {
        builder.ToTable("email_verification_tokens");

        // Primary Key
        builder.HasKey(t => t.Id);
        builder.Property(t => t.Id).HasColumnName("id");

        // User ID (foreign key) - stored as Guid, mapped to UserId value object
        builder.Property(t => t.UserId)
            .HasConversion(
                userId => (Guid)userId,
                value => UserId.Create(value))
            .IsRequired()
            .HasColumnName("user_id");

        // Token hash
        builder.Property(t => t.TokenHash)
            .HasMaxLength(64)
            .IsRequired()
            .HasColumnName("token_hash");

        // Timestamps
        builder.Property(t => t.ExpiresAt)
            .IsRequired()
            .HasColumnName("expires_at");

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

        builder.Property(t => t.VerifiedAt)
            .HasColumnName("verified_at");

        // Indexes
        builder.HasIndex(t => t.TokenHash)
            .HasDatabaseName("ix_email_verification_tokens_token_hash");

        builder.HasIndex(t => t.UserId)
            .HasDatabaseName("ix_email_verification_tokens_user_id");
    }
}
