# Spaces are tenant-scoped contextual workrooms. They organize threads while
# carrying the context, connected data, tool policy, MCP policy, and agent
# availability hints that shape agent turns inside the workroom.

enum SpaceStatus {
  ACTIVE
  ARCHIVED
}

enum SpaceKind {
  CUSTOM
  CUSTOMER_ONBOARDING
}

enum SpaceAccessMode {
  PUBLIC
  PRIVATE
}

enum SpaceMemberRole {
  OWNER
  ADMIN
  MEMBER
  VIEWER
}

enum SpaceNotificationPreference {
  SUBSCRIBED
  MENTIONS
  MUTED
}

enum SpaceAgentAssignmentStatus {
  ACTIVE
  PAUSED
  ARCHIVED
}

enum SpaceIntegrationProvider {
  LASTMILE_TASKS
  WEBHOOK
}

enum SpaceIntegrationStatus {
  ACTIVE
  PAUSED
  ARCHIVED
}

enum SpaceExternalWritebackPolicy {
  DISABLED
  STATUS_ONLY
  STATUS_AND_COMMENTS
}

type Space {
  id: ID!
  tenantId: ID!
  slug: String!
  name: String!
  description: String
  prompt: String
  status: SpaceStatus!
  kind: SpaceKind!
  accessMode: SpaceAccessMode!
  icon: String
  category: String
  templateKey: String
  config: AWSJSON
  contextConfig: AWSJSON
  connectedDataConfig: AWSJSON
  toolPolicy: AWSJSON
  mcpPolicy: AWSJSON
  agentAvailabilityPolicy: AWSJSON
  triggerConfig: AWSJSON
  renderDiagnostics: AWSJSON
  members: [SpaceMember!]!
  agentAssignments: [SpaceAgentAssignment!]!
  mcpServers: [SpaceMcpServer!]!
  checklistTemplates: [SpaceChecklistTemplate!]!
  integrations: [SpaceIntegration!]!
  unreadThreadCount: Int!
  lastActivityAt: AWSDateTime
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type SpaceMember {
  id: ID!
  tenantId: ID!
  spaceId: ID!
  userId: ID!
  user: User
  role: SpaceMemberRole!
  notificationPreference: SpaceNotificationPreference!
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type SpaceAgentAssignment {
  id: ID!
  tenantId: ID!
  spaceId: ID!
  agentId: ID!
  agent: Agent
  localRole: String
  localInstructions: String
  autoSubscribe: Boolean!
  allowedCapabilities: AWSJSON
  allowedTools: AWSJSON
  status: SpaceAgentAssignmentStatus!
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type SpaceMcpServer {
  id: ID!
  tenantId: ID!
  spaceId: ID!
  mcpServerId: ID!
  mcpServer: SpaceTenantMcpServer
  enabled: Boolean!
  config: AWSJSON
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type SpaceTenantMcpServer {
  id: ID!
  tenantId: ID!
  name: String!
  slug: String!
  transport: String!
  authType: String!
  oauthProvider: String
  tools: AWSJSON
  enabled: Boolean!
  status: String!
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type SpaceChecklistTemplate {
  id: ID!
  tenantId: ID!
  spaceId: ID!
  key: String!
  name: String!
  description: String
  config: AWSJSON
  items: [SpaceChecklistItem!]!
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type SpaceChecklistItem {
  id: ID!
  tenantId: ID!
  spaceId: ID!
  templateId: ID!
  key: String!
  title: String!
  description: String
  roleKey: String
  required: Boolean!
  sortOrder: Int!
  externalTaskTemplate: AWSJSON
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type SpaceIntegration {
  id: ID!
  tenantId: ID!
  spaceId: ID!
  provider: SpaceIntegrationProvider!
  status: SpaceIntegrationStatus!
  writebackPolicy: SpaceExternalWritebackPolicy!
  config: AWSJSON
  webhookConfigRef: String
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type StartCustomerOnboardingPayload {
  thread: Thread!
  threadId: ID!
  idempotent: Boolean!
  missingFields: [String!]!
  linkedTasks: [CustomerOnboardingLinkedTaskResult!]!
}

type CustomerOnboardingLinkedTaskResult {
  checklistItemId: ID!
  title: String!
  externalTaskId: String!
  externalTaskUrl: String
  status: LinkedTaskStatus!
  blocked: Boolean!
  syncStatus: LinkedTaskSyncStatus!
}

input StartCustomerOnboardingInput {
  tenantId: ID!
  spaceId: ID
  opportunity: AWSJSON!
}

input CreateSpaceInput {
  tenantId: ID!
  name: String!
  description: String
  accessMode: SpaceAccessMode
}

input UpdateSpaceInput {
  tenantId: ID!
  spaceId: ID!
  name: String
  description: String
  accessMode: SpaceAccessMode
}

input SetSpaceAgentAvailabilityInput {
  tenantId: ID!
  spaceId: ID!
  agentId: ID!
  enabled: Boolean!
  localRole: String
  localInstructions: String
  autoSubscribe: Boolean
  allowedCapabilities: AWSJSON
  allowedTools: AWSJSON
}

extend type Query {
  spaces(
    tenantId: ID!
    status: SpaceStatus
    includeAllForAdmin: Boolean
  ): [Space!]!
  space(id: ID!): Space
  customerOnboardingSpace(tenantId: ID!): Space
}

extend type Mutation {
  createSpace(input: CreateSpaceInput!): Space!
  updateSpace(input: UpdateSpaceInput!): Space!
  setSpaceAgentAvailability(
    input: SetSpaceAgentAvailabilityInput!
  ): SpaceAgentAssignment!
  startCustomerOnboarding(
    input: StartCustomerOnboardingInput!
  ): StartCustomerOnboardingPayload!
}
