{
  "$schema": "https://json-schema.org/draft-07/schema#",
  "$id": "https://multiflexi.eu/schema/application.json",
  "schemaVersion": "3.4.0",
  "title": "MultiFlexi App Definition",
  "type": "object",
  "$defs": {
    "localizedString": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "object",
          "minProperties": 1,
          "properties": {
            "en": {
              "type": "string"
            },
            "cs": {
              "type": "string"
            },
            "sk": {
              "type": "string"
            },
            "de": {
              "type": "string"
            },
            "fr": {
              "type": "string"
            },
            "es": {
              "type": "string"
            },
            "it": {
              "type": "string"
            },
            "pl": {
              "type": "string"
            },
            "nl": {
              "type": "string"
            },
            "pt": {
              "type": "string"
            },
            "sv": {
              "type": "string"
            },
            "fi": {
              "type": "string"
            },
            "da": {
              "type": "string"
            },
            "no": {
              "type": "string"
            },
            "hu": {
              "type": "string"
            },
            "ro": {
              "type": "string"
            },
            "bg": {
              "type": "string"
            },
            "el": {
              "type": "string"
            },
            "tr": {
              "type": "string"
            },
            "hr": {
              "type": "string"
            },
            "sl": {
              "type": "string"
            },
            "et": {
              "type": "string"
            },
            "lt": {
              "type": "string"
            },
            "lv": {
              "type": "string"
            },
            "ru": {
              "type": "string"
            },
            "uk": {
              "type": "string"
            },
            "ja": {
              "type": "string"
            },
            "zh": {
              "type": "string"
            },
            "ko": {
              "type": "string"
            },
            "ar": {
              "type": "string"
            },
            "he": {
              "type": "string"
            },
            "hi": {
              "type": "string"
            }
          },
          "additionalProperties": false
        }
      ]
    }
  },
  "properties": {
    "$schema": {
      "type": "string",
      "format": "uri",
      "$comment": "Schema file location",
      "description": "Schema file location.",
      "default": "https://multiflexi.eu/schema/application.json"
    },
    "image": {
      "type": "string",
      "description": "Application image or icon URL."
    },
    "title": {
      "$ref": "#/$defs/localizedString"
    },
    "name": {
      "$ref": "#/$defs/localizedString"
    },
    "description": {
      "$ref": "#/$defs/localizedString"
    },
    "author": {
      "type": "string",
      "description": "Author of the application."
    },
    "license": {
      "type": "string",
      "description": "License identifier or URL."
    },
    "category": {
      "type": "string",
      "description": "Category of the application."
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "Topics: Keywords or tags related to the application."
    },
    "homepage": {
      "type": "string",
      "format": "uri"
    },
    "uuid": {
      "type": "string",
      "format": "uuid"
    },
    "version": {
      "type": "string",
      "description": "Application version.",
      "pattern": "^(0|[1-9]\\d*)(\\.(0|[1-9]\\d*))?(\\.(0|[1-9]\\d*))?(\\.(0|[1-9]\\d*))?$",
      "examples": [
       "1",
       "1.2",
       "1.2.3"
      ]
    },
    "schemaVersion": {
      "type": "string",
      "description": "Schema version used by this application."
    },
    "ociimage": {
      "type": "string",
      "description": "OCI image reference for the application.",
      "pattern": "^[\\w./:-]+(:[\\w.-]+)?$"
    },
    "executable": {
      "type": "string"
    },
    "cmdparamsTemplate": {
      "type": "string",
      "description": "Command line template supporting placeholders like ${VAR}."
    },
    "environment": {
      "type": "object",
      "description": "Configuration keys required by the application.",
      "patternProperties": {
        "^[A-Z0-9_]+$": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string",
              "enum": [
                "string",
                "file-path",
                "email",
                "url",
                "integer",
                "float",
                "bool",
                "password",
                "set",
                "text"
              ],
              "description": "The data type of the environment variable."
            },
            "category": {
              "type": "string",
              "enum": ["API", "Database", "Behavior", "Security", "Other"],
              "description": "Group or category of this configuration key."
            },
            "description": {
              "$ref": "#/$defs/localizedString"
            },
            "hint": {
              "$ref": "#/$defs/localizedString"
            },
            "defval": {},
            "required": {
              "type": "boolean"
            },
            "options": {
              "type": ["object", "array"]
            }
          },
          "additionalProperties": true
        }
      }
    },
    "artifacts": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "$ref": "#/$defs/localizedString"
          },
          "path": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "description": {
            "$ref": "#/$defs/localizedString"
          }
        },
        "required": ["name", "path", "type"],
        "additionalProperties": false
      }
    },
    "produces": {
      "type": "object",
      "patternProperties": {
        "^[a-zA-Z0-9_-]+$": {
          "type": "object",
          "properties": {
            "description": {
              "$ref": "#/$defs/localizedString"
            },
            "patterns": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "format": {
              "type": "string",
              "enum": ["file", "json", "text", "url", "custom"]
            },
            "fields": {
              "type": "object",
              "description": "Named items inside this output, for job-chaining mapping editors (e.g. Node-RED) to offer as selectors. Metadata only - the actual data lives in the produced file/output.",
              "patternProperties": {
                "^[a-zA-Z0-9_-]+$": {
                  "type": "object",
                  "properties": {
                    "type": {
                      "type": "string",
                      "enum": ["string", "integer", "float", "boolean"]
                    },
                    "description": {
                      "$ref": "#/$defs/localizedString"
                    },
                    "path": {
                      "type": "string",
                      "description": "JSONPath into the produced JSON, when it differs from the field name (e.g. \"$.customer.id\")."
                    }
                  },
                  "required": ["type"]
                }
              }
            }
          },
          "required": ["format"]
        }
      }
    },
    "consumes": {
      "type": "object",
      "patternProperties": {
        "^[a-zA-Z0-9_-]+$": {
          "type": "object",
          "properties": {
            "description": {
              "$ref": "#/$defs/localizedString"
            },
            "required": {
              "type": "boolean",
              "default": true
            },
            "format": {
              "type": "string",
              "enum": ["file", "json", "text", "url", "custom"]
            },
            "target": {
              "type": "string",
              "description": "Name of an `environment` key that receives the whole consumed value (e.g. a materialised file path for a CLI switch)."
            },
            "fields": {
              "type": "object",
              "description": "Per-item bindings from this consumed input to individual `environment` keys.",
              "patternProperties": {
                "^[a-zA-Z0-9_-]+$": {
                  "type": "object",
                  "properties": {
                    "target": {
                      "type": "string",
                      "description": "Name of the `environment` key this item's value is written to."
                    },
                    "format": {
                      "type": "string",
                      "enum": ["string", "integer", "float", "boolean"]
                    }
                  },
                  "required": ["target"]
                }
              }
            }
          },
          "required": ["format"]
        }
      }
    },
    "events": {
      "type": "object",
      "description": "Business events this application emits or consumes, used by external orchestrators (e.g. Node-RED) to draw relationships between processes. Layered on top of produces/consumes/artifacts.",
      "additionalProperties": false,
      "properties": {
        "emits": {
          "type": "object",
          "description": "Named business events this application emits.",
          "patternProperties": {
            "^[a-zA-Z0-9_.-]+$": {
              "type": "object",
              "properties": {
                "description": {
                  "$ref": "#/$defs/localizedString"
                },
                "evidence": {
                  "type": "string",
                  "description": "Evidence/entity type associated with the event."
                },
                "operation": {
                  "type": "string",
                  "enum": ["create", "update", "delete", "any"],
                  "description": "Operation that raises the event."
                }
              },
              "additionalProperties": true
            }
          }
        },
        "consumes": {
          "type": "object",
          "description": "Named business events this application consumes as triggers.",
          "patternProperties": {
            "^[a-zA-Z0-9_.-]+$": {
              "type": "object",
              "properties": {
                "description": {
                  "$ref": "#/$defs/localizedString"
                },
                "format": {
                  "type": "string",
                  "enum": ["file", "json", "text", "url", "custom"]
                }
              },
              "additionalProperties": true
            }
          }
        }
      }
    },
    "exitCodes": {
      "type": "object",
      "description": "Possible exit codes returned by the application with their meanings.",
      "patternProperties": {
        "^[0-9]+$": {
          "type": "object",
          "properties": {
            "description": {
              "$ref": "#/$defs/localizedString",
              "description": "Localized description of what this exit code means."
            },
            "severity": {
              "type": "string",
              "enum": ["success", "info", "warning", "error", "critical"],
              "description": "Severity level of this exit code.",
              "default": "error"
            },
            "retry": {
              "type": "boolean",
              "description": "Whether the job should be retried when this exit code is returned.",
              "default": false
            }
          },
          "required": ["description"],
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    },
    "kubernetes": {
      "type": "object",
      "description": "Kubernetes-specific deployment metadata for this application.",
      "additionalProperties": false,
      "required": ["helm"],
      "properties": {
        "helm": {
          "type": "object",
          "description": "Helm deployment configuration used by Kubernetes-capable executors.",
          "additionalProperties": false,
          "properties": {
            "enabled": {
              "type": "boolean",
              "default": false,
              "description": "Enable Helm pre-deployment before application execution."
            },
            "releaseName": {
              "type": "string",
              "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
              "description": "Helm release name (DNS-1123 label)."
            },
            "namespace": {
              "type": "string",
              "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
              "description": "Kubernetes namespace for Helm release."
            },
            "chart": {
              "type": "string",
              "description": "Helm chart source: local path, OCI chart (oci://...), or repo/chart.",
              "oneOf": [
                {
                  "pattern": "^oci://.+"
                },
                {
                  "pattern": "^[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+$"
                },
                {
                  "pattern": "^(\\./|\\.\\./|/).+"
                }
              ]
            },
            "valuesFiles": {
              "type": "array",
              "description": "List of Helm values files applied in order.",
              "minItems": 1,
              "items": {
                "type": "string"
              }
            },
            "set": {
              "type": "object",
              "description": "Key-value map passed as Helm --set arguments.",
              "additionalProperties": {
                "type": ["string", "number", "boolean"]
              }
            },
            "upgradeInstall": {
              "type": "boolean",
              "default": true,
              "description": "Use helm upgrade --install semantics."
            },
            "timeoutSeconds": {
              "type": "integer",
              "minimum": 1,
              "maximum": 3600,
              "description": "Helm operation timeout in seconds."
            },
            "atomic": {
              "type": "boolean",
              "default": false,
              "description": "Enable Helm atomic mode."
            },
            "wait": {
              "type": "boolean",
              "default": true,
              "description": "Wait for resources to become ready."
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "enabled": {
                    "const": true
                  }
                },
                "required": ["enabled"]
              },
              "then": {
                "required": ["chart", "releaseName"]
              }
            }
          ]
        },
        "artifacts": {
          "type": "object",
          "description": "Kubernetes artifact extraction behavior.",
          "additionalProperties": false,
          "properties": {
            "enabled": {
              "type": "boolean",
              "default": false,
              "description": "Enable artifact copy after pod completion."
            },
            "outputPath": {
              "type": "string",
              "description": "Path inside container/pod from where artifacts are copied."
            },
            "keepPodOnFailure": {
              "type": "boolean",
              "default": false,
              "description": "Do not delete failed pod to allow troubleshooting."
            }
          }
        }
      }
    },
    "requirements": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "List of requirements Keywords needed by the application."
    }
  },
  "required": ["$schema", "name", "description", "executable", "environment"],
  "additionalProperties": true
}
