using System.ComponentModel;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Ui.Services;

public interface IWorkerClient : INotifyPropertyChanged
{
    bool IsConnected { get; }
    bool IsReconnecting { get; }

    event Action<string, string, DateTime>? TaskStartedEvent;
    event Action<string, string, string, DateTime>? TaskFinishedEvent;
    event Action<string>? TaskUpdatedEvent;
    /// <summary>Raised once when the SignalR connection is first established, and again on every reconnect.</summary>
    event Action? ConnectionRestoredEvent;
    event Action<string>? WorktreeUpdatedEvent;
    event Action<string>? ListUpdatedEvent;
    event Action<string, string>? TaskMessageEvent;
    event Action<WorkerLogEntry>? WorkerLogReceivedEvent;

    event Action? PrepStartedEvent;
    event Action<string>? PrepLineEvent;
    event Action<bool>? PrepFinishedEvent;

    event Action<string, string>? PlanningMergeStartedEvent;
    event Action<string, string>? PlanningSubtaskMergedEvent;
    event Action<string, string, IReadOnlyList<string>>? PlanningMergeConflictEvent;
    event Action<string>? PlanningMergeAbortedEvent;
    event Action<string>? PlanningCompletedEvent;

    event Action<PrimeFiredEvent>? PrimeFired;

    string? LastApproveTarget { get; }

    Task WakeQueueAsync();
    Task RunNowAsync(string taskId);
    Task ContinueTaskAsync(string taskId, string followUpPrompt);
    Task ResetTaskAsync(string taskId);
    Task CancelTaskAsync(string taskId);
    Task<List<AgentInfo>> GetAgentsAsync();
    Task RefreshAgentsAsync();
    Task<SeedResultDto?> RestoreDefaultAgentsAsync();
    Task<ListConfigDto?> GetListConfigAsync(string listId);
    Task UpdateTaskAgentSettingsAsync(UpdateTaskAgentSettingsDto dto);
    Task SetTaskStatusAsync(string taskId, TaskStatus status);
    Task<MergeResultDto?> ApproveReviewAsync(string taskId, string targetBranch);
    Task<MergePreviewDto?> PreviewMergeAsync(string taskId, string targetBranch);
    Task<MergeResultDto> MergeTaskAsync(string taskId, string targetBranch, bool removeWorktree, string commitMessage);
    Task RejectReviewToQueueAsync(string taskId, string feedback);
    Task RejectReviewToIdleAsync(string taskId);
    Task CancelReviewAsync(string taskId);

    // ── Conflict resolution (worker hub side implemented by Layer C) ──
    Task<MergeResultDto> StartConflictMergeAsync(string taskId, string targetBranch);
    Task<MergeConflictDocumentsDto> GetMergeConflictDocumentsAsync(string taskId);
    Task WriteConflictResolutionAsync(string taskId, string path, string resolvedContent);
    Task<MergeResultDto> ContinueConflictMergeAsync(string taskId);
    Task AbortConflictMergeAsync(string taskId);
    Task StartPlanningSessionAsync(string taskId, CancellationToken ct = default);
    Task OpenInteractiveTerminalAsync(string taskId, CancellationToken ct = default);
    Task ResumePlanningSessionAsync(string taskId, CancellationToken ct = default);
    Task<DiscardPlanningOutcome> DiscardPlanningSessionAsync(string taskId, bool dequeueQueuedChildren = false, CancellationToken ct = default);
    Task FinalizePlanningSessionAsync(string taskId, bool queueAgentTasks = true, CancellationToken ct = default);
    Task<int> GetPendingDraftCountAsync(string taskId, CancellationToken ct = default);
    Task<MergeTargetsDto?> GetMergeTargetsAsync(string taskId);
    Task<IReadOnlyList<SubtaskDiffDto>> GetPlanningAggregateAsync(string planningTaskId);
    Task<CombinedDiffResultDto?> BuildPlanningIntegrationBranchAsync(string planningTaskId, string targetBranch);
    Task ContinuePlanningMergeAsync(string planningTaskId);
    Task AbortPlanningMergeAsync(string planningTaskId);
    Task QueuePlanningSubtasksAsync(string parentTaskId, CancellationToken ct = default);
    Task<string?> GetWeekReportAsync(DateOnly start, DateOnly end);
    Task<string> GenerateWeekReportAsync(DateOnly start, DateOnly end);
    Task<bool> RunDailyPrepNowAsync();
    Task RefineTaskAsync(string taskId);

    event Action<string>? RefineStartedEvent;
    event Action<string, bool, string?>? RefineFinishedEvent;
    Task ClearMyDayAsync();
    Task<AppSettingsDto?> GetAppSettingsAsync();
    Task UpdateAppSettingsAsync(AppSettingsDto dto);
    Task<List<DailyNoteDto>> GetDailyNotesAsync(DateOnly day);
    Task<DailyNoteDto?> AddDailyNoteAsync(DateOnly day, string text);
    Task UpdateDailyNoteAsync(string id, string text);
    Task DeleteDailyNoteAsync(string id);
    Task<string> GetLastPrepLogAsync();
    Task<IReadOnlyList<WorkerLogEntry>> GetRecentLogsAsync();

    Task<List<PrimeScheduleDto>> GetPrimeSchedulesAsync();
    Task<PrimeScheduleDto?> UpsertPrimeScheduleAsync(PrimeScheduleDto dto);
    Task DeletePrimeScheduleAsync(Guid id);

    Task UpdateListAsync(UpdateListDto dto);
    Task UpdateListConfigAsync(UpdateListConfigDto dto);

    Task<WorktreeCleanupDto?> CleanupFinishedWorktreesAsync(string? listId = null);
    Task<WorktreeResetDto?> ResetAllWorktreesAsync();
    Task<List<WorktreeOverviewDto>> GetWorktreesOverviewAsync(string? listId);
    Task<(bool Ok, string? Error)> SetWorktreeStateAsync(string taskId, WorktreeState newState);
    Task<ForceRemoveResultDto?> ForceRemoveWorktreeAsync(string taskId);

    Task<OnlineInboxStateDto?> GetOnlineInboxStateAsync();
    Task SetOnlineInboxConfigAsync(OnlineInboxConfigInputDto input);
    Task SetOnlineInboxAuthAsync(string refreshToken);
    Task ClearOnlineInboxAuthAsync();
}
