{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aip.times.et/schemas/times.aip.schema.json",
  "title": "TIMES AIP guest manifest",
  "type": "object",
  "required": [
    "schemaVersion",
    "slug",
    "kind",
    "version",
    "name",
    "mode",
    "ui",
    "scopes",
    "i18n",
    "changelog"
  ],
  "additionalProperties": false,
  "properties": {
    "schemaVersion": {
      "type": "string",
      "const": "1"
    },
    "slug": {
      "type": "string",
      "minLength": 1,
      "pattern": "^[a-z0-9]+(?:-[a-z0-9]+)*$"
    },
    "kind": {
      "type": "string",
      "enum": ["app", "integration", "pack"]
    },
    "version": {
      "type": "string",
      "minLength": 1
    },
    "name": {
      "type": "string",
      "minLength": 1
    },
    "mode": {
      "type": "string",
      "enum": ["platform", "runtime"]
    },
    "ui": {
      "type": "object",
      "required": ["uses_times_tokens", "runtime", "surfaces", "entry"],
      "additionalProperties": false,
      "properties": {
        "framework": {
          "type": "string",
          "enum": ["static", "vite", "next"]
        },
        "runtime": {
          "type": "string",
          "enum": ["iframe"]
        },
        "surfaces": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "enum": ["page", "overlay", "configure"]
          }
        },
        "entry": {
          "type": "string",
          "minLength": 1
        },
        "icon": {
          "type": "string"
        },
        "uses_times_tokens": {
          "type": "boolean",
          "const": true
        }
      }
    },
    "scopes": {
      "type": "object",
      "required": ["required"],
      "additionalProperties": false,
      "properties": {
        "required": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        },
        "optional": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "i18n": {
      "type": "object",
      "required": ["locales", "catalogs"],
      "additionalProperties": false,
      "properties": {
        "locales": {
          "type": "array",
          "minItems": 4,
          "items": {
            "type": "string",
            "enum": ["en", "am", "om", "ti"]
          }
        },
        "catalogs": {
          "type": "object",
          "required": ["en", "am", "om", "ti"],
          "additionalProperties": false,
          "properties": {
            "en": { "type": "string" },
            "am": { "type": "string" },
            "om": { "type": "string" },
            "ti": { "type": "string" }
          }
        }
      }
    },
    "connect": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "mode": {
          "type": "string",
          "enum": ["oauth", "credentials", "configure"]
        },
        "oauth": {
          "type": "object",
          "required": ["provider"],
          "additionalProperties": false,
          "properties": {
            "provider": {
              "type": "string",
              "enum": ["slack", "google", "whatsapp", "telegram"]
            },
            "scopes": {
              "type": "array",
              "items": { "type": "string" }
            },
            "pkce": { "type": "boolean" },
            "authorizeUrl": { "type": "string" },
            "tokenUrl": { "type": "string" },
            "configKeys": {
              "type": "object",
              "additionalProperties": { "type": "string" }
            }
          }
        },
        "credentials": {
          "type": "object",
          "additionalProperties": true
        },
        "configure": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "platform": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "events": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        },
        "settingsSchema": {
          "type": "object",
          "additionalProperties": true
        }
      }
    },
    "runtime": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "workerUrl": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://"
        },
        "webhookPath": { "type": "string" },
        "signing": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "algorithm": {
              "type": "string",
              "enum": ["hmac-sha256"]
            },
            "header": { "type": "string" },
            "skewSeconds": {
              "type": "integer",
              "minimum": 30,
              "maximum": 600
            }
          }
        },
        "invoke": { "type": "boolean" },
        "sync": { "type": "boolean" },
        "events": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "catalog": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "autoPublish": { "type": "boolean" }
      }
    },
    "changelog": {
      "type": "array",
      "minItems": 1,
      "description": "User-facing release notes. Current version must have an entry. Keys resolve from i18n catalogs.",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["version", "date", "summaryKey", "highlightKeys"],
        "properties": {
          "version": {
            "type": "string",
            "minLength": 1
          },
          "date": {
            "type": "string",
            "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
          },
          "summaryKey": {
            "type": "string",
            "minLength": 1,
            "description": "i18n key for a short merchant-facing summary of the release"
          },
          "highlightKeys": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "string",
              "minLength": 1
            },
            "description": "i18n keys for concrete benefits (what the merchant can do), not commit messages"
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "mode": { "const": "runtime" } }
      },
      "then": {
        "required": ["runtime"],
        "properties": {
          "runtime": {
            "required": ["workerUrl"]
          }
        }
      }
    }
  ]
}
