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

namespace IamService.Infrastructure.EntityConfigurations;

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

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

        builder.Property(x => x.Id)
            .ValueGeneratedNever();

        builder.Property<Guid>("_requesterId")
            .HasColumnName("RequesterId")
            .IsRequired();

        builder.Property<string>("_resourceType")
            .HasColumnName("ResourceType")
            .HasMaxLength(100)
            .IsRequired();

        builder.Property<Guid>("_resourceId")
            .HasColumnName("ResourceId")
            .IsRequired();

        builder.Property<string>("_requestedPermission")
            .HasColumnName("RequestedPermission")
            .HasMaxLength(100)
            .IsRequired();

        builder.Property<string?>("_justification")
            .HasColumnName("Justification")
            .HasMaxLength(2000);

        builder.Property<DateTime>("_createdAt")
            .HasColumnName("CreatedAt")
            .IsRequired();

        builder.Property<DateTime?>("_submittedAt")
            .HasColumnName("SubmittedAt");

        builder.Property<DateTime?>("_resolvedAt")
            .HasColumnName("ResolvedAt");

        builder.Property<DateTime?>("_expiresAt")
            .HasColumnName("ExpiresAt");

        // EN: Status with value conversion to use proper static instances
        // VI: Status với value conversion để dùng đúng static instances
        builder.Property<AccessRequestStatus>("_status")
            .HasColumnName("StatusId")
            .HasConversion(
                v => v.Id,
                v => AccessRequestStatus.FromId(v) ?? AccessRequestStatus.Draft);

        // EN: Priority with value conversion
        // VI: Priority với value conversion
        builder.Property<AccessRequestPriority>("_priority")
            .HasColumnName("PriorityId")
            .HasConversion(
                v => v.Id,
                v => AccessRequestPriority.FromId(v) ?? AccessRequestPriority.Medium);

        // EN: Approvers collection
        // VI: Collection approvers
        builder.HasMany(x => x.Approvers)
            .WithOne()
            .HasForeignKey("_requestId")
            .OnDelete(DeleteBehavior.Cascade);

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

        // EN: Indexes
        // VI: Indexes
        builder.HasIndex("_requesterId").HasDatabaseName("IX_AccessRequests_RequesterId");
        builder.HasIndex("_resourceType", "_resourceId").HasDatabaseName("IX_AccessRequests_Resource");
    }
}


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

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

        builder.Property(x => x.Id)
            .ValueGeneratedNever();

        builder.Property<Guid>("_requestId")
            .HasColumnName("RequestId")
            .IsRequired();

        builder.Property<Guid>("_userId")
            .HasColumnName("UserId")
            .IsRequired();

        builder.Property<int>("_order")
            .HasColumnName("ApprovalOrder")
            .IsRequired();

        builder.Property<DateTime?>("_respondedAt")
            .HasColumnName("RespondedAt");

        builder.Property<string?>("_comments")
            .HasColumnName("Comments")
            .HasMaxLength(1000);

        // EN: Status with value conversion
        // VI: Status với value conversion
        builder.Property<ApproverStatus>("_status")
            .HasColumnName("StatusId")
            .HasConversion(
                v => v.Id,
                v => v == 1 ? ApproverStatus.Pending : v == 2 ? ApproverStatus.Approved : ApproverStatus.Rejected);

        builder.Ignore(x => x.DomainEvents);

        builder.HasIndex("_userId").HasDatabaseName("IX_AccessRequestApprovers_UserId");
    }
}
