using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SolutionErp.Domain.Office;

namespace SolutionErp.Infrastructure.Persistence.Configurations;

// EF Mig 41 P11-A (Phase 11) — Ý kiến cấp duyệt V2 dynamic cho LeaveRequest.
// Cookie-cutter mirror ProposalLevelOpinionConfiguration (Mig 38).
public class LeaveRequestLevelOpinionConfiguration : IEntityTypeConfiguration<LeaveRequestLevelOpinion>
{
    public void Configure(EntityTypeBuilder<LeaveRequestLevelOpinion> e)
    {
        e.ToTable("LeaveRequestLevelOpinions");

        e.Property(x => x.Comment).HasMaxLength(2000);
        e.Property(x => x.SignedByFullName).HasMaxLength(200).IsRequired();

        e.HasOne(x => x.LeaveRequest)
            .WithMany(p => p.LevelOpinions)
            .HasForeignKey(x => x.LeaveRequestId)
            .OnDelete(DeleteBehavior.Cascade);

        e.HasOne(x => x.Level)
            .WithMany()
            .HasForeignKey(x => x.ApprovalWorkflowLevelId)
            .OnDelete(DeleteBehavior.Restrict);

        e.HasIndex(x => new { x.LeaveRequestId, x.ApprovalWorkflowLevelId }).IsUnique();
        e.HasIndex(x => x.ApprovalWorkflowLevelId);
    }
}
