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

export const ILoggingService: ServiceContract = {
  id: 'logging',
  description: 'Log capture and query — captures $log.* bus events, FTS5 search',
  ops: {
    'logging.query': {
      description: 'Query log records with filters',
      payload: {
        source: { type: 'string', required: false, description: 'Filter by source actor' },
        target: { type: 'string', required: false, description: 'Filter by target actor' },
        op: { type: 'string', required: false, description: 'Filter by operation' },
        traceId: { type: 'string', required: false, description: 'Filter by trace ID' },
        requestId: { type: 'string', required: false, description: 'Filter by request ID' },
        sessionId: { type: 'string', required: false, description: 'Filter by session ID' },
        resumeId: { type: 'string', required: false, description: 'Filter by browser resume ID' },
        principalId: { type: 'string', required: false, description: 'Filter by principal ID' },
        kind: { type: 'string[]', required: false, description: 'Filter by record kind' },
        phase: { type: 'string[]', required: false, description: 'Filter by phase' },
        since: { type: 'string', required: false, description: 'ISO start time' },
        until: { type: 'string', required: false, description: 'ISO end time' },
        limit: { type: 'number', required: false, description: 'Max results' },
        offset: { type: 'number', required: false, description: 'Pagination offset' },
      },
      response: {
        ok: { type: 'boolean' },
        records: { type: 'InteractionRecord[]' },
        count: { type: 'number' },
      },
    },
    'logging.search': {
      description: 'Full-text search across log records',
      payload: {
        text: { type: 'string', required: true, description: 'Search text' },
        source: { type: 'string', required: false },
        target: { type: 'string', required: false },
        traceId: { type: 'string', required: false },
        requestId: { type: 'string', required: false },
        sessionId: { type: 'string', required: false },
        resumeId: { type: 'string', required: false },
        principalId: { 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[]' },
      },
    },
    'logging.setLevel': {
      description: 'Set log level for a component or globally',
      payload: {
        level: { type: 'string', required: true, description: 'debug|info|warn|error' },
        source: { type: 'string', required: false, description: 'Component mount (omit for global)' },
      },
      response: { ok: { type: 'boolean' } },
    },
    'logging.getLevel': {
      description: 'Get effective log level',
      payload: {
        source: { type: 'string', required: false },
      },
      response: {
        ok: { type: 'boolean' },
        level: { type: 'string' },
        source: { type: 'string' },
      },
    },
    'logging.getConfig': {
      description: 'Get all log level configuration',
      payload: {},
      response: {
        ok: { type: 'boolean' },
        globalLevel: { type: 'string' },
        overrides: { type: 'Record<string, string>' },
      },
    },
    'logging.count': {
      description: 'Count matching log records',
      payload: {
        traceId: { type: 'string', required: false },
        requestId: { type: 'string', required: false },
        sessionId: { type: 'string', required: false },
        resumeId: { type: 'string', required: false },
        principalId: { 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 },
      },
      response: {
        ok: { type: 'boolean' },
        count: { type: 'number' },
      },
    },
  },
};
