//
// OpenRocky — Voice-first AI Agent
// https://github.com/openrocky
//
// Developed by everettjf with the assistance of Claude Code and Codex.
// Date: 2026-05-04
// Copyright (c) 2026 everettjf. All rights reserved.
//

import Foundation

/// Live snapshot of an in-flight delegate-task — what subtasks are running,
/// which tool the sub-agent just hit, how it ended. Maintained by
/// SessionRuntime as a `@Published` so the voice overlay can render the
/// sub-agent's progress in real time instead of leaving the user staring at
/// a single status line for 5–30 seconds.
///
/// Cleared a few seconds after `planCompleted` so the panel naturally fades
/// away when the work is done.
struct OpenRockyDelegateProgress: Equatable, Identifiable {
    let id: UUID
    var taskDescription: String
    var subtasks: [Subtask]
    var startedAt: Date
    var finishedAt: Date?
    var planSucceededCount: Int?
    var planTotalCount: Int?

    init(
        id: UUID = UUID(),
        taskDescription: String,
        subtasks: [Subtask] = [],
        startedAt: Date = Date(),
        finishedAt: Date? = nil
    ) {
        self.id = id
        self.taskDescription = taskDescription
        self.subtasks = subtasks
        self.startedAt = startedAt
        self.finishedAt = finishedAt
    }

    /// True once `planCompleted` has fired. The overlay uses this to switch
    /// from "live" styling to a brief "done" summary before clearing.
    var isFinished: Bool { finishedAt != nil }

    struct Subtask: Equatable, Identifiable {
        let id: String
        var index: Int
        var totalCount: Int
        var description: String
        var status: Status
        /// Most recent ~5 tool events for compact display. Older ones rotate
        /// out so the panel doesn't grow unbounded; full history still lives
        /// in the timeline for users who want everything.
        var toolEvents: [ToolEvent]
        var summary: String?
        var succeeded: Bool?
        var elapsedSeconds: Double?

        enum Status: Equatable {
            case running
            case thinking(turn: Int)
            case completed
        }
    }

    struct ToolEvent: Equatable, Identifiable {
        let id: UUID
        let name: String
        let isSkill: Bool
        let argsPreview: String
        var status: Status
        var resultPreview: String?
        var elapsedSeconds: Double?

        enum Status: Equatable {
            case running
            case completed(succeeded: Bool)
        }
    }

    /// How many tool events to keep per subtask. Beyond this we drop the
    /// oldest. Five is enough to show "what the agent is doing right now"
    /// while small enough that the overlay panel stays reasonable on small
    /// devices.
    static let maxToolEventsPerSubtask = 5
}
