# Service Registry Contracts
#
# Type definitions for platform services
# Migrated from provisioning/kcl/services.k

{
  ResourceLimits = {
    cpu_limit | String | optional,
    memory_limit | String | optional,
    disk_limit | String | optional,
  },

  BinaryDeployment = {
    binary_path | String,
    args | Array String | default = [],
    working_dir | String | optional,
    env | { _ | String } | default = {},
    user | String | optional,
    group | String | optional,
  },

  DockerDeployment = {
    image | String,
    container_name | String,
    ports | Array String | default = [],
    volumes | Array String | default = [],
    environment | { _ | String } | default = {},
    command | Array String | optional,
    networks | Array String | default = [],
    restart_policy | [| 'no, 'always, 'on_failure, 'unless_stopped |] | default = 'unless_stopped,
  },

  DockerComposeDeployment = {
    compose_file | String,
    service_name | String,
    project_name | String | optional,
    env_file | String | optional,
  },

  HelmChart = {
    chart | String,
    release_name | String,
    repo_url | String | optional,
    version | String | optional,
    values_file | String | optional,
  },

  KubernetesDeployment = {
    namespace | String,
    deployment_name | String,
    kubeconfig | String | optional,
    manifests_path | String | optional,
    helm_chart | HelmChart | optional,
  },

  RemoteDeployment = {
    endpoint | String,
    tls_enabled | Bool | default = true,
    auth_token_path | String | optional,
    cert_path | String | optional,
  },

  ServiceDeployment = {
    mode | [| 'binary, 'docker, 'docker_compose, 'kubernetes, 'remote |],
    binary | BinaryDeployment | optional,
    docker | DockerDeployment | optional,
    docker_compose | DockerComposeDeployment | optional,
    kubernetes | KubernetesDeployment | optional,
    remote | RemoteDeployment | optional,
  },

  HttpHealthCheck = {
    endpoint | String,
    expected_status | Number | default = 200,
    method | [| 'GET, 'POST, 'HEAD |] | default = 'GET,
    headers | { _ | String } | default = {},
  },

  TcpHealthCheck = {
    host | String,
    port | Number,
  },

  CommandHealthCheck = {
    command | String,
    expected_exit_code | Number | default = 0,
  },

  FileHealthCheck = {
    path | String,
    must_exist | Bool | default = true,
  },

  HealthCheck = {
    type | [| 'http, 'tcp, 'command, 'file, 'none |],
    http | HttpHealthCheck | optional,
    tcp | TcpHealthCheck | optional,
    command | CommandHealthCheck | optional,
    file | FileHealthCheck | optional,
    interval | Number | default = 10,
    retries | Number | default = 3,
    timeout | Number | default = 5,
  },

  StartupConfig = {
    auto_start | Bool | default = false,
    start_timeout | Number | default = 60,
    start_order | Number | default = 100,
    restart_on_failure | Bool | default = true,
    max_restarts | Number | default = 3,
  },

  ServiceDefinition = {
    name | String,
    type | [| 'platform, 'infrastructure, 'utility |],
    category | [| 'orchestration, 'auth, 'dns, 'git, 'registry, 'api, 'ui, 'monitoring |],
    description | String | optional,
    required_for | Array String | default = [],
    dependencies | Array String | default = [],
    conflicts | Array String | default = [],
    deployment | ServiceDeployment,
    health_check | HealthCheck,
    startup | StartupConfig | default = { auto_start = false, start_timeout = 60, start_order = 100, restart_on_failure = true, max_restarts = 3 },
    resources | ResourceLimits | optional,
  },

  ServiceRegistry = {
    services | { _ | ServiceDefinition },
  },

  ServiceState = {
    name | String,
    status | [| 'running, 'stopped, 'failed, 'starting, 'stopping, 'unknown |],
    pid | Number | optional,
    started_at | String | optional,
    uptime | Number | optional,
    health_status | [| 'healthy, 'unhealthy, 'unknown |] | default = 'unknown,
    last_health_check | String | optional,
    restart_count | Number | default = 0,
  },

  ServiceOperation = {
    service_name | String,
    operation | [| 'start, 'stop, 'restart, 'reload, 'health_check |],
    force | Bool | default = false,
    timeout | Number | optional,
  },
}
