{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://shaleio.com/schemas/stratabi/module.schema.json",
  "title": "StrataBI Module Schema",
  "description": "Schema for StrataBI modules. Core runtime discovers modules from S3 at /modules/<module_name>/module.json.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "module_id",
    "module_type",
    "label",
    "service_dependencies",
    "lambdas"
  ],
  "properties": {
    "schema_version": {
      "type": "string",
      "default": "1.0"
    },
    "module_id": {
      "type": "string",
      "description": "Stable machine-readable module identifier.",
      "pattern": "^[a-zA-Z0-9_-]+$"
    },
    "module_type": {
      "type": "string",
      "description": "Primary module execution/application type.",
      "enum": [
        "app",
        "multi_page_app",
        "microservices",
        "single_lambda"
      ]
    },
    "label": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "version": {
      "type": "string",
      "default": "1.0.0"
    },
    "recommended_theme": {
      "type": "string"
    },
    "navigation_type": {
      "type": "string",
      "description": "Required only for multi_page_app modules.",
      "enum": [
        "tabs",
        "pagination",
        "next_previous"
      ]
    },
    "service_dependencies": {
      "type": "array",
      "description": "Required declaration of external/runtime services this module depends on.",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "service_type",
          "name"
        ],
        "properties": {
          "service_type": {
            "type": "string",
            "enum": [
              "s3",
              "dynamodb",
              "lambda",
              "eventbridge",
              "sqs",
              "sns",
              "ses",
              "athena",
              "glue",
              "stepfunctions",
              "ecs",
              "secretsmanager",
              "cloudwatch",
              "iam",
              "other"
            ]
          },
          "name": {
            "type": "string"
          },
          "required": {
            "type": "boolean",
            "default": true
          },
          "description": {
            "type": "string"
          }
        }
      }
    },
    "lambdas": {
      "type": "array",
      "description": "Lambda functions required by this module. Microservice modules are forced through declared lambdas.",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "function_id",
          "handler",
          "runtime"
        ],
        "properties": {
          "function_id": {
            "type": "string",
            "pattern": "^[a-zA-Z0-9_-]+$"
          },
          "description": {
            "type": "string"
          },
          "handler": {
            "type": "string"
          },
          "runtime": {
            "type": "string",
            "examples": [
              "python3.12",
              "nodejs20.x"
            ]
          },
          "memory_mb": {
            "type": "integer",
            "minimum": 128,
            "default": 256
          },
          "timeout_seconds": {
            "type": "integer",
            "minimum": 1,
            "maximum": 900,
            "default": 30
          },
          "environment": {
            "type": "object",
            "additionalProperties": {
              "type": "string"
            }
          },
          "trigger": {
            "type": "string",
            "enum": [
              "manual",
              "dashboard_event",
              "schedule",
              "s3_event",
              "sqs",
              "api",
              "other"
            ],
            "default": "manual"
          }
        }
      }
    },
    "overrides": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "default_page": {
          "type": "string"
        },
        "show_labels": {
          "type": "boolean"
        },
        "show_progress": {
          "type": "boolean"
        },
        "show_first_last": {
          "type": "boolean"
        },
        "loop": {
          "type": "boolean"
        }
      }
    },
    "settings": {
      "type": "object",
      "additionalProperties": false,
      "description": "Optional runtime settings applied to the module's pages. Required for async (lambda) tiles to auto-refresh: enable interval polling so the page picks up results.",
      "properties": {
        "interval_enabled": {
          "type": "boolean",
          "default": false,
          "description": "If true, the module's pages poll on an interval (needed for async lambda tiles to display their result)."
        },
        "interval_ms": {
          "type": "integer",
          "minimum": 5000,
          "maximum": 3600000,
          "default": 60000,
          "description": "Poll interval in milliseconds."
        }
      },
      "allOf": [
        {
          "if": {
            "properties": { "interval_enabled": { "const": true } },
            "required": ["interval_enabled"]
          },
          "then": { "required": ["interval_ms"] }
        }
      ]
    },
    "pages": {
      "type": "array",
      "description": "Required for multi_page_app modules. Ordered pages containing normal StrataBI dashboard layouts.",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "page_id",
          "label",
          "layout"
        ],
        "properties": {
          "page_id": {
            "type": "string",
            "pattern": "^[a-zA-Z0-9_-]+$"
          },
          "label": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "layout": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": true
            }
          }
        }
      }
    },
    "metadata": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "author": {
          "type": "string"
        },
        "owner": {
          "type": "string"
        },
        "tags": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "created_at": {
          "type": "string"
        },
        "updated_at": {
          "type": "string"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "module_type": {
            "const": "multi_page_app"
          }
        }
      },
      "then": {
        "required": [
          "navigation_type",
          "pages"
        ],
        "properties": {
          "pages": {
            "minItems": 1
          }
        }
      }
    },
      {
      "if": {
        "properties": {
          "module_type": {
            "const": "app"
          }
        }
      },
      "then": {
        "required": [
          "pages"
        ],
        "properties": {
          "pages": {
            "minItems": 1,
            "maxItems": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "module_type": {
            "const": "single_lambda"
          }
        }
      },
      "then": {
        "properties": {
          "lambdas": {
            "minItems": 1,
            "maxItems": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "module_type": {
            "const": "microservices"
          }
        }
      },
      "then": {
        "properties": {
          "lambdas": {
            "minItems": 1
          }
        }
      }
    }
  ]
}