{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.forwardfi.app/schemas/export-v1.json",
  "title": "ForwardFi export",
  "type": "object",
  "required": ["format", "version", "exportedAt", "app", "accounts"],
  "additionalProperties": false,
  "properties": {
    "$schema": { "type": "string" },
    "format": { "const": "forwardfi.export" },
    "version": { "const": 1 },
    "exportedAt": { "type": "string" },
    "app": { "const": "forwardfi" },
    "preferences": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "currency": { "type": "string", "minLength": 3, "maxLength": 3 }
      }
    },
    "accounts": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "name",
          "openingBalance",
          "currency",
          "forecastRangeMonths",
          "rules",
          "scenarios"
        ],
        "additionalProperties": false,
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string", "minLength": 1, "maxLength": 120 },
          "openingBalance": { "type": "number" },
          "currency": { "type": "string", "minLength": 3, "maxLength": 3 },
          "forecastRangeMonths": { "enum": [3, 6, 12, 24, 36, 60] },
          "sortOrder": { "type": "integer" },
          "rules": {
            "type": "array",
            "items": { "$ref": "#/$defs/rule" }
          },
          "scenarios": {
            "type": "array",
            "items": { "$ref": "#/$defs/scenario" }
          }
        }
      }
    }
  },
  "$defs": {
    "rule": {
      "type": "object",
      "required": ["name", "type", "entries"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "type": { "enum": ["income", "expense"] },
        "groupLabel": { "type": ["string", "null"] },
        "color": { "type": ["string", "null"] },
        "transferAccountId": { "type": ["string", "null"] },
        "sortOrder": { "type": "integer" },
        "entries": {
          "type": "array",
          "items": { "$ref": "#/$defs/entry" }
        }
      }
    },
    "entry": {
      "type": "object",
      "required": ["amount", "recurrence", "startDate"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "amount": { "type": "number" },
        "recurrence": {
          "enum": [
            "weekly",
            "fortnightly",
            "monthly",
            "every_x_months",
            "dayofmonth",
            "quarterly",
            "yearly",
            "single"
          ]
        },
        "monthInterval": { "type": ["integer", "null"] },
        "dayOfMonth": { "type": ["integer", "null"] },
        "startDate": { "type": "string" },
        "endDate": { "type": ["string", "null"] },
        "description": { "type": ["string", "null"] }
      }
    },
    "scenario": {
      "type": "object",
      "required": ["name", "enabled", "rules"],
      "additionalProperties": false,
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "enabled": { "type": "boolean" },
        "sortOrder": { "type": "integer" },
        "rules": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["transactionRuleId", "excluded"],
            "additionalProperties": false,
            "properties": {
              "transactionRuleId": { "type": "string" },
              "amountOverride": { "type": ["number", "null"] },
              "excluded": { "type": "boolean" }
            }
          }
        }
      }
    }
  }
}
