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

namespace TechHelper.Context.Configuration
{
	public class AssignmentConfiguration : IEntityTypeConfiguration<Exam>
	{
		public void Configure(EntityTypeBuilder<Exam> builder)
		{
			builder.ToTable("assignments");

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

			builder.Property(a => a.Id)
				   .HasColumnName("id")
				   .ValueGeneratedOnAdd();

			builder.Property(a => a.Title)
				   .IsRequired()         
				   .HasColumnName("title") 
				   .HasMaxLength(255);    

			builder.Property(a => a.Description)
				   .HasColumnName("description"); 

			builder.Property(a => a.DueDate)
				   .IsRequired()         
				   .HasColumnName("due_date");

			builder.Property(a => a.TotalQuestions)
				   .HasColumnName("total_points"); 

			builder.Property(a => a.CreatorId)
				   .HasColumnName("created_by"); 

			builder.Property(a => a.CreatedAt)
				   .IsRequired()         
				   .HasColumnName("created_at");

			builder.Property(a => a.UpdatedAt)
				   .IsRequired()         
				   .HasColumnName("updated_at"); 

			builder.Property(a => a.IsDeleted)
				   .HasColumnName("deleted"); 

			// 配置导航属性和关系

			// 关系: Assignment (多) 到 User (一) - CreatedBy 外键
			// 假设 User 实体有 Id 作为主键
			// .WithMany() 表示 User 有多个 Assignment，但这里不指定 User 上的导航属性名称
			// 如果 User 有一个名为 AssignmentsCreated 的导航属性，应写为 .WithMany(u => u.AssignmentsCreated)
			builder.HasOne(a => a.Creator)
				   .WithMany() // User 实体没有指向 Assignment 的导航属性 (或我们不知道)
				   .HasForeignKey(a => a.CreatorId)
				   .IsRequired(); // CreatedBy 是必填的，对应 [Required] 在外键属性上



			builder.HasOne(a=>a.ExamStruct)
				.WithOne()
				.HasForeignKey<Exam>(a=>a.ExamStructId)
				.OnDelete(DeleteBehavior.Cascade);

		}
	}
}
