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

namespace IamService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity configuration for PrivilegedAccessGrant.
/// VI: Cấu hình entity cho PrivilegedAccessGrant.
/// </summary>
public class PrivilegedAccessEntityConfiguration : IEntityTypeConfiguration<PrivilegedAccessGrant>
{
    public void Configure(EntityTypeBuilder<PrivilegedAccessGrant> builder)
    {
        builder.ToTable("PrivilegedAccessGrants");
        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id).ValueGeneratedNever();

        builder.Property<Guid>("_userId").HasColumnName("UserId").IsRequired();
        builder.Property<Guid>("_roleId").HasColumnName("RoleId").IsRequired();
        builder.Property<string>("_resourceScope").HasColumnName("ResourceScope").HasMaxLength(200).IsRequired();
        builder.Property<string?>("_reason").HasColumnName("Reason").HasMaxLength(500);
        builder.Property<Guid>("_grantedByUserId").HasColumnName("GrantedByUserId").IsRequired();
        builder.Property<DateTime>("_createdAt").HasColumnName("CreatedAt").IsRequired();
        builder.Property<DateTime>("_startsAt").HasColumnName("StartsAt").IsRequired();
        builder.Property<DateTime>("_expiresAt").HasColumnName("ExpiresAt").IsRequired();
        builder.Property<DateTime?>("_revokedAt").HasColumnName("RevokedAt");
        builder.Property<Guid?>("_revokedByUserId").HasColumnName("RevokedByUserId");
        builder.Property<string?>("_revocationReason").HasColumnName("RevocationReason").HasMaxLength(500);

        builder.Property<PrivilegedAccessStatus>("_status")
            .HasColumnName("StatusId")
            .HasConversion(v => v.Id, v => PrivilegedAccessStatus.FromId(v) ?? PrivilegedAccessStatus.Pending);

        builder.Ignore(x => x.DomainEvents);
        builder.HasIndex("_userId").HasDatabaseName("IX_PrivilegedAccessGrants_UserId");
        builder.HasIndex("_userId", "_roleId", "_status").HasDatabaseName("IX_PrivilegedAccessGrants_Active");
    }
}
