using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using UniVerse.Domain.Entities;

namespace UniVerse.Infrastructure.Data.Configurations;

public class ReviewConfiguration : IEntityTypeConfiguration<Review>
{
    public void Configure(EntityTypeBuilder<Review> builder)
    {
        builder.ToTable("reviews");

        builder.HasKey(r => r.Id);
        builder.Property(r => r.Id).HasColumnName("id");
        builder.Property(r => r.LectureId).HasColumnName("lecture_id");
        builder.Property(r => r.UserId).HasColumnName("user_id");
        builder.Property(r => r.Rating).HasColumnName("rating");
        builder.Property(r => r.Text).HasColumnName("text");
        builder.Property(r => r.LlmStatus).HasColumnName("llm_status").HasDefaultValue(Domain.Enums.ReviewLlmStatus.Pending);
        builder.Property(r => r.Sentiment).HasColumnName("sentiment");
        builder.Property(r => r.QualityScore).HasColumnName("quality_score");
        builder.Property(r => r.IsInformative).HasColumnName("is_informative");
        builder.Property(r => r.LlmTags).HasColumnName("llm_tags");
        builder.Property(r => r.LlmRawOutput).HasColumnName("llm_raw_output");
        builder.Property(r => r.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");
        builder.Property(r => r.UpdatedAt).HasColumnName("updated_at").HasDefaultValueSql("NOW()");

        builder.HasOne(r => r.Lecture)
            .WithMany(l => l.Reviews)
            .HasForeignKey(r => r.LectureId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasOne(r => r.User)
            .WithMany(u => u.Reviews)
            .HasForeignKey(r => r.UserId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasIndex(r => new { r.LectureId, r.UserId }).IsUnique();
        builder.HasIndex(r => r.LlmStatus);
    }
}
