# GraphQL schema for ARP (Agent-native ERP)
# Based on models defined in arp_spec.md

# User account
type User {
  id: ID!
  email: String!
  roles: [Role!]!
  createdAt: String!
  updatedAt: String!
}

# Note belonging to a user and associated with a Service
type Note {
  id: ID!
  title: String!
  content: String!
  userId: ID!
  user: User!
  serviceId: ID!
  service: Service!
  createdAt: String!
  updatedAt: String!  
}

# Role for grouping permissions
type Role {
  id: ID!
  name: String!
  description: String!
  permissions: [Permission!]!
}

# Permission for fine-grained access control
type Permission {
  id: ID!
  code: String!
  description: String!
}

# Authentication payload returned on successful login
type AuthPayload {
  token: String!
  user: User!
}

# Service entity that agents coordinate work around
type Service {
  id: ID!
  name: String!
  description: String
  createdById: ID!
  createdBy: User!
  participants: [User!]!
  tasks: [Task!]!
  createdAt: String!
  updatedAt: String!
}

# Task management
type Task {
  id: ID!
  title: String!
  content: String!
  createdById: ID!
  createdBy: User!
  updatedById: ID!
  updatedBy: User!
  assigneeId: ID
  assignee: User
  statusId: ID
  status: TaskStatus
  dueDate: String
  priority: String!
  createdAt: String!
  updatedAt: String!  
}

# Task status for workflow steps
type TaskStatus {
  id: ID!
  code: String!
  label: String!
  tasks: [Task!]!
  createdAt: String!
  updatedAt: String!
}

# Message sent inside a conversation
type Message {
  id: ID!
  senderId: ID!
  sender: User!
  content: String!
  sentAt: String!
  receivers: [ID!]!
  receiverObjects: [User!]!
  createdAt: String!
  updatedAt: String!
}

# Workflow types
type WorkflowTemplate {
  id: ID!
  name: String!
  description: String
  definition: String!
  isActive: Boolean!
  createdBy: User!
  createdAt: String!
  updatedAt: String!
}

type WorkflowInstance {
  id: ID!
  template: WorkflowTemplate!
  status: String!
  context: String
  service: Service
  createdAt: String!
  updatedAt: String!
  completedAt: String
}

type WorkflowNode {
  id: ID!
  nodeKey: String!
  nodeType: String!
  status: String!
  task: Task
  inputData: String
  outputData: String
  createdAt: String!
  updatedAt: String!
  startedAt: String
  completedAt: String
}

# Root Query type
type Query {
  # Users
  users: [User!]!
  user(id: ID!): User

  # Notes
  notes: [Note!]!
  note(id: ID!): Note

  # Roles
  roles: [Role!]!
  role(id: ID!): Role

  # Permissions
  permissions: [Permission!]!
  permission(id: ID!): Permission

  # Services
  services: [Service!]!
  service(id: ID!): Service

  # Tasks
  tasks: [Task!]!
  task(id: ID!): Task

  # TaskStatuses
  taskStatuses: [TaskStatus!]!
  taskStatus(id: ID!): TaskStatus

  # Messages
  messages: [Message!]!
  message(id: ID!): Message

  # Workflows
  workflowTemplates: [WorkflowTemplate!]!
  workflowTemplate(id: ID!): WorkflowTemplate
  workflowInstances: [WorkflowInstance!]!
  workflowInstance(id: ID!): WorkflowInstance
}

# Input types for workflow mutations
input NewWorkflowTemplate {
  name: String!
  description: String
  definition: String!
  isActive: Boolean
}

input UpdateWorkflowTemplateInput {
  name: String
  description: String
  definition: String
  isActive: Boolean
}

input StartWorkflowInput {
  serviceId: ID
  context: String
}

# Root Mutation type
type Mutation {
  # Authentication
  login(email: String!, password: String!): AuthPayload!

  # Users
  createUser(input: NewUser!): User!
  updateUser(id: ID!, input: UpdateUserInput!): User!
  deleteUser(id: ID!): Boolean!

  # Notes
  createNote(input: NewNote!): Note!
  updateNote(id: ID!, input: UpdateNoteInput!): Note!
  deleteNote(id: ID!): Boolean!

  # Roles
  createRole(input: NewRole!): Role!
  updateRole(id: ID!, input: UpdateRoleInput!): Role!
  deleteRole(id: ID!): Boolean!

  # Permissions
  createPermission(input: NewPermission!): Permission!
  updatePermission(id: ID!, input: UpdatePermissionInput!): Permission!
  deletePermission(id: ID!): Boolean!

  # Services
  createService(input: NewService!): Service!
  updateService(id: ID!, input: UpdateServiceInput!): Service!
  deleteService(id: ID!): Boolean!

  # Tasks
  createTask(input: NewTask!): Task!
  updateTask(id: ID!, input: UpdateTaskInput!): Task!
  deleteTask(id: ID!): Boolean!

  # TaskStatuses
  createTaskStatus(input: NewTaskStatus!): TaskStatus!
  updateTaskStatus(id: ID!, input: UpdateTaskStatusInput!): TaskStatus!
  deleteTaskStatus(id: ID!): Boolean!

  # Messages
  createMessage(input: NewMessage!): Message!
  updateMessage(id: ID!, input: UpdateMessageInput!): Message!
  deleteMessage(id: ID!): Boolean!

  # Workflows
  createWorkflowTemplate(input: NewWorkflowTemplate!): WorkflowTemplate!
  updateWorkflowTemplate(id: ID!, input: UpdateWorkflowTemplateInput!): WorkflowTemplate!
  deleteWorkflowTemplate(id: ID!): Boolean!
  startWorkflow(templateId: ID!, input: StartWorkflowInput!): WorkflowInstance!
  cancelWorkflow(id: ID!): WorkflowInstance!
  retryWorkflowNode(nodeId: ID!): WorkflowNode!
}

# Input types for mutations
input NewUser {
  email: String!
  password: String!
  roles: [ID!]!
}

input UpdateUserInput {
  email: String
  password: String
  roles: [ID!]
}

input NewNote {
  title: String!
  content: String!
  userId: ID!
  serviceId: ID!
}

input UpdateNoteInput {
  title: String
  content: String
  userId: ID
  serviceId: ID
}

input NewRole {
  name: String!
  description: String!
  permissions: [ID!]!
}

input UpdateRoleInput {
  name: String
  description: String
  permissions: [ID!]!
}

input NewPermission {
  code: String!
  description: String!
}

input UpdatePermissionInput {
  code: String
  description: String
}

input NewService {
  name: String!
  description: String
  createdById: ID!
  participants: [ID!]!
}

input UpdateServiceInput {
  name: String
  description: String
  createdById: ID
  participants: [ID!]!
}

input NewTask {
  title: String!
  content: String!
  createdById: ID!
  assigneeId: ID
  statusId: ID
  dueDate: String
  priority: String!
}

input UpdateTaskInput {
  title: String
  content: String
  createdById: ID
  assigneeId: ID
  statusId: ID
  dueDate: String
  priority: String
}

input NewTaskStatus {
  code: String!
  label: String!
}

input UpdateTaskStatusInput {
  code: String
  label: String
}

input NewMessage {
  content: String!
  receivers: [ID!]!
}

input UpdateMessageInput {
  content: String
  receivers: [ID!]
}

# Subscriptions for real-time updates
type Subscription {
  taskCreated: Task!
  taskUpdated: Task!
  taskDeleted: Task!
  messageAdded: Message!
}
