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

namespace SolutionErp.Infrastructure.Persistence.Configurations;

// EF Mig 38 G-O3 (S37) — Ý kiến cấp duyệt V2 dynamic. UPSERT auto từ ApproveV2Async.
// Cookie-cutter mirror PurchaseEvaluationLevelOpinionConfiguration (Mig 26).
public class ProposalLevelOpinionConfiguration : IEntityTypeConfiguration<ProposalLevelOpinion>
{
    public void Configure(EntityTypeBuilder<ProposalLevelOpinion> e)
    {
        e.ToTable("ProposalLevelOpinions");

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

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

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

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