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

namespace IamService.Infrastructure.EntityConfigurations;

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

        builder.Property<string>("_name").HasColumnName("Name").HasMaxLength(200).IsRequired();
        builder.Property<string?>("_description").HasColumnName("Description").HasMaxLength(1000);
        builder.Property<Guid>("_ownerId").HasColumnName("OwnerId").IsRequired();
        builder.Property<string>("_scope").HasColumnName("Scope").HasMaxLength(200).IsRequired();
        builder.Property<DateTime>("_createdAt").HasColumnName("CreatedAt").IsRequired();
        builder.Property<DateTime?>("_startedAt").HasColumnName("StartedAt");
        builder.Property<DateTime>("_dueDate").HasColumnName("DueDate").IsRequired();
        builder.Property<DateTime?>("_completedAt").HasColumnName("CompletedAt");

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

        builder.HasMany(x => x.Items)
            .WithOne()
            .HasForeignKey("_reviewId")
            .OnDelete(DeleteBehavior.Cascade);

        builder.Ignore(x => x.DomainEvents);
        builder.HasIndex("_ownerId").HasDatabaseName("IX_AccessReviews_OwnerId");
    }
}

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

        builder.Property<Guid>("_reviewId").HasColumnName("ReviewId").IsRequired();
        builder.Property<Guid>("_userId").HasColumnName("UserId").IsRequired();
        builder.Property<string>("_resourceType").HasColumnName("ResourceType").HasMaxLength(100).IsRequired();
        builder.Property<Guid>("_resourceId").HasColumnName("ResourceId").IsRequired();
        builder.Property<string>("_permission").HasColumnName("Permission").HasMaxLength(100).IsRequired();
        builder.Property<Guid?>("_reviewedByUserId").HasColumnName("ReviewedByUserId");
        builder.Property<DateTime?>("_reviewedAt").HasColumnName("ReviewedAt");
        builder.Property<string?>("_comments").HasColumnName("Comments").HasMaxLength(500);

        builder.Property<ReviewDecision>("_decision")
            .HasColumnName("DecisionId")
            .HasConversion(v => v.Id, v => ReviewDecision.FromId(v) ?? ReviewDecision.Pending);

        builder.Ignore(x => x.DomainEvents);
        builder.HasIndex("_userId").HasDatabaseName("IX_AccessReviewItems_UserId");
    }
}
