using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ClaudeDo.Data.Migrations
{
    /// <inheritdoc />
    public partial class UniqueListName : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Remove duplicate list rows that have no tasks — keep the oldest rowid.
            // This handles the startup-race case where both App and Worker seeded
            // the same default list names concurrently.
            migrationBuilder.Sql("""
                DELETE FROM lists
                WHERE (SELECT COUNT(*) FROM tasks WHERE list_id = lists.id) = 0
                  AND rowid NOT IN (
                      SELECT MIN(l2.rowid) FROM lists l2 WHERE l2.name = lists.name
                  )
                """);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
        }
    }
}
