{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://license.aurea.software/docs/license/license-file-v1.schema.json",
  "title": "Aurea License File v1",
  "type": "object",
  "required": ["payload", "signature"],
  "properties": {
    "payload": {
      "type": "object",
      "required": [
        "schemaVersion",
        "licenseRevision",
        "licenseId",
        "licenseType",
        "edition",
        "account",
        "organization",
        "validity",
        "entitlements"
      ],
      "properties": {
        "schemaVersion": {
          "const": 1
        },
        "licenseRevision": {
          "type": "integer",
          "minimum": 1,
          "maximum": 9007199254740991
        },
        "licenseId": {
          "type": "string",
          "pattern": "^lic_[0-9a-f]{32}$"
        },
        "licenseType": {
          "enum": ["subscription", "perpetual", "community"]
        },
        "edition": {
          "enum": ["core", "standard"]
        },
        "account": {
          "type": "object",
          "required": ["accountId", "name"],
          "properties": {
            "accountId": {
              "type": "string",
              "pattern": "^acct_[0-9a-f]{32}$"
            },
            "name": {
              "type": "string",
              "minLength": 1
            }
          },
          "additionalProperties": false
        },
        "organization": {
          "type": "object",
          "required": ["organizationId", "name", "environmentType"],
          "properties": {
            "organizationId": {
              "type": "string",
              "pattern": "^org_[0-9a-hjkmnp-tv-z]{26}$"
            },
            "name": {
              "type": "string",
              "minLength": 1
            },
            "environmentType": {
              "enum": ["production", "staging", "test", "development"]
            }
          },
          "additionalProperties": false
        },
        "validity": {
          "type": "object",
          "required": ["issuedAt", "validFrom"],
          "properties": {
            "issuedAt": {
              "type": "string",
              "format": "date-time"
            },
            "validFrom": {
              "type": "string",
              "format": "date-time"
            },
            "validUntil": {
              "type": "string",
              "format": "date-time"
            },
            "graceUntil": {
              "type": "string",
              "format": "date-time"
            }
          },
          "additionalProperties": false
        },
        "entitlements": {
          "type": "array",
          "minItems": 0,
          "maxItems": 1,
          "items": {
            "type": "object",
            "required": ["code", "value"],
            "properties": {
              "code": {
                "enum": ["users.active", "business_processes.active", "compute.cores"]
              },
              "value": {
                "type": "integer",
                "minimum": 1,
                "maximum": 2147483647
              }
            },
            "additionalProperties": false
          }
        }
      },
      "allOf": [
        {
          "if": {"properties": {"licenseType": {"const": "community"}}},
          "then": {
            "properties": {
              "edition": {"const": "standard"},
              "entitlements": {
                "minItems": 1,
                "maxItems": 1,
                "items": {
                  "properties": {
                    "code": {"const": "users.active"},
                    "value": {"const": 5}
                  }
                }
              }
            }
          },
          "else": {
            "properties": {
              "edition": {"enum": ["core", "standard"]},
              "entitlements": {"minItems": 1}
            }
          }
        },
        {
          "if": {
            "properties": {
              "licenseType": {"const": "subscription"}
            }
          },
          "then": {
            "properties": {
              "validity": {
                "required": ["issuedAt", "validFrom", "validUntil", "graceUntil"]
              }
            }
          },
          "else": {
            "properties": {
              "validity": {
                "not": {
                  "anyOf": [
                    {"required": ["validUntil"]},
                    {"required": ["graceUntil"]}
                  ]
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "edition": {"const": "core"}
            }
          },
          "then": {
            "properties": {
              "licenseType": {"const": "subscription"},
              "entitlements": {
                "items": {
                  "properties": {
                    "code": {"const": "users.active"}
                  }
                }
              }
            }
          }
        }
      ],
      "additionalProperties": false
    },
    "signature": {
      "type": "object",
      "required": ["algorithm", "canonicalization", "keyId", "value"],
      "properties": {
        "algorithm": {
          "const": "Ed25519"
        },
        "canonicalization": {
          "const": "jcs-rfc8785"
        },
        "keyId": {
          "type": "string",
          "minLength": 1
        },
        "value": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_-]{86}$"
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
