using Microsoft.EntityFrameworkCore;

namespace ClaudeDo.Data.Seeding;

public static class DefaultListsSeeder
{
    private static readonly string[] Defaults = { "My Day", "Important", "Planned" };

    public static async Task SeedAsync(ClaudeDoDbContext ctx, CancellationToken ct = default)
    {
        var now = DateTime.UtcNow;
        foreach (var name in Defaults)
        {
            var id = Guid.NewGuid().ToString();
            // Atomic conditional insert: the SELECT ... WHERE NOT EXISTS is a single
            // SQLite statement and cannot race — only one writer holds the lock.
            await ctx.Database.ExecuteSqlAsync(
                $"""
                INSERT INTO lists (id, name, created_at, default_commit_type, sort_order)
                SELECT {id}, {name}, {now}, 'chore', 0
                WHERE NOT EXISTS (SELECT 1 FROM lists WHERE name = {name})
                """, ct);
        }
    }
}
