enum AgentWorkspaceRunStatus {
  PENDING
  CLAIMED
  PROCESSING
  COMPLETED
  FAILED
  AWAITING_REVIEW
  AWAITING_SUBRUN
  CANCELLED
  EXPIRED
}

enum AgentWorkspaceEventType {
  WORK_REQUESTED
  RUN_STARTED
  RUN_BLOCKED
  RUN_COMPLETED
  RUN_FAILED
  REVIEW_REQUESTED
  REVIEW_RESPONDED
  MEMORY_CHANGED
  EVENT_REJECTED
}

enum WorkspaceReviewKind {
  PAIRED
  SYSTEM
  UNROUTED
}

type AgentWorkspaceRun {
  id: ID!
  tenantId: ID!
  agentId: ID!
  targetPath: String!
  status: String!
  sourceObjectKey: String
  requestObjectKey: String
  currentWakeupRequestId: ID
  currentThreadTurnId: ID
  parentRunId: ID
  depth: Int!
  inboxWriteCount: Int!
  wakeupRetryCount: Int!
  lastEventAt: AWSDateTime!
  completedAt: AWSDateTime
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type AgentWorkspaceEvent {
  id: ID!
  tenantId: ID!
  agentId: ID
  runId: ID
  eventType: String!
  bucket: String!
  sourceObjectKey: String!
  auditObjectKey: String
  objectEtag: String
  objectVersionId: String
  sequencer: String!
  mirrorStatus: String!
  reason: String
  payload: AWSJSON
  actorType: String
  actorId: String
  parentEventId: ID
  createdAt: AWSDateTime!
}

type AgentWorkspaceWait {
  id: ID!
  tenantId: ID!
  waitingRunId: ID!
  waitForRunId: ID
  waitForTargetPath: String
  status: String!
  createdAt: AWSDateTime!
  satisfiedAt: AWSDateTime
}

type AgentWorkspaceReview {
  run: AgentWorkspaceRun!
  latestEvent: AgentWorkspaceEvent
  threadId: ID
  reviewObjectKey: String
  targetPath: String!
  requestedAt: AWSDateTime!
  reason: String
  payload: AWSJSON
  reviewBody: String
  reviewEtag: String
  reviewMissing: Boolean
  proposedChanges: [AgentWorkspaceProposedChange!]!
  events: [AgentWorkspaceEvent!]!
  decisionEvents: [AgentWorkspaceEvent!]!
  responsibleUserId: ID
  kind: WorkspaceReviewKind!
}

type AgentWorkspaceProposedChange {
  path: String
  kind: String!
  summary: String!
  diff: String
  before: String
  after: String
}

input AgentWorkspaceReviewDecisionInput {
  notes: String
  idempotencyKey: String
  expectedReviewEtag: String
  responseMarkdown: String
}

extend type Query {
  agentWorkspaceRuns(
    agentId: ID
    targetPath: String
    status: String
    limit: Int = 50
  ): [AgentWorkspaceRun!]!
  agentWorkspaceEvents(runId: ID!, limit: Int = 100): [AgentWorkspaceEvent!]!
  agentWorkspaceReviews(
    tenantId: ID!
    agentId: ID
    responsibleUserId: ID
    kind: WorkspaceReviewKind
    status: String = "awaiting_review"
    limit: Int = 50
  ): [AgentWorkspaceReview!]!
  agentWorkspaceReview(runId: ID!): AgentWorkspaceReview
  pendingSystemReviewsCount(tenantId: ID!): Int!
}

extend type Mutation {
  acceptAgentWorkspaceReview(
    runId: ID!
    input: AgentWorkspaceReviewDecisionInput
  ): AgentWorkspaceRun!
  cancelAgentWorkspaceReview(
    runId: ID!
    input: AgentWorkspaceReviewDecisionInput
  ): AgentWorkspaceRun!
  resumeAgentWorkspaceRun(
    runId: ID!
    input: AgentWorkspaceReviewDecisionInput
  ): AgentWorkspaceRun!
}
