using ClaudeDo.Data.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace ClaudeDo.Data.Configuration;

public class SubtaskEntityConfiguration : IEntityTypeConfiguration<SubtaskEntity>
{
    public void Configure(EntityTypeBuilder<SubtaskEntity> builder)
    {
        builder.ToTable("subtasks");

        builder.HasKey(s => s.Id);
        builder.Property(s => s.Id).HasColumnName("id");
        builder.Property(s => s.TaskId).HasColumnName("task_id").IsRequired();
        builder.Property(s => s.Title).HasColumnName("title").IsRequired();
        builder.Property(s => s.Completed).HasColumnName("completed").IsRequired().HasDefaultValue(false);
        builder.Property(s => s.OrderNum).HasColumnName("order_num").IsRequired();
        builder.Property(s => s.CreatedAt).HasColumnName("created_at").IsRequired();

        builder.HasOne(s => s.Task)
            .WithMany(t => t.Subtasks)
            .HasForeignKey(s => s.TaskId)
            .OnDelete(DeleteBehavior.Cascade);

        builder.HasIndex(s => s.TaskId).HasDatabaseName("idx_subtasks_task_id");
    }
}
