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

namespace ClaudeDo.Data.Configuration;

public class TaskRunEntityConfiguration : IEntityTypeConfiguration<TaskRunEntity>
{
    public void Configure(EntityTypeBuilder<TaskRunEntity> builder)
    {
        builder.ToTable("task_runs");

        builder.HasKey(r => r.Id);
        builder.Property(r => r.Id).HasColumnName("id");
        builder.Property(r => r.TaskId).HasColumnName("task_id").IsRequired();
        builder.Property(r => r.RunNumber).HasColumnName("run_number").IsRequired();
        builder.Property(r => r.SessionId).HasColumnName("session_id");
        builder.Property(r => r.IsRetry).HasColumnName("is_retry").IsRequired().HasDefaultValue(false);
        builder.Property(r => r.Prompt).HasColumnName("prompt").IsRequired();
        builder.Property(r => r.ResultMarkdown).HasColumnName("result_markdown");
        builder.Property(r => r.StructuredOutputJson).HasColumnName("structured_output");
        builder.Property(r => r.ErrorMarkdown).HasColumnName("error_markdown");
        builder.Property(r => r.ExitCode).HasColumnName("exit_code");
        builder.Property(r => r.TurnCount).HasColumnName("turn_count");
        builder.Property(r => r.TokensIn).HasColumnName("tokens_in");
        builder.Property(r => r.TokensOut).HasColumnName("tokens_out");
        builder.Property(r => r.CacheReadTokens).HasColumnName("cache_read_tokens");
        builder.Property(r => r.CacheWriteTokens).HasColumnName("cache_write_tokens");
        builder.Property(r => r.LogPath).HasColumnName("log_path");
        builder.Property(r => r.StartedAt).HasColumnName("started_at");
        builder.Property(r => r.FinishedAt).HasColumnName("finished_at");
        builder.Property(r => r.Model).HasColumnName("model");
        builder.Property(r => r.ResultSubtype).HasColumnName("result_subtype");
        builder.Property(r => r.TerminalReason).HasColumnName("terminal_reason");
        builder.Property(r => r.Errors).HasColumnName("errors");

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

        builder.HasIndex(r => r.TaskId).HasDatabaseName("idx_task_runs_task_id");
    }
}
