import { PoolStatusCount, JobRunFailure, ListJobRunsQuery, JobRunPage, JobRunSummary } from '../jobs/job-run-service.protocol.js';
import { ListEventsQuery, EventPage, EventSummary } from '../events/event-read.protocol.js';
import { StatusHistogram } from '../bridge/bridge.protocol.js';
import { IntegrationRunSummary } from '../integration/integration-run-recorder.protocol.js';
import { CursorSnapshot } from '../integration/integration-cursor-store.protocol.js';
import '../../../job-orchestrator.protocol-ZuJ3ow-O.js';
import '../events/event-bus.protocol.js';
import '../../types/drizzle.js';
import 'drizzle-orm/node-postgres';
import '../jobs/job-orchestration.schema.js';
import 'drizzle-orm/pg-core';
import 'drizzle-orm';
import '@nestjs/common';
import '../events/event-registry.js';
import '../bridge/bridge-delivery.schema.js';
import '../integration/integration-field-diff.protocol.js';
import 'zod';

/**
 * IObservability — read-only composer port for the observability combiner
 * subsystem (ADR-025, OBS-5).
 *
 * Every method:
 *   - Accepts an optional `tenantId`, passed VERBATIM to the owning sibling
 *     port. Observability never re-implements tenant filtering — that is
 *     the owning subsystem's job (jobs / bridge / integration). See
 *     `.claude/skills/observability/SKILL.md` §3.
 *       - `undefined` — "not provided"; sibling default semantics apply.
 *       - `null`      — explicit cross-tenant match (sibling-specific).
 *       - `string`    — filter to that single tenant.
 *   - Returns an empty shape (not throws) when the owning sibling port is
 *     absent in the consumer's DI container. Consumers get a graceful
 *     degradation instead of a boot failure.
 *
 * Phase-1 scope (epic #195): five reads composed from four sibling ports.
 * Cross-subsystem JOIN analytics are deferred to the Cube.js layer
 * (ADR-025 §Phase-1 scope note; skill §5).
 */

/**
 * One chronological entry in a correlation timeline (OBS-LIST-1). Either a
 * `job_run` or a `domain_event` sharing the same `rootRunId`, tagged with a
 * `kind` discriminator and a single `at` timestamp used for ordering.
 */
type CorrelationTimelineEntry = {
    kind: 'job_run';
    at: Date;
    run: JobRunSummary;
} | {
    kind: 'event';
    at: Date;
    event: EventSummary;
};
/**
 * Stitched view of everything correlated to a single `rootRunId`
 * (OBS-LIST-1): the job runs sharing that root plus the domain events whose
 * `metadata.rootRunId` matches, merged into one ascending timeline with a
 * small roll-up summary.
 */
interface CorrelationTimeline {
    rootRunId: string;
    /** Ascending by `at`. Job runs ordered by `createdAt`; events by `occurredAt`. */
    entries: CorrelationTimelineEntry[];
    summary: {
        runCount: number;
        eventCount: number;
        /** Earliest `at` across all entries, or `null` when empty. */
        startedAt: Date | null;
        /** Latest `at` across all entries, or `null` when empty. */
        lastActivityAt: Date | null;
    };
}
interface IObservability {
    /**
     * Live `(pool, status)` counts across `job_run`. Delegates to
     * `IJobRunService.countByPoolAndStatus`.
     *
     * Empty array when the jobs subsystem is not installed.
     */
    getPoolDepths(tenantId?: string | null): Promise<PoolStatusCount[]>;
    /**
     * Most-recent `failed` job runs, newest first. Delegates to
     * `IJobRunService.listRecentFailed`.
     *
     * Empty array when the jobs subsystem is not installed.
     */
    getRecentFailedJobs(limit: number, tenantId?: string | null): Promise<JobRunFailure[]>;
    /**
     * Bridge-delivery status counts over a trailing window. Delegates to
     * `IJobBridge.getStatusHistogram`.
     *
     * Returns an all-zero histogram (`{ pending: 0, delivered: 0, skipped: 0,
     * failed: 0 }`) when the bridge subsystem is not installed — matches the
     * bridge protocol's "fixed keys, zero-filled" contract.
     */
    getBridgeDeliveryHistogram(windowHours: number, tenantId?: string | null): Promise<StatusHistogram>;
    /**
     * Recent `integration_runs` (optionally filtered by subscription). Delegates to
     * `IIntegrationRunRecorder.listRecent`.
     *
     * Empty array when the integration subsystem is not installed.
     */
    getRecentIntegrationRuns(limit: number, subscriptionId?: string, tenantId?: string | null): Promise<IntegrationRunSummary[]>;
    /**
     * Cursor state per enabled `integration_subscriptions` row. Delegates to
     * `ICursorStore.listAll`.
     *
     * Empty array when the integration subsystem is not installed.
     */
    getCursors(tenantId?: string | null): Promise<CursorSnapshot[]>;
    /**
     * Paginated, filterable `job_run` list (OBS-LIST-1). Delegates to
     * `IJobRunService.listJobRuns`. Keyset pagination on `created_at`.
     *
     * Returns an empty page (`{ items: [], nextCursor: null }`) when the jobs
     * subsystem is not installed.
     */
    listJobRuns(query?: ListJobRunsQuery): Promise<JobRunPage>;
    /**
     * Paginated, filterable `domain_events` list (OBS-LIST-1). Delegates to
     * `IEventReadPort.listEvents`. Keyset pagination on `occurred_at`.
     *
     * Returns an empty page when the events read port is not installed (e.g.
     * the events subsystem is absent, or its backend is `redis` which retains
     * no history).
     */
    listEvents(query?: ListEventsQuery): Promise<EventPage>;
    /**
     * Stitch the job runs and domain events sharing a `rootRunId` into a
     * single ascending timeline + summary (OBS-LIST-1). Composes
     * `IJobRunService.listJobRuns` (filtered by the run tree) and
     * `IEventReadPort.listEvents({ rootRunId })`.
     *
     * Returns an empty timeline (zero counts, null bounds) when neither the
     * jobs subsystem nor the events read port is installed.
     */
    getCorrelationTimeline(rootRunId: string, tenantId?: string | null): Promise<CorrelationTimeline>;
}

export { type CorrelationTimeline, type CorrelationTimelineEntry, CursorSnapshot, EventPage, EventSummary, type IObservability, IntegrationRunSummary, JobRunFailure, JobRunPage, JobRunSummary, ListEventsQuery, ListJobRunsQuery, PoolStatusCount, StatusHistogram };
