{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/codai-ro/json-schema/codai-ro-environment-detail-schema.json",
  "title": "Environment detail",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/codai-ro-projects-environments-api-openapi.yml#/components/schemas/EnvironmentDetail",
  "type": "object",
  "required": [
    "environment",
    "isolation",
    "role",
    "members",
    "ports",
    "events"
  ],
  "properties": {
    "environment": {
      "$ref": "#/$defs/Environment"
    },
    "isolation": {
      "$ref": "#/$defs/ProjectIsolation"
    },
    "role": {
      "allOf": [
        {
          "$ref": "#/$defs/EnvironmentRole"
        }
      ],
      "description": "Your effective role (owner / org admin → `owner`; plain org member → `viewer`)."
    },
    "members": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/EnvironmentMember"
      }
    },
    "ports": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/EnvironmentPort"
      }
    },
    "events": {
      "type": "array",
      "maxItems": 20,
      "description": "The 20 most recent events, newest first.",
      "items": {
        "$ref": "#/$defs/EnvironmentEvent"
      }
    }
  },
  "$defs": {
    "Environment": {
      "type": "object",
      "title": "Environment",
      "description": "The stored environment row (camelCase, as persisted) plus snake_case `for_user_id` and, when known, the project's `isolation`. `metadata` never contains the enrolment token hash.",
      "required": [
        "id",
        "projectId",
        "ownerUserId",
        "orgId",
        "forUserId",
        "for_user_id",
        "name",
        "slug",
        "provider",
        "region",
        "size",
        "state",
        "stateReason",
        "deviceId",
        "providerRef",
        "publicIp",
        "idleTimeoutMinutes",
        "lastActivityAt",
        "startedAt",
        "stoppedAt",
        "hourlyRateMicroEur",
        "includedHoursUsedSeconds",
        "metadata",
        "createdAt",
        "updatedAt"
      ],
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "projectId": {
          "type": "string",
          "format": "uuid"
        },
        "ownerUserId": {
          "type": "string",
          "format": "uuid"
        },
        "orgId": {
          "type": [
            "string",
            "null"
          ],
          "format": "uuid"
        },
        "forUserId": {
          "type": [
            "string",
            "null"
          ],
          "format": "uuid",
          "description": "In `vm_per_dev` projects, the developer this machine is pinned to."
        },
        "for_user_id": {
          "type": [
            "string",
            "null"
          ],
          "format": "uuid",
          "description": "Same as `forUserId` (snake_case convenience)."
        },
        "isolation": {
          "$ref": "#/$defs/ProjectIsolation"
        },
        "name": {
          "type": "string"
        },
        "slug": {
          "type": "string",
          "description": "Globally unique preview hostname label (`https://<port>-<slug>.preview.codai.ro`)."
        },
        "provider": {
          "$ref": "#/$defs/EnvironmentProvider"
        },
        "region": {
          "type": [
            "string",
            "null"
          ]
        },
        "size": {
          "type": [
            "string",
            "null"
          ]
        },
        "state": {
          "$ref": "#/$defs/EnvironmentState"
        },
        "stateReason": {
          "type": [
            "string",
            "null"
          ]
        },
        "deviceId": {
          "type": [
            "string",
            "null"
          ],
          "format": "uuid",
          "description": "The daemon's device row once enrolled."
        },
        "providerRef": {
          "type": [
            "string",
            "null"
          ],
          "description": "Provider-side id (server id, container id)."
        },
        "publicIp": {
          "type": [
            "string",
            "null"
          ]
        },
        "idleTimeoutMinutes": {
          "type": "integer",
          "minimum": 5,
          "maximum": 1440,
          "default": 30
        },
        "lastActivityAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "startedAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "stoppedAt": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time"
        },
        "hourlyRateMicroEur": {
          "type": [
            "integer",
            "null"
          ],
          "description": "Managed providers only; micro-EUR per hour."
        },
        "includedHoursUsedSeconds": {
          "type": "integer"
        },
        "metadata": {
          "type": "object",
          "additionalProperties": true,
          "description": "Provider/daemon facts, e.g. `daemon: { version, os, arch, enrolled_at }`, `budget_micro_eur`."
        },
        "prebuild": {
          "type": "boolean",
          "default": false,
          "description": "True for an unclaimed prebuild pool machine (owned by the project owner); cleared when claimed."
        },
        "createdAt": {
          "type": "string",
          "format": "date-time"
        },
        "updatedAt": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "EnvironmentEvent": {
      "type": "object",
      "title": "Environment event",
      "description": "One append-only audit row (camelCase, as stored).",
      "required": [
        "id",
        "environmentId",
        "actorUserId",
        "actorKind",
        "kind",
        "payload",
        "createdAt"
      ],
      "properties": {
        "id": {
          "type": "integer"
        },
        "environmentId": {
          "type": "string",
          "format": "uuid"
        },
        "actorUserId": {
          "type": [
            "string",
            "null"
          ],
          "format": "uuid"
        },
        "actorKind": {
          "type": "string",
          "enum": [
            "user",
            "agent",
            "system",
            "daemon"
          ]
        },
        "kind": {
          "type": "string",
          "description": "`created`, `updated`, `state:<from>-><to>`, `enrolled`, `enroll_token_issued`, `member_added`, `member_role_changed`, `member_removed`, `member_ssh_key_set`, `port_visibility_changed`, `secret_set`, `secret_deleted`, …"
        },
        "payload": {
          "type": "object",
          "additionalProperties": true
        },
        "createdAt": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "EnvironmentMember": {
      "type": "object",
      "title": "Environment member",
      "required": [
        "user_id",
        "role",
        "email",
        "name",
        "linux_username",
        "port_range_start",
        "ssh_public_key",
        "provisioned_at",
        "created_at"
      ],
      "properties": {
        "user_id": {
          "type": "string",
          "format": "uuid"
        },
        "role": {
          "$ref": "#/$defs/EnvironmentRole"
        },
        "email": {
          "type": [
            "string",
            "null"
          ],
          "format": "email"
        },
        "name": {
          "type": [
            "string",
            "null"
          ]
        },
        "linux_username": {
          "type": [
            "string",
            "null"
          ],
          "description": "Linux account provisioned for this member inside the environment."
        },
        "port_range_start": {
          "type": [
            "integer",
            "null"
          ],
          "description": "First port of the member's reserved range."
        },
        "ssh_public_key": {
          "type": "boolean",
          "description": "Whether a key is on file (the key itself is only fetched by the daemon)."
        },
        "provisioned_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time",
          "description": "When the daemon last confirmed the account + key as provisioned."
        },
        "created_at": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "EnvironmentPort": {
      "type": "object",
      "title": "Published port",
      "required": [
        "id",
        "environmentId",
        "port",
        "protocol",
        "label",
        "visibility",
        "openedByUserId",
        "createdAt",
        "updatedAt"
      ],
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "environmentId": {
          "type": "string",
          "format": "uuid"
        },
        "port": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        },
        "protocol": {
          "$ref": "#/$defs/PortProtocol"
        },
        "label": {
          "type": [
            "string",
            "null"
          ]
        },
        "visibility": {
          "$ref": "#/$defs/PortVisibility"
        },
        "openedByUserId": {
          "type": [
            "string",
            "null"
          ],
          "format": "uuid"
        },
        "createdAt": {
          "type": "string",
          "format": "date-time"
        },
        "updatedAt": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "EnvironmentProvider": {
      "type": "string",
      "enum": [
        "byo",
        "openstack",
        "hetzner",
        "local",
        "gce"
      ],
      "description": "`byo` (bring your own machine; the daemon enrols with a token) is open to every key. `openstack` / `hetzner` are managed by the worker and billed hourly; `gce` (Google Cloud) offers on-demand CPU sizes `small`/`medium`/`large` (e2-standard-2/4/8) and the GPU size `gpu-l4` (Spot, 30-minute cap); it is available only where the deployment enables it AND to `super_admin` accounts (403 for everyone else); `local` is a developer convenience. Managed providers answer 403 until enabled for your key."
    },
    "EnvironmentRole": {
      "type": "string",
      "enum": [
        "owner",
        "maintainer",
        "developer",
        "viewer"
      ],
      "description": "Role of a member on an environment; `owner` ≥ `maintainer` ≥ `developer` ≥ `viewer`."
    },
    "EnvironmentState": {
      "type": "string",
      "enum": [
        "pending",
        "creating",
        "enrolling",
        "starting",
        "running",
        "stopping",
        "stopped",
        "archiving",
        "archived",
        "destroying",
        "destroyed",
        "error"
      ],
      "description": "Lifecycle state. `byo` starts in `pending` and reaches `enrolling` when the daemon enrols; managed providers start in `creating` and reach `running` once enrolled. `destroyed` is terminal."
    },
    "PortProtocol": {
      "type": "string",
      "enum": [
        "http",
        "https",
        "tcp"
      ]
    },
    "PortVisibility": {
      "type": "string",
      "enum": [
        "owner",
        "org",
        "authenticated",
        "public"
      ],
      "description": "Who may reach a published port through the preview router. `public` needs `maintainer` to set."
    },
    "ProjectIsolation": {
      "type": "string",
      "enum": [
        "shared_vm",
        "container",
        "vm_per_dev"
      ],
      "description": "How a project's environments are shared. `shared_vm` (default) — one machine, many members; `container` — one container per member; `vm_per_dev` — one machine per developer, pinned via `for_user_id`."
    }
  }
}