using ClaudeDo.Data.Models;
using ClaudeDo.Worker.Prime;
using ClaudeDo.Worker.Refine;
using Microsoft.AspNetCore.SignalR;

namespace ClaudeDo.Worker.Hub;

public sealed class HubBroadcaster : IPrimeBroadcaster, IRefineBroadcaster
{
    private readonly IHubContext<WorkerHub> _hub;

    public HubBroadcaster(IHubContext<WorkerHub> hub) => _hub = hub;

    public Task TaskStarted(string slot, string taskId, DateTime startedAt) =>
        _hub.Clients.All.SendAsync("TaskStarted", slot, taskId, startedAt);

    public Task TaskFinished(string slot, string taskId, string status, DateTime finishedAt) =>
        _hub.Clients.All.SendAsync("TaskFinished", slot, taskId, status, finishedAt);

    public Task TaskMessage(string taskId, string ndjsonLine) =>
        _hub.Clients.All.SendAsync("TaskMessage", taskId, ndjsonLine);

    public Task WorktreeUpdated(string taskId) =>
        _hub.Clients.All.SendAsync("WorktreeUpdated", taskId);

    public Task TaskUpdated(string taskId) =>
        _hub.Clients.All.SendAsync("TaskUpdated", taskId);

    public Task TaskQuestionAsked(string taskId, string questionId, string question) =>
        _hub.Clients.All.SendAsync("TaskQuestionAsked", taskId, questionId, question);

    public Task TaskQuestionResolved(string taskId, string questionId) =>
        _hub.Clients.All.SendAsync("TaskQuestionResolved", taskId, questionId);

    public Task ListUpdated(string listId) =>
        _hub.Clients.All.SendAsync("ListUpdated", listId);

    public Task RunCreated(string taskId, int runNumber, bool isRetry) =>
        _hub.Clients.All.SendAsync("RunCreated", taskId, runNumber, isRetry);

    public Task WorkerLog(string message, WorkerLogLevel level, DateTime timestampUtc) =>
        _hub.Clients.All.SendAsync("WorkerLog", message, level, timestampUtc);

    public Task PlanningMergeStarted(string planningTaskId, string targetBranch) =>
        _hub.Clients.All.SendAsync("PlanningMergeStarted", planningTaskId, targetBranch);

    public Task PlanningSubtaskMerged(string planningTaskId, string subtaskId) =>
        _hub.Clients.All.SendAsync("PlanningSubtaskMerged", planningTaskId, subtaskId);

    public Task PlanningMergeConflict(string planningTaskId, string subtaskId, IReadOnlyList<string> files) =>
        _hub.Clients.All.SendAsync("PlanningMergeConflict", planningTaskId, subtaskId, files);

    public Task PlanningMergeAborted(string planningTaskId) =>
        _hub.Clients.All.SendAsync("PlanningMergeAborted", planningTaskId);

    public Task PlanningCompleted(string planningTaskId) =>
        _hub.Clients.All.SendAsync("PlanningCompleted", planningTaskId);

    public Task PrimeFired(Guid scheduleId, bool success, string message, DateTimeOffset firedAt) =>
        _hub.Clients.All.SendAsync("PrimeFired", scheduleId, success, message, firedAt);

    Task IPrimeBroadcaster.PrimeFiredAsync(Guid scheduleId, bool success, string message, DateTimeOffset firedAt) =>
        PrimeFired(scheduleId, success, message, firedAt);

    public Task PrepStarted() => _hub.Clients.All.SendAsync("PrepStarted");
    public Task PrepLine(string line) => _hub.Clients.All.SendAsync("PrepLine", line);
    public Task PrepFinished(bool success) => _hub.Clients.All.SendAsync("PrepFinished", success);

    Task IPrimeBroadcaster.PrepStartedAsync() => PrepStarted();
    Task IPrimeBroadcaster.PrepLineAsync(string line) => PrepLine(line);
    Task IPrimeBroadcaster.PrepFinishedAsync(bool success) => PrepFinished(success);

    public Task RefineStarted(string taskId) => _hub.Clients.All.SendAsync("RefineStarted", taskId);
    public Task RefineFinished(string taskId, bool success, string? error) =>
        _hub.Clients.All.SendAsync("RefineFinished", taskId, success, error);

    Task IRefineBroadcaster.RefineStartedAsync(string taskId) => RefineStarted(taskId);
    Task IRefineBroadcaster.RefineFinishedAsync(string taskId, bool success, string? error) =>
        RefineFinished(taskId, success, error);
}
