{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/shruggietech/insonic/v1.1.0/schemas/v1.1.0/backend-plan.schema.json",
  "title": "insonic backend configuration plan",
  "description": "Complete nonsecret initial backend selection. Omitted identities and local defaults are generated by workspace plan. Submit its generated configuration unchanged to reconcile retries.",
  "type": "object",
  "properties": {
    "schema_version": {
      "const": "1.1.0",
      "description": "Version of this public insonic contract."
    },
    "kind": {
      "const": "backend-plan",
      "description": "Document discriminator selected by the master registry."
    },
    "workspace_id": {
      "$ref": "common.schema.json#/$defs/uuid",
      "description": "Stable workspace scope used by all domain records."
    },
    "display_name": {
      "type": "string",
      "minLength": 1,
      "description": "User-facing workspace name."
    },
    "control_directory": {
      "type": "string",
      "minLength": 1,
      "description": "Selected local directory for control files, scratch and caches even when authority is remote."
    },
    "profiles": {
      "type": "object",
      "description": "Three independent required backend selections.",
      "properties": {
        "storage": {
          "type": "object",
          "description": "Selected artifact storage profile with an immutable configuration revision.",
          "properties": {
            "profile_id": {
              "$ref": "common.schema.json#/$defs/uuid",
              "description": "Stable configured profile identity."
            },
            "profile_revision": {
              "$ref": "common.schema.json#/$defs/revision",
              "description": "Selected profile configuration revision."
            },
            "adapter_id": {
              "type": "string",
              "enum": [
                "filesystem",
                "s3"
              ],
              "description": "Backend adapter selected for this role."
            },
            "contract_version": {
              "type": "string",
              "minLength": 1,
              "description": "Versioned insonic adapter contract."
            },
            "configuration": {
              "oneOf": [
                {
                  "$ref": "workspace-config.schema.json#/$defs/filesystemConfiguration",
                  "description": "Default local filesystem artifact store. The root is a selected location, not a source identity."
                },
                {
                  "$ref": "workspace-config.schema.json#/$defs/s3Configuration",
                  "description": "Generic S3 API configuration, applicable to remote providers and user-run filesystem-backed S3 endpoints."
                }
              ],
              "description": "Nonsecret adapter configuration matching adapter_id; conditional rules identify the exact permitted shape."
            },
            "expected_backend_version": {
              "type": "string",
              "minLength": 1,
              "description": "Optional tested backend version constraint; health/capability checks verify it before use."
            }
          },
          "required": [
            "adapter_id",
            "configuration"
          ],
          "additionalProperties": false,
          "allOf": [
            {
              "if": {
                "properties": {
                  "adapter_id": {
                    "const": "filesystem",
                    "description": "Backend adapter selected for this role."
                  }
                },
                "required": [
                  "adapter_id"
                ],
                "type": "object"
              },
              "then": {
                "properties": {
                  "configuration": {
                    "$ref": "workspace-config.schema.json#/$defs/filesystemConfiguration",
                    "description": "Default local filesystem artifact store. The root is a selected location, not a source identity."
                  }
                },
                "type": "object"
              }
            },
            {
              "if": {
                "properties": {
                  "adapter_id": {
                    "const": "s3",
                    "description": "Backend adapter selected for this role."
                  }
                },
                "required": [
                  "adapter_id"
                ],
                "type": "object"
              },
              "then": {
                "properties": {
                  "configuration": {
                    "$ref": "workspace-config.schema.json#/$defs/s3Configuration",
                    "description": "Generic S3 API configuration, applicable to remote providers and user-run filesystem-backed S3 endpoints."
                  }
                },
                "type": "object"
              }
            }
          ]
        },
        "catalog": {
          "type": "object",
          "description": "Selected operational catalog profile with an immutable configuration revision.",
          "properties": {
            "profile_id": {
              "$ref": "common.schema.json#/$defs/uuid",
              "description": "Stable configured profile identity."
            },
            "profile_revision": {
              "$ref": "common.schema.json#/$defs/revision",
              "description": "Selected profile configuration revision."
            },
            "adapter_id": {
              "type": "string",
              "enum": [
                "sqlite",
                "postgresql"
              ],
              "description": "Backend adapter selected for this role."
            },
            "contract_version": {
              "type": "string",
              "minLength": 1,
              "description": "Versioned insonic adapter contract."
            },
            "configuration": {
              "oneOf": [
                {
                  "$ref": "workspace-config.schema.json#/$defs/sqliteConfiguration",
                  "description": "Default local SQLite catalog configuration."
                },
                {
                  "$ref": "workspace-config.schema.json#/$defs/postgresqlConfiguration",
                  "description": "Full PostgreSQL catalog configuration. Credentials remain outside this document and application transactions retain workspace scope."
                }
              ],
              "description": "Nonsecret adapter configuration matching adapter_id; conditional rules identify the exact permitted shape."
            },
            "expected_backend_version": {
              "type": "string",
              "minLength": 1,
              "description": "Optional tested backend version constraint; health/capability checks verify it before use."
            }
          },
          "required": [
            "adapter_id",
            "configuration"
          ],
          "additionalProperties": false,
          "allOf": [
            {
              "if": {
                "properties": {
                  "adapter_id": {
                    "const": "sqlite",
                    "description": "Backend adapter selected for this role."
                  }
                },
                "required": [
                  "adapter_id"
                ],
                "type": "object"
              },
              "then": {
                "properties": {
                  "configuration": {
                    "$ref": "workspace-config.schema.json#/$defs/sqliteConfiguration",
                    "description": "Default local SQLite catalog configuration."
                  }
                },
                "type": "object"
              }
            },
            {
              "if": {
                "properties": {
                  "adapter_id": {
                    "const": "postgresql",
                    "description": "Backend adapter selected for this role."
                  }
                },
                "required": [
                  "adapter_id"
                ],
                "type": "object"
              },
              "then": {
                "properties": {
                  "configuration": {
                    "$ref": "workspace-config.schema.json#/$defs/postgresqlConfiguration",
                    "description": "Full PostgreSQL catalog configuration. Credentials remain outside this document and application transactions retain workspace scope."
                  }
                },
                "type": "object"
              }
            }
          ]
        },
        "graph": {
          "type": "object",
          "description": "Selected graph projection profile with an immutable configuration revision.",
          "properties": {
            "profile_id": {
              "$ref": "common.schema.json#/$defs/uuid",
              "description": "Stable configured profile identity."
            },
            "profile_revision": {
              "$ref": "common.schema.json#/$defs/revision",
              "description": "Selected profile configuration revision."
            },
            "adapter_id": {
              "type": "string",
              "enum": [
                "ladybugdb",
                "arcadedb",
                "community"
              ],
              "description": "Backend adapter selected for this role."
            },
            "contract_version": {
              "type": "string",
              "minLength": 1,
              "description": "Versioned insonic adapter contract."
            },
            "configuration": {
              "oneOf": [
                {
                  "$ref": "workspace-config.schema.json#/$defs/ladybugdbConfiguration",
                  "description": "Default embedded LadybugDB projection attached to the owning runtime."
                },
                {
                  "$ref": "workspace-config.schema.json#/$defs/arcadedbConfiguration",
                  "description": "ArcadeDB server graph adapter configuration, distinct from the operational catalog role."
                },
                {
                  "$ref": "workspace-config.schema.json#/$defs/communityConfiguration",
                  "description": "Optional user-provided graph adapter configuration. This does not make its engine officially supported or redistributed."
                }
              ],
              "description": "Nonsecret adapter configuration matching adapter_id; conditional rules identify the exact permitted shape."
            },
            "expected_backend_version": {
              "type": "string",
              "minLength": 1,
              "description": "Optional tested backend version constraint; health/capability checks verify it before use."
            }
          },
          "required": [
            "adapter_id",
            "configuration"
          ],
          "additionalProperties": false,
          "allOf": [
            {
              "if": {
                "properties": {
                  "adapter_id": {
                    "const": "ladybugdb",
                    "description": "Backend adapter selected for this role."
                  }
                },
                "required": [
                  "adapter_id"
                ],
                "type": "object"
              },
              "then": {
                "properties": {
                  "configuration": {
                    "$ref": "workspace-config.schema.json#/$defs/ladybugdbConfiguration",
                    "description": "Default embedded LadybugDB projection attached to the owning runtime."
                  }
                },
                "type": "object"
              }
            },
            {
              "if": {
                "properties": {
                  "adapter_id": {
                    "const": "arcadedb",
                    "description": "Backend adapter selected for this role."
                  }
                },
                "required": [
                  "adapter_id"
                ],
                "type": "object"
              },
              "then": {
                "properties": {
                  "configuration": {
                    "$ref": "workspace-config.schema.json#/$defs/arcadedbConfiguration",
                    "description": "ArcadeDB server graph adapter configuration, distinct from the operational catalog role."
                  }
                },
                "type": "object"
              }
            },
            {
              "if": {
                "properties": {
                  "adapter_id": {
                    "const": "community",
                    "description": "Backend adapter selected for this role."
                  }
                },
                "required": [
                  "adapter_id"
                ],
                "type": "object"
              },
              "then": {
                "properties": {
                  "configuration": {
                    "$ref": "workspace-config.schema.json#/$defs/communityConfiguration",
                    "description": "Optional user-provided graph adapter configuration. This does not make its engine officially supported or redistributed."
                  }
                },
                "type": "object"
              }
            }
          ]
        }
      },
      "required": [],
      "additionalProperties": false
    },
    "default_import_timezone": {
      "$ref": "common.schema.json#/$defs/timezone",
      "description": "Default timezone captured when creating an import batch."
    },
    "extensions": {
      "$ref": "common.schema.json#/$defs/extensions",
      "description": "Optional namespaced nonsecret extension data."
    },
    "credential_namespace_id": {
      "$ref": "common.schema.json#/$defs/uuid",
      "description": "Optional nonsecret local credential-store namespace, defaulting to workspace_id. Restore preserves the independently configured destination namespace when adopting source catalog identity; it never copies credential values."
    }
  },
  "required": [
    "schema_version",
    "kind",
    "display_name"
  ],
  "additionalProperties": false,
  "examples": [
    {
      "schema_version": "1.1.0",
      "kind": "backend-plan",
      "display_name": "Example library",
      "profiles": {
        "storage": {
          "adapter_id": "filesystem",
          "configuration": {
            "root": "artifacts"
          }
        },
        "catalog": {
          "adapter_id": "sqlite",
          "configuration": {
            "path": "catalog.sqlite"
          }
        },
        "graph": {
          "adapter_id": "ladybugdb",
          "configuration": {
            "path": "graph/library.lbug"
          }
        }
      }
    }
  ]
}
