{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Portable brain package 0.1.0-draft",
  "description": "Borrador propio. Valida estructura; no ejecuta ni autoriza acciones. Referencias, ciclos, RRULE y permisos requieren controles adicionales.",
  "type": "object",
  "properties": {
    "format_version": {
      "const": "0.1.0-draft"
    },
    "package_id": {
      "$ref": "#/$defs/id"
    },
    "exported_at": {
      "type": "string",
      "format": "date-time"
    },
    "source": {
      "type": "object",
      "properties": {
        "label": {
          "type": "string",
          "minLength": 1
        },
        "coverage": {
          "enum": [
            "complete",
            "partial",
            "unknown"
          ]
        }
      },
      "required": [
        "label",
        "coverage"
      ],
      "additionalProperties": false
    },
    "instructions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/context_item"
      }
    },
    "memories": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/context_item"
      }
    },
    "blocks": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "$ref": "#/$defs/id"
          },
          "label": {
            "type": "string",
            "minLength": 1
          }
        },
        "required": [
          "id",
          "label"
        ],
        "additionalProperties": false
      }
    },
    "works": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/work"
      }
    },
    "activation_request": {
      "$ref": "#/$defs/selection"
    },
    "omissions": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "extensions": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "required": [
    "format_version",
    "package_id",
    "exported_at",
    "source",
    "instructions",
    "memories",
    "blocks",
    "works",
    "activation_request",
    "omissions"
  ],
  "additionalProperties": false,
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9._-]{1,80}$"
    },
    "context_item": {
      "type": "object",
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "text": {
          "type": "string",
          "minLength": 1
        },
        "source_ref": {
          "type": "string",
          "minLength": 1
        },
        "recorded_at": {
          "type": "string",
          "format": "date-time"
        },
        "privacy": {
          "enum": [
            "local_only",
            "private",
            "shareable"
          ]
        }
      },
      "required": [
        "id",
        "text",
        "source_ref",
        "recorded_at",
        "privacy"
      ],
      "additionalProperties": false
    },
    "work": {
      "type": "object",
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "title": {
          "type": "string",
          "minLength": 1
        },
        "block_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "depends_on": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "trigger": {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "kind": {
                  "const": "manual"
                }
              },
              "required": [
                "kind"
              ],
              "additionalProperties": false
            },
            {
              "type": "object",
              "properties": {
                "kind": {
                  "const": "time"
                },
                "dtstart": {
                  "type": "string",
                  "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}$"
                },
                "timezone": {
                  "type": "string",
                  "minLength": 1
                },
                "rrule": {
                  "type": "string",
                  "minLength": 1
                }
              },
              "required": [
                "kind",
                "dtstart",
                "timezone"
              ],
              "additionalProperties": false
            },
            {
              "type": "object",
              "properties": {
                "kind": {
                  "const": "event"
                },
                "source_ref": {
                  "type": "string",
                  "minLength": 1
                },
                "event_type": {
                  "type": "string",
                  "minLength": 1
                },
                "condition": {
                  "type": "string",
                  "minLength": 1
                }
              },
              "required": [
                "kind",
                "source_ref",
                "event_type",
                "condition"
              ],
              "additionalProperties": false
            }
          ]
        },
        "action": {
          "type": "object",
          "properties": {
            "kind": {
              "enum": [
                "prepare_draft",
                "reminder",
                "checklist",
                "review"
              ]
            },
            "description": {
              "type": "string",
              "minLength": 1
            },
            "resource_refs": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              }
            },
            "delivery": {
              "type": "object",
              "properties": {
                "channel_ref": {
                  "type": [
                    "string",
                    "null"
                  ]
                }
              },
              "required": [
                "channel_ref"
              ],
              "additionalProperties": false
            }
          },
          "required": [
            "kind",
            "description",
            "resource_refs",
            "delivery"
          ],
          "additionalProperties": false
        },
        "requirements": {
          "type": "object",
          "properties": {
            "required_access": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "resource_ref": {
                    "type": "string",
                    "minLength": 1
                  },
                  "scope": {
                    "enum": [
                      "read",
                      "notify_self",
                      "external_effect"
                    ]
                  }
                },
                "required": [
                  "resource_ref",
                  "scope"
                ],
                "additionalProperties": false
              }
            },
            "effect_policy": {
              "enum": [
                "prepare_only",
                "confirm_before_effect"
              ]
            }
          },
          "required": [
            "required_access",
            "effect_policy"
          ],
          "additionalProperties": false
        },
        "constraints": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "privacy": {
          "enum": [
            "local_only",
            "private",
            "shareable"
          ]
        },
        "missed_run_policy": {
          "enum": [
            "skip",
            "ask",
            "undecided"
          ]
        },
        "source_snapshot": {
          "type": "object",
          "properties": {
            "status": {
              "enum": [
                "unknown",
                "pending",
                "completed",
                "cancelled",
                "paused",
                "in_progress"
              ]
            },
            "observed_at": {
              "type": "string",
              "format": "date-time"
            },
            "source_ref": {
              "type": "string",
              "minLength": 1
            }
          },
          "required": [
            "status",
            "observed_at",
            "source_ref"
          ],
          "additionalProperties": false
        }
      },
      "required": [
        "id",
        "title",
        "block_ids",
        "depends_on",
        "trigger",
        "action",
        "requirements",
        "constraints",
        "privacy",
        "missed_run_policy",
        "source_snapshot"
      ],
      "additionalProperties": false
    },
    "selection": {
      "type": "object",
      "properties": {
        "mode": {
          "enum": [
            "none",
            "all",
            "individual",
            "blocks"
          ]
        },
        "work_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "block_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "exclude_work_ids": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        }
      },
      "required": [
        "mode",
        "work_ids",
        "block_ids",
        "exclude_work_ids"
      ],
      "additionalProperties": false,
      "allOf": [
        {
          "if": {
            "properties": {
              "mode": {
                "const": "none"
              }
            },
            "required": [
              "mode"
            ]
          },
          "then": {
            "properties": {
              "work_ids": {
                "maxItems": 0
              },
              "block_ids": {
                "maxItems": 0
              },
              "exclude_work_ids": {
                "maxItems": 0
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "mode": {
                "const": "all"
              }
            },
            "required": [
              "mode"
            ]
          },
          "then": {
            "properties": {
              "work_ids": {
                "maxItems": 0
              },
              "block_ids": {
                "maxItems": 0
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "mode": {
                "const": "individual"
              }
            },
            "required": [
              "mode"
            ]
          },
          "then": {
            "properties": {
              "block_ids": {
                "maxItems": 0
              },
              "work_ids": {
                "minItems": 1
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "mode": {
                "const": "blocks"
              }
            },
            "required": [
              "mode"
            ]
          },
          "then": {
            "properties": {
              "work_ids": {
                "maxItems": 0
              },
              "block_ids": {
                "minItems": 1
              }
            }
          }
        }
      ]
    }
  }
}
