using Entities.Contracts;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;

namespace TechHelper.Context.Configuration
{
	public class AssignmentQuestionConfiguration : IEntityTypeConfiguration<ExamQuestion>
	{
		public void Configure(EntityTypeBuilder<ExamQuestion> builder)
		{
			// 1. 设置表名
			builder.ToTable("assignment_questions");

			// 2. 设置主键
			builder.HasKey(aq => aq.Id);

			// 3. 配置列名、必需性及其他属性

			// 配置 Id 列
			builder.Property(aq => aq.Id)
				.HasColumnName("id");

			// 配置 QuestionId 列 (已修正拼写)
			builder.Property(aq => aq.QuestionId)
				.HasColumnName("question_id");


			// 配置 QuestionNumber 列
			builder.Property(aq => aq.Index)
				.HasColumnName("question_number")
				.IsRequired(); // uint 类型默认非空

			// 配置 CreatedAt 列
			builder.Property(aq => aq.CreatedAt)
				.HasColumnName("created_at")
				.IsRequired(); // 通常创建时间字段是非空的

			builder.Property(aq => aq.Score)
				.HasColumnName("score");

			builder.Property(aq => aq.IsDeleted)
				.HasColumnName("deleted")
				.HasDefaultValue(false); // 适用于软删除策略


			builder.HasOne(aq => aq.Question)             
				.WithMany(q => q.ExamQuestions)     
				.HasForeignKey(aq => aq.QuestionId)       
				.OnDelete(DeleteBehavior.Cascade);        

			builder.HasOne(aq => aq.ParentExamQuestion)
				.WithMany(aq => aq.ChildrenAssignmentQuestion)
				.HasForeignKey(aq => aq.ParentExamQuestionId)
				.OnDelete(DeleteBehavior.Cascade);

			builder.HasOne(aq => aq.QuestionContext)
				.WithMany(qc => qc.Questions)
				.HasForeignKey(aq => aq.QuestionContextId)
				.OnDelete(DeleteBehavior.SetNull);


		}

	}
}
