import type { ServiceContract } from '../ServiceContract';

export const ITracingService: ServiceContract = {
  id: 'tracing',
  description: 'Distributed trace queries — trace, session, request threading',
  ops: {
    'trace.get': {
      description: 'Get all records for a trace ID',
      payload: { traceId: { type: 'string', required: true } },
      response: { ok: { type: 'boolean' }, records: { type: 'InteractionRecord[]' } },
    },
    'trace.query': {
      description: 'Query traces with full filter support',
      payload: {
        traceId: { type: 'string', required: false },
        sessionId: { type: 'string', required: false },
        resumeId: { type: 'string', required: false },
        requestId: { type: 'string', required: false },
        sessionGroup: { type: 'string', required: false },
        principalId: { type: 'string', required: false },
        source: { type: 'string', required: false },
        target: { type: 'string', required: false },
        op: { type: 'string', required: false },
        phase: { type: 'string[]', required: false },
        role: { type: 'string', required: false },
        modelId: { type: 'string', required: false },
        channel: { type: 'string', required: false },
        since: { type: 'string', required: false },
        until: { type: 'string', required: false },
        kind: { type: 'string[]', required: false },
        limit: { type: 'number', required: false },
        offset: { type: 'number', required: false },
      },
      response: { ok: { type: 'boolean' }, records: { type: 'InteractionRecord[]' } },
    },
    'trace.search': {
      description: 'Full-text search across traces',
      payload: {
        text: { type: 'string', required: true },
        traceId: { type: 'string', required: false },
        sessionId: { type: 'string', required: false },
        resumeId: { type: 'string', required: false },
        requestId: { type: 'string', required: false },
        source: { type: 'string', required: false },
        target: { type: 'string', required: false },
        op: { type: 'string', required: false },
        kind: { type: 'string[]', required: false },
        phase: { type: 'string[]', required: false },
        since: { type: 'string', required: false },
        until: { type: 'string', required: false },
        limit: { type: 'number', required: false },
        offset: { type: 'number', required: false },
      },
      response: { ok: { type: 'boolean' }, records: { type: 'InteractionRecord[]' } },
    },
    'trace.sample': {
      description: 'Get recent traces for overview',
      payload: { limit: { type: 'number', required: false } },
      response: { ok: { type: 'boolean' }, traces: { type: 'object[]' } },
    },
    'trace.request': {
      description: 'Get all records for a request cycle',
      payload: { requestId: { type: 'string', required: true } },
      response: { ok: { type: 'boolean' }, records: { type: 'InteractionRecord[]' } },
    },
    'trace.thread': {
      description: 'Get conversation thread (messages + tool-calls)',
      payload: { sessionId: { type: 'string', required: true }, limit: { type: 'number', required: false } },
      response: { ok: { type: 'boolean' }, records: { type: 'InteractionRecord[]' } },
    },
    'trace.state': {
      description: 'Get latest state snapshot for an actor mount',
      payload: { source: { type: 'string', required: true } },
      response: { ok: { type: 'boolean' }, record: { type: 'InteractionRecord | null' } },
    },
    'trace.count': {
      description: 'Count matching trace records',
      payload: {
        traceId: { type: 'string', required: false },
        sessionId: { type: 'string', required: false },
        resumeId: { type: 'string', required: false },
        requestId: { type: 'string', required: false },
        principalId: { type: 'string', required: false },
        source: { type: 'string', required: false },
        target: { type: 'string', required: false },
        op: { type: 'string', required: false },
        phase: { type: 'string[]', required: false },
        kind: { type: 'string[]', required: false },
        since: { type: 'string', required: false },
        until: { type: 'string', required: false },
      },
      response: { ok: { type: 'boolean' }, count: { type: 'number' } },
    },
    'trace.activity': {
      description: 'Get activity summary (ops, counts, timing)',
      payload: { mount: { type: 'string', required: false }, limit: { type: 'number', required: false } },
      response: { ok: { type: 'boolean' }, activity: { type: 'object[]' } },
    },
  },
};
