import { ModuleManifest, ModuleContext, CollectionEntityDefinition, ComputedEntityDefinition, PluginManifest } from '@gzl10/nexus-sdk';

/**
 * Schedules module manifest for the plugin.
 */

declare const schedulesModule: ModuleManifest;

/**
 * Target types for schedules
 */
type ScheduleTargetType = 'service_action' | 'http_internal' | 'http_external' | 'function_registry';
/**
 * Schedule statuses
 */
type ScheduleStatus = 'idle' | 'running' | 'paused' | 'error' | 'retrying';
/**
 * Retry backoff strategies
 */
type RetryBackoff = 'fixed' | 'exponential' | 'linear';
/**
 * Configuration for service_action target
 */
interface ServiceActionConfig {
    service: string;
    action: string;
    input?: Record<string, unknown>;
    recordId?: string;
}
/**
 * Configuration for http_internal target
 */
interface HttpInternalConfig {
    method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
    path: string;
    body?: Record<string, unknown>;
    headers?: Record<string, string>;
}
/**
 * Configuration for http_external target
 */
interface HttpExternalConfig {
    method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
    url: string;
    body?: Record<string, unknown>;
    headers?: Record<string, string>;
    timeout?: number;
}
/**
 * Configuration for function_registry target
 */
interface FunctionRegistryConfig {
    functionKey: string;
    args?: Record<string, unknown>;
}
/**
 * Union of all target configurations
 */
type TargetConfig = ServiceActionConfig | HttpInternalConfig | HttpExternalConfig | FunctionRegistryConfig;
/**
 * Schedule persisted in the DB
 */
interface Schedule {
    id: string;
    name: string;
    description?: string | null;
    cron_expression: string;
    timezone: string;
    target_type: ScheduleTargetType;
    target_config: TargetConfig;
    enabled: boolean;
    status: ScheduleStatus;
    last_run_at?: string | null;
    last_error?: string | null;
    run_count: number;
    max_retries: number;
    retry_count: number;
    retry_delay_ms: number;
    retry_backoff: RetryBackoff;
    created_at: string;
    updated_at: string;
    created_by?: string | null;
    updated_by?: string | null;
}
/**
 * Input for creating a schedule
 */
interface CreateScheduleInput {
    name: string;
    description?: string;
    cron_expression: string;
    timezone?: string;
    target_type: ScheduleTargetType;
    target_config: TargetConfig;
    enabled?: boolean;
    max_retries?: number;
    retry_delay_ms?: number;
    retry_backoff?: RetryBackoff;
}
/**
 * Input for updating a schedule
 */
interface UpdateScheduleInput {
    name?: string;
    description?: string;
    cron_expression?: string;
    timezone?: string;
    target_type?: ScheduleTargetType;
    target_config?: TargetConfig;
    enabled?: boolean;
    max_retries?: number;
    retry_delay_ms?: number;
    retry_backoff?: RetryBackoff;
}
/**
 * Registered function for function_registry
 */
type RegisteredFunction = (args?: Record<string, unknown>) => Promise<unknown>;
/**
 * Execution result
 */
interface ExecutionResult {
    success: boolean;
    data?: unknown;
    error?: string;
    duration_ms: number;
}
/**
 * Next run info
 */
interface NextRunInfo {
    id: string;
    name: string;
    next_run: string;
    cron_expression: string;
}

/**
 * Schedules service with node-cron
 */
declare class SchedulesService {
    private ctx;
    private jobs;
    private retryTimeouts;
    private initialized;
    private initPromise;
    private generateId;
    private safeJsonParse;
    private logger;
    private t;
    constructor(ctx: ModuleContext);
    /**
     * Initializes the service by loading active schedules from the DB.
     * Safe to call multiple times - concurrent calls share the same initialization.
     */
    init(): Promise<void>;
    private doInit;
    /**
     * Stops all jobs
     */
    shutdown(): void;
    /**
     * Creates a new schedule
     */
    create(data: CreateScheduleInput, userId?: string): Promise<Schedule>;
    /**
     * Updates an existing schedule
     */
    update(id: string, data: UpdateScheduleInput, userId?: string): Promise<Schedule>;
    /**
     * Deletes a schedule
     */
    delete(id: string): Promise<void>;
    /**
     * Finds a schedule by ID
     */
    findById(id: string): Promise<Schedule | null>;
    /**
     * Lists all schedules
     */
    findAll(): Promise<Schedule[]>;
    /**
     * Pauses a schedule
     */
    pause(id: string): Promise<void>;
    /**
     * Resumes a paused schedule
     */
    resume(id: string): Promise<void>;
    /**
     * Runs a schedule immediately (manual trigger)
     */
    trigger(id: string): Promise<ExecutionResult>;
    /**
     * Gets upcoming runs
     */
    getNextRuns(limit?: number): Promise<NextRunInfo[]>;
    /**
     * Syncs a job with the DB (for auto-mounted CRUD events)
     */
    syncJob(id: string): Promise<void>;
    /**
     * Cancels a job by ID (public for use from events)
     */
    cancelJobById(id: string): void;
    /**
     * Registers a function for function_registry
     */
    registerFunction(key: string, fn: RegisteredFunction): void;
    /**
     * Unregisters a function
     */
    unregisterFunction(key: string): void;
    /**
     * Lists registered functions
     */
    getRegisteredFunctions(): string[];
    /**
     * Schedules a job with node-cron
     */
    private scheduleJob;
    /**
     * Cancels a job
     */
    private cancelJob;
    /**
     * Executes a schedule with retry support
     */
    private executeSchedule;
    /**
     * Calculates retry delay based on backoff strategy
     */
    private calculateRetryDelay;
    /**
     * Records an execution log
     */
    private logExecution;
    /**
     * Calculates the next run date using cron-parser
     */
    private getNextRunDate;
}
/**
 * Initializes the schedules service
 */
declare function initSchedulesService(ctx: ModuleContext): SchedulesService;
/**
 * Gets the schedules service
 */
declare function getSchedulesService(): SchedulesService;

/**
 * Registers a function for use with function_registry
 */
declare function registerFunction(key: string, fn: RegisteredFunction): void;
/**
 * Unregisters a function
 */
declare function unregisterFunction(key: string): void;
/**
 * Lists all registered functions
 */
declare function listRegisteredFunctions(): string[];
/**
 * Clears the function registry (for testing)
 */
declare function clearFunctionRegistry(): void;
/**
 * Validates that an internal path is safe (no traversal, starts with /).
 */
declare function assertSafeInternalPath(path: string): void;
/**
 * Checks if an IP address belongs to a private/reserved range.
 */
declare function isPrivateIP(ip: string): boolean;
/**
 * Validates that an external URL does not target private/internal networks.
 * Checks both the hostname and resolved IP addresses.
 */
declare function assertSafeExternalUrl(urlStr: string): Promise<void>;

/**
 * Schedule Entity (collection)
 */
declare const scheduleEntity: CollectionEntityDefinition;
/**
 * Schedule Log Entity (event - append-only)
 */
declare const scheduleLogEntity: CollectionEntityDefinition;
/**
 * Schedule Dashboard Entity (computed, single)
 */
declare const dashboardEntity: ComputedEntityDefinition;

/**
 * @module @gzl10/nexus-plugin-schedules
 * @description Cron-based task scheduling with persistence and audit
 */

declare const schedulesPlugin: PluginManifest;

export { type CreateScheduleInput, type ExecutionResult, type FunctionRegistryConfig, type HttpExternalConfig, type HttpInternalConfig, type NextRunInfo, type RegisteredFunction, type RetryBackoff, type Schedule, type ScheduleStatus, type ScheduleTargetType, SchedulesService, type ServiceActionConfig, type TargetConfig, type UpdateScheduleInput, assertSafeExternalUrl, assertSafeInternalPath, clearFunctionRegistry, dashboardEntity, schedulesPlugin as default, getSchedulesService, initSchedulesService, isPrivateIP, listRegisteredFunctions, registerFunction, scheduleEntity, scheduleLogEntity, schedulesModule, schedulesPlugin, unregisterFunction };
