{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Automation Plugin Definition",
  "type": "object",
  "required": [
    "id",
    "name",
    "category",
    "publisher",
    "version",
    "entrypoint",
    "capabilities"
  ],
  "properties": {
    "id": {
      "type": "string",
      "description": "Unique plugin identifier"
    },
    "name": {
      "type": "string",
      "description": "Human-readable plugin name"
    },
    "category": {
      "type": "string",
      "enum": ["device", "system", "hybrid"],
      "description": "Type of plugin"
    },
    "publisher": {
      "type": "string",
      "description": "Author or organization creating the plugin"
    },
    "version": {
      "type": "string",
      "description": "Semantic version (e.g., 1.0.0)"
    },
    "description": {
      "type": "string",
      "description": "Optional description for this plugin"
    },
    "entrypoint": {
      "type": "string",
      "description": "Main file or module used to start the plugin"
    },
    "capabilities": {
      "type": "object",
      "description": "FHIR resource-based capabilities",
      "patternProperties": {
        "^[A-Z][a-zA-Z]+$": {
          "type": "object",
          "required": ["actions"],
          "properties": {
            "actions": {
              "type": "array",
              "items": { "type": "string" }
            },
            "codes": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["localCode", "description"],
                "properties": {
                  "localCode": { "type": "string" },
                  "description": { "type": "string" },
                  "loinc": { "type": "string" },
                  "snomed": { "type": "string" },
                  "unit": { "type": "string" },
                  "valueType": { "type": "string" },
                  "flags": {
                    "type": "array",
                    "items": { "type": "string" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "device": {
      "type": "object",
      "description": "Present only if category is 'device' or 'hybrid'",
      "required": ["vendor", "model", "version"],
      "properties": {
        "vendor": { "type": "string" },
        "model": { "type": "string" },
        "version": { "type": "string" },
        "serialNumber": { "type": "string" },
        "communication": {
          "type": "object",
          "properties": {
            "mode": { "type": "string" },
            "protocol": { "type": "string" },
            "portSettings": {
              "type": "object",
              "properties": {
                "baudRate": { "type": "integer" },
                "dataBits": { "type": "integer" },
                "parity": { "type": "string" },
                "stopBits": { "type": "integer" }
              }
            }
          }
        }
      }
    },
    "system": {
      "type": "object",
      "description": "Present only if category is 'system' or 'hybrid'",
      "required": ["vendor", "product", "version"],
      "properties": {
        "vendor": { "type": "string" },
        "product": { "type": "string" },
        "version": { "type": "string" },
        "integrationModes": {
          "type": "array",
          "items": { "type": "string" }
        },
        "fhir": {
          "type": "object",
          "properties": {
            "baseUrl": { "type": "string", "format": "uri" },
            "fhirVersion": { "type": "string" },
            "auth": {
              "type": "object",
              "properties": {
                "type": {
                  "type": "string",
                  "enum": ["basic", "bearer", "none"]
                },
                "username": { "type": "string" },
                "password": { "type": "string" },
                "token": { "type": "string" }
              }
            }
          }
        }
      }
    },
    "endpoints": {
      "type": "object",
      "description": "Connection settings or file paths",
      "additionalProperties": { "type": "string" }
    }
  },
  "if": {
    "properties": { "category": { "const": "device" } }
  },
  "then": {
    "required": ["device"]
  },
  "else": {
    "if": {
      "properties": { "category": { "const": "system" } }
    },
    "then": {
      "required": ["system"]
    }
  }
}
