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

export const IStorageService: ServiceContract = {
  id: 'sqlite',
  description: 'Per-package SQLite database manager — namespace-partitioned storage',
  ops: {
    'sqlite.query': {
      description: 'SELECT — returns rows',
      payload: {
        domain: { type: 'string', required: true, description: 'Package database domain' },
        sql: { type: 'string', required: true, description: 'SQL SELECT statement' },
        params: { type: 'unknown[]', required: false, description: 'Bind parameters' },
      },
      response: { ok: { type: 'boolean' }, rows: { type: 'object[]' }, count: { type: 'number' } },
    },
    'sqlite.execute': {
      description: 'INSERT/UPDATE/DELETE — returns changes count',
      payload: {
        domain: { type: 'string', required: true },
        sql: { type: 'string', required: true },
        params: { type: 'unknown[]', required: false },
      },
      response: { ok: { type: 'boolean' }, changes: { type: 'number' } },
    },
    'sqlite.batch': {
      description: 'Transaction — multiple statements atomically',
      payload: {
        domain: { type: 'string', required: true },
        statements: { type: 'Array<{sql, params}>', required: true },
      },
      response: { ok: { type: 'boolean' }, results: { type: 'object[]' }, count: { type: 'number' } },
    },
    'sqlite.resolve-namespace': {
      description: 'Mount path → namespace ID (auto-create)',
      payload: {
        domain: { type: 'string', required: true },
        mount: { type: 'string', required: true },
      },
      response: { ok: { type: 'boolean' }, namespaceId: { type: 'number' }, mount: { type: 'string' } },
    },
    'sqlite.init-schema': {
      description: 'Create/open database + run schema for a domain (privileged)',
      payload: {
        domain: { type: 'string', required: true },
        schema: { type: 'string', required: true, description: 'SQL schema to execute' },
        packageName: { type: 'string', required: false },
        version: { type: 'number', required: false },
      },
      response: { ok: { type: 'boolean' }, domain: { type: 'string' }, status: { type: 'string' } },
    },
    'sqlite.drop-domain': {
      description: 'Delete database for uninstalled package (privileged)',
      payload: { domain: { type: 'string', required: true } },
      response: { ok: { type: 'boolean' }, domain: { type: 'string' }, status: { type: 'string' } },
    },
    'sqlite.status': {
      description: 'List managed databases + stats',
      payload: {},
      response: {
        ok: { type: 'boolean' },
        dataDir: { type: 'string' },
        databases: { type: 'object[]' },
        openCount: { type: 'number' },
        totalCount: { type: 'number' },
      },
    },
  },
};
