import { buildAuthInspectionSnapshot } from './auth/inspection.js';
import { buildProviderAccountSnapshot } from './provider-accounts/registry.js';
import { getProviderRuntimeSnapshot, getProviderUsageSnapshot, listProviderRuntimeSnapshots } from '../providers/runtime-snapshot.js';
function normalizeLimit(limit) {
    if (!Number.isFinite(limit))
        return 1;
    return Math.max(1, Math.floor(limit));
}
function listTasksSnapshot(snapshot, limit = 100) {
    return snapshot.tasks.slice(0, normalizeLimit(limit));
}
function getTaskSnapshot(snapshot, taskId) {
    return snapshot.tasks.find((task) => task.id === taskId) ?? null;
}
export function createOperatorClient(services) {
    const sessions = {
        current: () => services.readModels.session.getSnapshot(),
        list: (limit = 100) => services.sessionBroker.listSessions(normalizeLimit(limit)),
        get: (sessionId) => services.sessionBroker.getSession(sessionId),
        messages: (sessionId, limit = 100) => services.sessionBroker.getMessages(sessionId, normalizeLimit(limit)),
        inputs: (sessionId, limit = 100) => services.sessionBroker.getInputs(sessionId, normalizeLimit(limit)),
        ensureSession: (input = {}) => services.sessionBroker.ensureSession(input),
        close: (sessionId) => services.sessionBroker.closeSession(sessionId),
        reopen: (sessionId) => services.sessionBroker.reopenSession(sessionId),
        bindAgent: (sessionId, agentId) => services.sessionBroker.bindAgent(sessionId, agentId),
        submitMessage: (input) => services.sessionBroker.submitMessage(input),
        steerMessage: (input) => services.sessionBroker.steerMessage(input),
        followUpMessage: (input) => services.sessionBroker.followUpMessage(input),
        cancelInput: (sessionId, inputId) => services.sessionBroker.cancelInput(sessionId, inputId),
    };
    const tasks = {
        snapshot: () => services.readModels.tasks.getSnapshot(),
        list: (limit = 100) => listTasksSnapshot(services.readModels.tasks.getSnapshot(), limit),
        get: (taskId) => getTaskSnapshot(services.readModels.tasks.getSnapshot(), taskId),
        running: () => services.readModels.tasks.getSnapshot().tasks.filter((task) => task.status === 'running'),
    };
    const approvals = {
        list: (limit = 100) => services.approvalBroker.listApprovals(normalizeLimit(limit)),
        get: (approvalId) => services.approvalBroker.getApproval(approvalId),
        request: (input) => services.approvalBroker.requestApproval(input),
        claim: (approvalId, actor, actorSurface = 'operator', note) => services.approvalBroker.claimApproval(approvalId, actor, actorSurface, note),
        resolve: (approvalId, input) => services.approvalBroker.resolveApproval(approvalId, input),
        approve: (approvalId, actor, actorSurface = 'operator', note) => services.approvalBroker.resolveApproval(approvalId, {
            approved: true,
            actor,
            actorSurface,
            note,
        }),
        deny: (approvalId, actor, actorSurface = 'operator', note) => services.approvalBroker.resolveApproval(approvalId, {
            approved: false,
            actor,
            actorSurface,
            note,
        }),
        cancel: (approvalId, actor, actorSurface = 'operator', note) => services.approvalBroker.cancelApproval(approvalId, actor, actorSurface, note),
        update: (approvalId, input) => services.approvalBroker.recordRemoteUpdate(approvalId, input),
    };
    const providers = {
        listIds: () => services.readModels.providers.getSnapshot().providerIds,
        runtimeSnapshots: () => listProviderRuntimeSnapshots(services.providerRegistry),
        runtimeSnapshot: (providerId) => getProviderRuntimeSnapshot(services.providerRegistry, providerId),
        usageSnapshot: (providerId) => getProviderUsageSnapshot(services.providerRegistry, providerId),
        accountSnapshot: () => buildProviderAccountSnapshot({
            providerRegistry: services.providerRegistry,
            serviceRegistry: services.serviceRegistry,
            subscriptionManager: services.subscriptionManager,
            secretsManager: services.secretsManager,
        }),
        authInspection: () => buildAuthInspectionSnapshot({
            serviceRegistry: services.serviceRegistry,
            subscriptionManager: services.subscriptionManager,
            secretsManager: services.secretsManager,
        }),
        snapshot: async () => {
            const [runtimeSnapshots, accountSnapshot, authInspection] = await Promise.all([
                listProviderRuntimeSnapshots(services.providerRegistry),
                buildProviderAccountSnapshot({
                    providerRegistry: services.providerRegistry,
                    serviceRegistry: services.serviceRegistry,
                    subscriptionManager: services.subscriptionManager,
                    secretsManager: services.secretsManager,
                }),
                buildAuthInspectionSnapshot({
                    serviceRegistry: services.serviceRegistry,
                    subscriptionManager: services.subscriptionManager,
                    secretsManager: services.secretsManager,
                }),
            ]);
            return {
                providerIds: services.readModels.providers.getSnapshot().providerIds,
                runtimeSnapshots,
                accountSnapshot,
                authInspection,
            };
        },
    };
    const controlPlane = {
        snapshot: () => services.readModels.controlPlane.getSnapshot(),
        recentEvents: (limit = 6) => services.readModels.controlPlane.getSnapshot().recentEvents.slice(0, normalizeLimit(limit)),
    };
    return Object.freeze({
        sessions,
        tasks,
        approvals,
        providers,
        controlPlane,
        events: services.events,
        shellPaths: services.shellPaths,
    });
}
