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

namespace ClaudeDo.Data.Configuration;

public class PrimeScheduleEntityConfiguration : IEntityTypeConfiguration<PrimeScheduleEntity>
{
    private static string KindToString(PrimeActionKind v)
        => v == PrimeActionKind.Ping ? "ping"
         : v == PrimeActionKind.FillMyDay ? "fill_my_day"
         : v == PrimeActionKind.Custom ? "custom"
         : throw new ArgumentOutOfRangeException(nameof(v));

    private static PrimeActionKind KindFromString(string v)
        => v == "ping" ? PrimeActionKind.Ping
         : v == "fill_my_day" ? PrimeActionKind.FillMyDay
         : v == "custom" ? PrimeActionKind.Custom
         : throw new ArgumentOutOfRangeException(nameof(v));

    private static readonly ValueConverter<PrimeActionKind, string> KindConverter =
        new(v => KindToString(v), v => KindFromString(v));

    public void Configure(EntityTypeBuilder<PrimeScheduleEntity> builder)
    {
        builder.ToTable("prime_schedules");

        builder.HasKey(s => s.Id);
        builder.Property(s => s.Id).HasColumnName("id").ValueGeneratedNever();

        builder.Property(s => s.Days).HasColumnName("days_of_week")
            .IsRequired().HasDefaultValue(PrimeDays.Weekdays);
        builder.Property(s => s.Kind).HasColumnName("action_kind")
            .IsRequired().HasDefaultValue(PrimeActionKind.Ping)
            .HasConversion(KindConverter);
        builder.Property(s => s.TimeOfDay).HasColumnName("time_of_day").IsRequired();
        builder.Property(s => s.Enabled).HasColumnName("enabled").IsRequired().HasDefaultValue(true);
        builder.Property(s => s.LastRunAt).HasColumnName("last_run_at");
        builder.Property(s => s.PromptOverride).HasColumnName("prompt_override");
        builder.Property(s => s.CreatedAt).HasColumnName("created_at").IsRequired();
    }
}
