# Scheduled Jobs — unified scheduled job definitions and run tracking.

type ScheduledJob {
  id: ID!
  tenantId: ID!
  triggerType: String!
  agentId: ID
  computerId: ID
  routineId: ID
  teamId: ID
  name: String!
  description: String
  prompt: String
  config: AWSJSON
  scheduleType: String
  scheduleExpression: String
  timezone: String!
  enabled: Boolean!
  ebScheduleName: String
  lastRunAt: AWSDateTime
  nextRunAt: AWSDateTime
  createdByType: String
  createdById: String
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

input CreateScheduledJobInput {
  tenantId: ID!
  triggerType: String!
  agentId: ID
  computerId: ID
  routineId: ID
  teamId: ID
  name: String!
  description: String
  prompt: String
  config: AWSJSON
  scheduleType: String
  scheduleExpression: String
  timezone: String
  createdByType: String
  createdById: String
}

extend type Query {
  scheduledJobs(
    tenantId: ID!
    agentId: ID
    computerId: ID
    routineId: ID
    triggerType: String
    connectionId: ID
    enabled: Boolean
    limit: Int
  ): [ScheduledJob!]!
  scheduledJob(id: ID!): ScheduledJob
}

"""
Result of `deleteScheduledJob`. `id` echoes the row that was removed;
`ok` is false when no row matched (already deleted, or tenant mismatch).
The EventBridge schedule, when one existed, is removed by the
job-schedule-manager Lambda first; if that side-effect fails the
resolver throws so the DB row is preserved and the caller can retry.
"""
type DeleteScheduledJobResult {
  id: ID!
  ok: Boolean!
}

"""
Partial-update input for `updateScheduledJob`. Any field set updates that
column on the scheduled_jobs row. Changing `scheduleExpression`,
`scheduleType`, `timezone`, or `enabled` propagates to AWS EventBridge
via the job-schedule-manager Lambda (re-creates the underlying schedule
when the expression changes; toggles state when only `enabled` changes).
`config` is sent as an object — the resolver passes it through to the
Lambda which JSON-serializes for the DB column.
"""
input UpdateScheduledJobInput {
  name: String
  description: String
  prompt: String
  config: AWSJSON
  scheduleType: String
  scheduleExpression: String
  timezone: String
  enabled: Boolean
}

"""
Result of `runScheduledJob`. Synchronously invokes the job-trigger
Lambda with the same payload AWS Scheduler would have sent at the
next firing — effectively "run now". `dispatched` is true when the
Lambda accepted the invocation; downstream side effects (thread turn,
routine execution, eval run) happen asynchronously and aren't part of
this result. `statusCode` echoes the Lambda's response code so
operators can distinguish accept (200/202) vs throttle / error.
"""
type RunScheduledJobResult {
  id: ID!
  dispatched: Boolean!
  statusCode: Int
  errorMessage: String
}

extend type Mutation {
  createScheduledJob(input: CreateScheduledJobInput!): ScheduledJob!
  updateScheduledJob(id: ID!, input: UpdateScheduledJobInput!): ScheduledJob!
  deleteScheduledJob(id: ID!): DeleteScheduledJobResult!
  runScheduledJob(id: ID!): RunScheduledJobResult!
}
