using Nexus.Api.Data;
using Nexus.Api.DTOs;
using Nexus.Api.Models;

namespace Nexus.Api.Services;

public enum TaskOperationOutcome { Success, NotFound, InvalidState }

public sealed record TaskOperationResult(TaskOperationOutcome Outcome, WorkTask? Task = null);

public interface ITaskService
{
    Task<IReadOnlyList<WorkTask>> GetAllAsync(CancellationToken ct = default);
    Task<WorkTask?> GetByIdAsync(Guid id, CancellationToken ct = default);
    Task<IReadOnlyList<WorkTask>> GetPendingApprovalAsync(CancellationToken ct = default);
    Task<WorkTask> CreateAsync(CreateTaskRequest request, CancellationToken ct = default);
    Task<TaskOperationResult> ApproveAsync(Guid id, CancellationToken ct = default);
    Task<TaskOperationResult> RejectAsync(Guid id, CancellationToken ct = default);
    Task<TaskOperationResult> UpdateStateAsync(Guid id, string state, CancellationToken ct = default);
    Task<TaskOperationResult> UpdateAsync(Guid id, UpdateTaskRequest request, CancellationToken ct = default);
    Task<TaskOperationResult> DeleteAsync(Guid id, CancellationToken ct = default);

    // Dashboard-facing task operations
    Task<IReadOnlyList<WorkTask>> GetOpenAsync(CancellationToken ct = default);
    Task<WorkTask> CreateDashboardTaskAsync(string title, string? detail, string? source, string? priority, string? assignedTo, Guid? parentTaskId = null, CancellationToken ct = default);
    Task<WorkTask> CreateAgentTaskAsync(string title, string? detail, string? source, string? priority, string? assignedTo, string? expectedFrom, Guid? parentTaskId = null, bool startsInProgress = true, string? initialState = null, CancellationToken ct = default);
    Task<TaskOperationResult> UpdateDashboardTaskAsync(Guid id, string? title, string? detail, string? source, string? priority, string? assignedTo, DateTimeOffset? dueDate = null, CancellationToken ct = default);
    Task<TaskOperationResult> UpdateStatusAsync(Guid id, string status, CancellationToken ct = default);
    Task<TaskOperationResult> StartCoordinationAsync(Guid id, CancellationToken ct = default);
    Task<TaskOperationResult> CompleteViaQueueAsync(Guid id, CancellationToken ct = default);
    Task<TaskOperationResult> CyclePriorityAsync(Guid id, CancellationToken ct = default);

    // Task Board
    Task<BoardResponse> GetBoardAsync(CancellationToken ct = default);
    Task<TaskBoardPageDto> GetBoardPageAsync(
        int doneLimit = 50,
        string? doneCursor = null,
        CancellationToken ct = default);
    Task<TaskBoardCardDto?> GetBoardCardAsync(
        Guid id,
        CancellationToken ct = default);
    Task<TaskOperationResult> MoveTaskAsync(Guid id, string newState, CancellationToken ct = default);
    Task<int> ResetStaleAsync(int staleHours, CancellationToken ct = default);
    Task<int> ResetStaleInProgressTasksAsync(TimeSpan staleThreshold, CancellationToken ct = default);
    Task<IReadOnlyList<WorkTask>> GetChildTasksAsync(Guid parentId, CancellationToken ct = default);
    Task<List<ActivityEvent>> GetTaskActivityAsync(Guid taskId, CancellationToken ct = default);
    Task<DashboardTaskDto?> GetDashboardTaskByIdAsync(Guid id, CancellationToken ct = default);

    // Agent Workflow Overview
    Task<IReadOnlyList<WorkTask>> GetWaitingTasksAsync(CancellationToken ct = default);
    Task<AgentWorkflowOverview> GetAgentWorkflowOverviewAsync(TimeSpan staleThreshold, CancellationToken ct = default);
}
