using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using IamService.Domain.AggregatesModel.VerificationAggregate;

namespace IamService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity configuration for IdentityVerification.
/// VI: Cấu hình entity cho IdentityVerification.
/// </summary>
public class IdentityVerificationEntityConfiguration : IEntityTypeConfiguration<IdentityVerification>
{
    public void Configure(EntityTypeBuilder<IdentityVerification> builder)
    {
        builder.ToTable("identity_verifications");

        builder.HasKey(v => v.Id);

        builder.Property(v => v.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        builder.Property("_userId")
            .HasColumnName("user_id")
            .IsRequired();

        builder.Property(v => v.TypeId)
            .HasColumnName("type_id")
            .IsRequired();

        builder.Property(v => v.StatusId)
            .HasColumnName("status_id")
            .IsRequired();

        builder.Property("_verificationData")
            .HasColumnName("verification_data")
            .HasMaxLength(1000);

        builder.Property("_verificationCodeHash")
            .HasColumnName("verification_code_hash")
            .HasMaxLength(100);

        builder.Property("_requestedAt")
            .HasColumnName("requested_at")
            .IsRequired();

        builder.Property("_verifiedAt")
            .HasColumnName("verified_at");

        builder.Property("_expiresAt")
            .HasColumnName("expires_at")
            .IsRequired();

        builder.Property("_attemptCount")
            .HasColumnName("attempt_count")
            .HasDefaultValue(0);

        builder.Property("_rejectionReason")
            .HasColumnName("rejection_reason")
            .HasMaxLength(500);

        builder.Property("_metadata")
            .HasColumnName("metadata")
            .HasColumnType("jsonb");

        // EN: Index for user verifications lookup
        // VI: Index để tìm kiếm verifications theo user
        builder.HasIndex("_userId")
            .HasDatabaseName("ix_identity_verifications_user_id");

        // EN: Composite index for checking active verification
        // VI: Composite index để kiểm tra verification đang hoạt động
        builder.HasIndex("_userId", "TypeId", "StatusId")
            .HasDatabaseName("ix_identity_verifications_user_type_status");

        // EN: Ignore domain events
        // VI: Bỏ qua domain events
        builder.Ignore(v => v.DomainEvents);
    }
}

/// <summary>
/// EN: Entity configuration for VerificationType.
/// VI: Cấu hình entity cho VerificationType.
/// </summary>
public class VerificationTypeEntityConfiguration : IEntityTypeConfiguration<VerificationType>
{
    public void Configure(EntityTypeBuilder<VerificationType> builder)
    {
        builder.ToTable("verification_types");

        builder.HasKey(t => t.Id);

        builder.Property(t => t.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        builder.Property(t => t.Name)
            .HasColumnName("name")
            .HasMaxLength(50)
            .IsRequired();

        // EN: Seed data
        // VI: Seed data
        builder.HasData(
            VerificationType.Email,
            VerificationType.Phone,
            VerificationType.Document,
            VerificationType.Identity
        );
    }
}

/// <summary>
/// EN: Entity configuration for VerificationStatus.
/// VI: Cấu hình entity cho VerificationStatus.
/// </summary>
public class VerificationStatusEntityConfiguration : IEntityTypeConfiguration<VerificationStatus>
{
    public void Configure(EntityTypeBuilder<VerificationStatus> builder)
    {
        builder.ToTable("verification_statuses");

        builder.HasKey(s => s.Id);

        builder.Property(s => s.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        builder.Property(s => s.Name)
            .HasColumnName("name")
            .HasMaxLength(50)
            .IsRequired();

        // EN: Seed data
        // VI: Seed data
        builder.HasData(
            VerificationStatus.Pending,
            VerificationStatus.InProgress,
            VerificationStatus.Verified,
            VerificationStatus.Rejected,
            VerificationStatus.Expired,
            VerificationStatus.Cancelled
        );
    }
}
