# Message domain types: Message, MessageArtifact, pagination types,
# and related enums/inputs.

enum MessageRole {
  USER
  ASSISTANT
  SYSTEM
  TOOL
}

enum MessageMentionTargetType {
  USER
  AGENT
}

type Message {
  id: ID!
  threadId: ID!
  tenantId: ID!
  role: MessageRole!
  content: String
  # Plan-012 U7: typed UIMessage parts (Vercel AI SDK Stream Protocol shape)
  # persisted at turn-finalize for new Computer assistant messages. Null on
  # legacy rows; null on user messages until/unless a future plan opts users
  # into typed shape too. Draft app previews are represented as
  # tool-preview_app parts carrying an unsaved draft_app_preview payload, not
  # as MessageArtifact rows. Render-path precedence: parts non-null wins over
  # content. Tenant-scoped by the same gate as content (resolveCallerTenantId
  # + thread/computer ownership) — see contract v1 §Tenant scoping.
  parts: AWSJSON
  senderType: String
  senderId: ID
  ownerType: String!
  ownerId: ID
  toolCalls: AWSJSON
  toolResults: AWSJSON
  metadata: AWSJSON
  tokenCount: Int
  sender: MessageSender
  mentions: [MessageMention!]!
  artifacts: [MessageArtifact!]!
  durableArtifact: Artifact
  createdAt: AWSDateTime!
}

type MessageSender {
  type: String!
  id: ID
  displayName: String!
  avatarUrl: String
}

type MessageMention {
  id: ID!
  messageId: ID!
  threadId: ID!
  tenantId: ID!
  targetType: MessageMentionTargetType!
  targetId: ID!
  displayName: String!
  rawText: String
  startOffset: Int
  endOffset: Int
  metadata: AWSJSON
  user: User
  agent: Agent
  createdAt: AWSDateTime!
}

type MessageArtifact {
  id: ID!
  messageId: ID!
  threadId: ID!
  tenantId: ID!
  artifactType: String!
  name: String
  content: String
  s3Key: String
  mimeType: String
  sizeBytes: Int
  metadata: AWSJSON
  createdAt: AWSDateTime!
}

# --- Pagination ---

type MessageConnection {
  edges: [MessageEdge!]!
  pageInfo: PageInfo!
}

type MessageEdge {
  node: Message!
  cursor: String!
}

# --- Inputs ---

input SendMessageInput {
  threadId: ID!
  role: MessageRole!
  content: String
  senderType: String
  senderId: ID
  toolCalls: AWSJSON
  toolResults: AWSJSON
  metadata: AWSJSON
  mentions: [SendMessageMentionInput!]
}

input SendMessageMentionInput {
  targetType: MessageMentionTargetType!
  targetId: ID!
  displayName: String
  rawText: String
  startOffset: Int
  endOffset: Int
}

# --- Query / Mutation extensions ---

extend type Query {
  messages(threadId: ID!, limit: Int, cursor: String): MessageConnection!
}

extend type Mutation {
  sendMessage(input: SendMessageInput!): Message!
  deleteMessage(id: ID!): Boolean!
}
