{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://schema.dpksystem.cloud/Web/tenant-identity.schema.json",
  "title": "USP Tenant Identity",
  "description": "Read-only identity klienta USP — provisioned przy deploymencie, edytowane tylko przez root admin USP",
  "type": "object",
  "additionalProperties": false,
  "required": ["tenantId", "schemaVersion", "clientId"],

  "properties": {
    "$schema": {
      "type": "string",
      "format": "uri",
      "description": "Link do JSON Schema — dla VS Code autocomplete/walidacji"
    },

    "tenantId": {
      "type": "string",
      "pattern": "^[A-Z][A-Z0-9_]*$",
      "minLength": 2,
      "maxLength": 32,
      "description": "Unikalny klucz tenanta w UPPERCASE_SNAKE_CASE (np. KML, MPK_LODZ, GAIT)",
      "examples": ["KML", "MPK_LODZ", "DEFAULT"]
    },

    "schemaVersion": {
      "type": "integer",
      "const": 1,
      "description": "Wersja tego JSON Schema. Obecnie zawsze 1. Bump przy breaking change."
    },

    "clientId": {
      "type": "string",
      "pattern": "^usp_[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$",
      "description": "OAuth Client ID w formacie 'usp_<uuid>'. Provisioned w OpenID server, MUSI się zgadzać.",
      "examples": ["usp_88aea24c-3e63-4c74-9577-ed19d3c0831d"]
    },

    "clientVersion": {
      "type": "integer",
      "minimum": 0,
      "default": 0,
      "description": "Version identyfikatora klienta. Używane przy token refresh — bump gdy re-provisioning w OAuth."
    },

    "virtualHost": {
      "type": ["string", "null"],
      "description": "Custom hostname jeśli klient ma dedykowany (np. 'portal.kolejemalopolskie.com.pl'). null dla shared.",
      "examples": [null, "portal.kolejemalopolskie.com.pl"]
    },

    "customAuthMetadataUrl": {
      "type": "boolean",
      "default": false,
      "description": "Czy używać proxy OpenID metadata endpoint przez WebApi (true), czy bezpośrednio na Auth (false, default)"
    },

    "displayName": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100,
      "description": "Human-readable name dla UI (np. 'Koleje Małopolskie sp. z o.o.')",
      "examples": ["Koleje Małopolskie", "MPK Łódź"]
    },

    "environment": {
      "type": "string",
      "enum": ["development", "staging", "production"],
      "description": "Środowisko dla którego identity jest ważne — pomaga wykryć pomyłki (identity produkcyjne w dev)"
    },

    "createdAt": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601 timestamp gdy tenant został provisioned"
    },

    "provisionedBy": {
      "type": "string",
      "format": "email",
      "description": "Email osoby która provisioned tenant (audit trail)"
    },

    "contactEmail": {
      "type": "string",
      "format": "email",
      "description": "Contact person / support email po stronie klienta"
    },

    "tags": {
      "type": "array",
      "items": { "type": "string", "pattern": "^[a-z0-9-]+$" },
      "uniqueItems": true,
      "description": "Meta tags dla filtrowania/kategoryzacji (np. ['railway', 'poland', 'premium'])"
    }
  }
}
