import { AgentScopeConfig, ProjectScopeConfig } from "../../db/manage/scope-definitions.js";
import { PaginationConfig } from "../../types/utility.js";
import { AgentsRunDatabaseClient } from "../../db/runtime/runtime-client.js";
import { ScheduledTriggerInvocationStatus } from "../../validation/schemas.js";
import { LastRunSummary, ScheduledTriggerInvocation, ScheduledTriggerInvocationInsert, ScheduledTriggerInvocationUpdate } from "../../types/entities.js";

//#region src/data-access/runtime/scheduledTriggerInvocations.d.ts
/**
 * Get a scheduled trigger invocation by ID (agent-scoped)
 */
declare const getScheduledTriggerInvocationById: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  invocationId: string;
}) => Promise<ScheduledTriggerInvocation | undefined>;
/**
 * Get a scheduled trigger invocation by idempotency key
 * Used to check if an invocation already exists for a given schedule
 */
declare const getScheduledTriggerInvocationByIdempotencyKey: (db: AgentsRunDatabaseClient) => (params: {
  idempotencyKey: string;
}) => Promise<ScheduledTriggerInvocation | undefined>;
/**
 * List scheduled trigger invocations with optional filtering (agent-scoped)
 */
declare const listScheduledTriggerInvocationsPaginated: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  pagination?: PaginationConfig;
  filters?: {
    status?: ScheduledTriggerInvocationStatus;
    from?: string;
    to?: string;
  };
}) => Promise<{
  data: {
    scheduledTriggerId: string;
    ref: {
      type: "commit" | "tag" | "branch";
      name: string;
      hash: string;
    } | null;
    status: "pending" | "failed" | "running" | "completed" | "cancelled";
    scheduledFor: string;
    startedAt: string | null;
    completedAt: string | null;
    resolvedPayload: Record<string, unknown> | null;
    conversationIds: string[] | null;
    attemptNumber: number;
    idempotencyKey: string;
    runAsUserId: string | null;
    createdAt: string;
    agentId: string;
    projectId: string;
    tenantId: string;
    id: string;
  }[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    pages: number;
  };
}>;
/**
 * List pending invocations for a trigger, ordered by scheduledFor (earliest first)
 * Used by workflow to get the next invocation to execute
 */
declare const listPendingScheduledTriggerInvocations: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  limit?: number;
}) => Promise<ScheduledTriggerInvocation[]>;
/**
 * Delete all pending invocations for a trigger
 * Used when cron expression changes or trigger is disabled
 */
declare const deletePendingInvocationsForTrigger: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
}) => Promise<number>;
/**
 * Create a new scheduled trigger invocation (agent-scoped)
 */
declare const createScheduledTriggerInvocation: (db: AgentsRunDatabaseClient) => (params: ScheduledTriggerInvocationInsert) => Promise<ScheduledTriggerInvocation>;
/**
 * Update scheduled trigger invocation status (agent-scoped)
 */
declare const updateScheduledTriggerInvocationStatus: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  invocationId: string;
  data: ScheduledTriggerInvocationUpdate;
}) => Promise<ScheduledTriggerInvocation>;
/**
 * Mark invocation as running
 */
declare const markScheduledTriggerInvocationRunning: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  invocationId: string;
}) => Promise<ScheduledTriggerInvocation>;
/**
 * Mark invocation as completed
 * Note: Will not update if status is already 'cancelled' to respect user cancellation
 */
declare const markScheduledTriggerInvocationCompleted: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  invocationId: string;
}) => Promise<ScheduledTriggerInvocation | undefined>;
/**
 * Mark invocation as failed
 * Note: Will not update if status is already 'cancelled' to respect user cancellation
 */
declare const markScheduledTriggerInvocationFailed: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  invocationId: string;
}) => Promise<ScheduledTriggerInvocation | undefined>;
/**
 * Reset a cancelled invocation back to pending.
 * Only updates if the current status is 'cancelled' to prevent race conditions.
 */
declare const resetCancelledInvocationToPending: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  invocationId: string;
}) => Promise<ScheduledTriggerInvocation | undefined>;
/**
 * Add a conversation ID to the invocation's conversationIds array
 * Used to track all conversations created during retries
 */
declare const addConversationIdToInvocation: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  invocationId: string;
  conversationId: string;
}) => Promise<ScheduledTriggerInvocation | undefined>;
/**
 * Mark invocation as cancelled
 */
declare const markScheduledTriggerInvocationCancelled: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  scheduledTriggerId: string;
  invocationId: string;
}) => Promise<ScheduledTriggerInvocation>;
/**
 * Cancel all pending invocations for a trigger
 * Used when a trigger is deleted
 */
declare const cancelPendingInvocationsForTrigger: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  scheduledTriggerId: string;
}) => Promise<number>;
/**
 * Cancel only PAST pending invocations for a trigger (scheduledFor <= now)
 * Used when a trigger is disabled - keeps future invocations pending
 */
declare const cancelPastPendingInvocationsForTrigger: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  scheduledTriggerId: string;
}) => Promise<number>;
type ScheduledTriggerRunInfo = {
  lastRunAt: string | null;
  lastRunStatus: 'completed' | 'failed' | null;
  lastRunConversationIds: string[];
  nextRunAt: string | null;
  lastRunSummary: LastRunSummary | null;
};
/**
 * Get run info for multiple scheduled triggers in a single query.
 * Groups invocations by (triggerId, scheduledFor) to support multi-user triggers.
 * Excludes manual runs (manual-run-* and manual-rerun-* idempotency keys).
 * Returns lastRunSummary with per-status counts for the most recent scheduled tick.
 */
declare const getScheduledTriggerRunInfoBatch: (db: AgentsRunDatabaseClient) => (params: {
  scopes: Omit<AgentScopeConfig, "agentId">;
  triggerIds: Array<{
    agentId: string;
    triggerId: string;
  }>;
}) => Promise<Map<string, ScheduledTriggerRunInfo>>;
/**
 * List upcoming invocations across ALL triggers for an agent with pagination
 * Used for the upcoming runs dashboard with full pagination support
 */
declare const listUpcomingInvocationsForAgentPaginated: (db: AgentsRunDatabaseClient) => (params: {
  scopes: Omit<AgentScopeConfig, "agentId"> & {
    agentId: string;
  };
  pagination?: PaginationConfig;
  includeRunning?: boolean;
}) => Promise<{
  data: {
    scheduledTriggerId: string;
    ref: {
      type: "commit" | "tag" | "branch";
      name: string;
      hash: string;
    } | null;
    status: "pending" | "failed" | "running" | "completed" | "cancelled";
    scheduledFor: string;
    startedAt: string | null;
    completedAt: string | null;
    resolvedPayload: Record<string, unknown> | null;
    conversationIds: string[] | null;
    attemptNumber: number;
    idempotencyKey: string;
    runAsUserId: string | null;
    createdAt: string;
    agentId: string;
    projectId: string;
    tenantId: string;
    id: string;
  }[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    pages: number;
  };
}>;
/**
 * List all invocations across ALL triggers for a PROJECT with pagination
 * Used for the project-level invocations dashboard
 */
declare const listProjectScheduledTriggerInvocationsPaginated: (db: AgentsRunDatabaseClient) => (params: {
  scopes: Omit<AgentScopeConfig, "agentId">;
  pagination?: PaginationConfig;
  filters?: {
    status?: ScheduledTriggerInvocationStatus;
  };
}) => Promise<{
  data: {
    scheduledTriggerId: string;
    ref: {
      type: "commit" | "tag" | "branch";
      name: string;
      hash: string;
    } | null;
    status: "pending" | "failed" | "running" | "completed" | "cancelled";
    scheduledFor: string;
    startedAt: string | null;
    completedAt: string | null;
    resolvedPayload: Record<string, unknown> | null;
    conversationIds: string[] | null;
    attemptNumber: number;
    idempotencyKey: string;
    runAsUserId: string | null;
    createdAt: string;
    agentId: string;
    projectId: string;
    tenantId: string;
    id: string;
  }[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    pages: number;
  };
}>;
/**
 * Get status summary for all invocations with a given scheduledTriggerId.
 * Used for dataset run status aggregation where scheduledTriggerId = datasetRunId.
 */
declare const getScheduledTriggerInvocationStatusSummary: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  scheduledTriggerId: string;
}) => Promise<{
  pending: number;
  running: number;
  completed: number;
  failed: number;
  cancelled: number;
}>;
/**
 * List invocations by scheduledTriggerId without agentId filter (project-scoped).
 * Used for dataset run items listing where scheduledTriggerId = datasetRunId.
 */
declare const listScheduledTriggerInvocationsByTriggerId: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  scheduledTriggerId: string;
  filters?: {
    status?: string;
  };
}) => Promise<{
  scheduledTriggerId: string;
  ref: {
    type: "commit" | "tag" | "branch";
    name: string;
    hash: string;
  } | null;
  status: "pending" | "failed" | "running" | "completed" | "cancelled";
  scheduledFor: string;
  startedAt: string | null;
  completedAt: string | null;
  resolvedPayload: Record<string, unknown> | null;
  conversationIds: string[] | null;
  attemptNumber: number;
  idempotencyKey: string;
  runAsUserId: string | null;
  createdAt: string;
  agentId: string;
  projectId: string;
  tenantId: string;
  id: string;
}[]>;
declare const listScheduledTriggerInvocationsByDatasetRunId: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  datasetRunId: string;
  filters?: {
    status?: string;
  };
}) => Promise<{
  scheduledTriggerId: string;
  ref: {
    type: "commit" | "tag" | "branch";
    name: string;
    hash: string;
  } | null;
  status: "pending" | "failed" | "running" | "completed" | "cancelled";
  scheduledFor: string;
  startedAt: string | null;
  completedAt: string | null;
  resolvedPayload: Record<string, unknown> | null;
  conversationIds: string[] | null;
  attemptNumber: number;
  idempotencyKey: string;
  runAsUserId: string | null;
  createdAt: string;
  agentId: string;
  projectId: string;
  tenantId: string;
  id: string;
}[]>;
declare const getLastRunAtForTrigger: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  scheduledTriggerId: string;
}) => Promise<string | null>;
//#endregion
export { ScheduledTriggerRunInfo, addConversationIdToInvocation, cancelPastPendingInvocationsForTrigger, cancelPendingInvocationsForTrigger, createScheduledTriggerInvocation, deletePendingInvocationsForTrigger, getLastRunAtForTrigger, getScheduledTriggerInvocationById, getScheduledTriggerInvocationByIdempotencyKey, getScheduledTriggerInvocationStatusSummary, getScheduledTriggerRunInfoBatch, listPendingScheduledTriggerInvocations, listProjectScheduledTriggerInvocationsPaginated, listScheduledTriggerInvocationsByDatasetRunId, listScheduledTriggerInvocationsByTriggerId, listScheduledTriggerInvocationsPaginated, listUpcomingInvocationsForAgentPaginated, markScheduledTriggerInvocationCancelled, markScheduledTriggerInvocationCompleted, markScheduledTriggerInvocationFailed, markScheduledTriggerInvocationRunning, resetCancelledInvocationToPending, updateScheduledTriggerInvocationStatus };