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

export const UserRegistryContract: ServiceContract = {
  id: 'matrix.user.registry',
  description: 'Registry for the active user Space.',
  ops: {
    'registry.list': {
      description: 'List actor/service claims for the active user Space.',
      payload: {},
      response: {
        ok: { type: 'boolean', required: true },
        claims: { type: 'array', required: false },
        entries: { type: 'array', required: false },
      },
    },
    'registry.query': {
      description: 'Query actor/service claims for the active user Space.',
      payload: {
        namespaceRoot: { type: 'string', required: false },
        authorityRoot: { type: 'string', required: false },
        principalId: { type: 'string', required: false },
        busToken: { type: 'string', required: false },
        includeStale: { type: 'boolean', required: false },
        includeOffline: { type: 'boolean', required: false },
      },
      response: {
        ok: { type: 'boolean', required: true },
        claims: { type: 'array', required: false },
        entries: { type: 'array', required: false },
        events: { type: 'array', required: false },
      },
    },
  },
};

export const UserDevicesContract: ServiceContract = {
  id: 'matrix.user.devices',
  description: 'Account-level linked Device inventory for the authenticated user.',
  ops: {
    'devices.list': {
      description: 'List linked Devices for the authenticated account.',
      payload: {
        authorityRoot: { type: 'string', required: false },
        principalId: { type: 'string', required: false },
        busToken: { type: 'string', required: false },
        includeOffline: { type: 'boolean', required: false },
      },
      response: {
        ok: { type: 'boolean', required: true },
        devices: { type: 'array', required: true },
      },
    },
    'devices.get': {
      description: 'Read one linked Device for the authenticated account.',
      payload: {
        deviceId: { type: 'string', required: true },
      },
      response: {
        ok: { type: 'boolean', required: true },
        device: { type: 'object', required: false },
      },
    },
    'devices.revoke': {
      description: 'Revoke/unlink one account-level device credential.',
      payload: {
        authorityRoot: { type: 'string', required: false },
        deviceId: { type: 'string', required: true },
        principalId: { type: 'string', required: false },
        busToken: { type: 'string', required: false },
      },
      response: {
        ok: { type: 'boolean', required: true },
      },
    },
  },
};

export const UserRuntimesContract: ServiceContract = {
  id: 'matrix.user.runtimes',
  description: 'Runtime inventory/control-plane service for the active user Space.',
  ops: {
    'runtimes.list': {
      description: 'List runtimes for the active user Space.',
      payload: {
        namespaceRoot: { type: 'string', required: false },
        authorityRoot: { type: 'string', required: false },
        principalId: { type: 'string', required: false },
        busToken: { type: 'string', required: false },
        includeStale: { type: 'boolean', required: false },
        includeOffline: { type: 'boolean', required: false },
      },
      response: {
        ok: { type: 'boolean', required: true },
        runtimes: { type: 'array', required: true },
      },
    },
    'runtimes.registered': {
      description: 'List registered runtimes for the active user Space.',
      payload: {
        namespaceRoot: { type: 'string', required: false },
        authorityRoot: { type: 'string', required: false },
        includeStale: { type: 'boolean', required: false },
      },
      response: {
        ok: { type: 'boolean', required: true },
        runtimes: { type: 'array', required: true },
      },
    },
    '$introspect': {
      description: 'Introspect a runtime control-plane mount.',
      payload: {
        depth: { type: 'string', required: false },
      },
      response: {
        ok: { type: 'boolean', required: false },
        mount: { type: 'string', required: false },
        children: { type: 'array', required: false },
      },
    },
  },
};

export const UserAgentsContract: ServiceContract = {
  id: 'matrix.user.agents',
  description: 'Agent/session service for the active user Space.',
  ops: {
    '$sessionList': {
      description: 'List agent sessions for the active user Space.',
      payload: {},
      response: {
        ok: { type: 'boolean', required: false },
        sessions: { type: 'array', required: true },
      },
    },
    'session_state.read': {
      description: 'Read agent session state.',
      payload: {
        sessionId: { type: 'string', required: true },
      },
      response: {
        ok: { type: 'boolean', required: false },
        state: { type: 'object', required: false },
      },
    },
  },
};

export const PlatformAuthContract: ServiceContract = {
  id: 'matrix.platform.auth',
  description: 'Platform authentication/session service.',
  ops: {
    'session.current': {
      description: 'Read current authenticated platform session.',
      payload: {},
      response: {
        ok: { type: 'boolean', required: true },
        principal: { type: 'object', required: false },
      },
    },
    'auth.accountDevices.list': {
      description: 'List account-level registered Devices for the authenticated principal.',
      payload: {
        principalId: { type: 'string', required: false },
        busToken: { type: 'string', required: false },
        includeGrants: { type: 'boolean', required: false },
      },
      response: {
        ok: { type: 'boolean', required: true },
        devices: { type: 'array', required: true },
      },
    },
  },
};

export const PlatformCapabilitiesContract: ServiceContract = {
  id: 'matrix.platform.capabilities',
  description: 'Platform capability resolution and grant service.',
  ops: {
    'capability.check': {
      description: 'Check whether the caller has a capability.',
      payload: {
        capability: { type: 'string', required: true },
      },
      response: {
        ok: { type: 'boolean', required: true },
        allowed: { type: 'boolean', required: true },
      },
    },
  },
};
