using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using Microsoft.EntityFrameworkCore;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Worker.Online;

public static class OnlineBacklog
{
    /// <summary>
    /// Returns the current Idle backlog: Status==Idle, no parent, PlanningPhase==None, not blocked.
    /// These are the tasks mirrored to the online store (§2 of the contract).
    /// </summary>
    public static async Task<List<MirrorTask>> CurrentAsync(
        TaskRepository tasks,
        CancellationToken ct = default)
    {
        var all = await tasks.GetAllIdleBacklogAsync(ct);
        return all.Select(t => new MirrorTask(t.Id, t.ListId, t.Title, t.Description)).ToList();
    }

    internal static bool IsBacklogItem(TaskEntity t) =>
        t.Status == TaskStatus.Idle
        && t.ParentTaskId == null
        && t.PlanningPhase == PlanningPhase.None
        && t.BlockedByTaskId == null;
}
