{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/ritwikshanker/mensis/blob/main/docs/mensis-export.schema.json",
  "title": "Mensis export",
  "description": "Mensis JSON export, formatVersion 2. Temperatures in °C, weights in kg, sleep in minutes. Readers must ignore properties they do not know. See export-format.md.",
  "type": "object",
  "required": ["format", "formatVersion", "schemaVersion", "exportedAt", "app"],
  "properties": {
    "format": { "const": "mensis-export" },
    "formatVersion": { "const": 2 },
    "schemaVersion": { "type": "integer", "minimum": 1 },
    "exportedAt": { "$ref": "#/$defs/instant" },
    "app": {
      "type": "object",
      "required": ["versionName", "versionCode"],
      "properties": {
        "versionName": { "type": "string" },
        "versionCode": { "type": "integer" }
      }
    },
    "settings": { "$ref": "#/$defs/settings" },
    "uiPrefs": { "$ref": "#/$defs/uiPrefs" },
    "contraceptionPeriods": { "type": "array", "items": { "$ref": "#/$defs/contraceptionPeriod" } },
    "customItems": { "type": "array", "items": { "$ref": "#/$defs/customItem" } },
    "dayEntries": { "type": "array", "items": { "$ref": "#/$defs/dayEntry" } }
  },
  "$defs": {
    "date": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$" },
    "time": { "type": "string", "pattern": "^\\d{2}:\\d{2}(:\\d{2}(\\.\\d+)?)?$" },
    "instant": { "type": "string", "format": "date-time" },
    "reminder": {
      "type": "object",
      "properties": {
        "enabled": { "type": "boolean" },
        "time": { "$ref": "#/$defs/time" },
        "option": {
          "enum": [
            "same_day", "one_day_before", "two_days_before", "daily", "active_days_only", "weekly",
            "three_weeks_in_one_out", "four_weekly", "twelve_weeks", "thirteen_weeks",
            "one_month_before", "three_months_before", "follicular_only"
          ]
        }
      }
    },
    "settings": {
      "type": "object",
      "required": [
        "trackingLevel", "goal", "mode", "typicalCycleLength", "typicalPeriodLength",
        "temperatureUnit", "weightUnit", "createdAt", "updatedAt"
      ],
      "properties": {
        "trackingLevel": { "enum": ["period", "symptoms", "full"] },
        "goal": { "enum": ["track_cycle", "trying_to_conceive", "avoiding_pregnancy", "understand_symptoms"] },
        "mode": { "enum": ["cycle", "pregnancy"] },
        "pregnancyLmp": { "$ref": "#/$defs/date" },
        "typicalCycleLength": { "type": "integer", "minimum": 1 },
        "typicalPeriodLength": { "type": "integer", "minimum": 1 },
        "temperatureUnit": { "enum": ["celsius", "fahrenheit"] },
        "weightUnit": { "enum": ["kilogram", "pound"] },
        "reminders": {
          "type": "object",
          "description": "One entry per reminder the user has touched; absent entries and fields use the defaults",
          "properties": {
            "period": { "$ref": "#/$defs/reminder" },
            "fertile": { "$ref": "#/$defs/reminder" },
            "pill": { "$ref": "#/$defs/reminder" },
            "patch": { "$ref": "#/$defs/reminder" },
            "ring": { "$ref": "#/$defs/reminder" },
            "injection": { "$ref": "#/$defs/reminder" },
            "replacement": { "$ref": "#/$defs/reminder" },
            "temperature": { "$ref": "#/$defs/reminder" },
            "discreetTitle": { "type": "string" },
            "discreetBody": { "type": "string" }
          }
        },
        "discreetWording": { "type": "boolean" },
        "createdAt": { "$ref": "#/$defs/instant" },
        "updatedAt": { "$ref": "#/$defs/instant" }
      }
    },
    "uiPrefs": {
      "type": "object",
      "required": ["theme", "appearance", "lockEnabled", "hideInRecents", "relockTimeoutMinutes"],
      "properties": {
        "theme": { "enum": ["rose", "sage", "dynamic"] },
        "appearance": { "enum": ["light", "dark", "system"] },
        "lockEnabled": { "type": "boolean" },
        "hideInRecents": { "type": "boolean" },
        "relockTimeoutMinutes": { "type": "integer", "description": "0 = immediately, -1 = never" }
      }
    },
    "contraceptionPeriod": {
      "type": "object",
      "required": ["id", "method", "startDate", "createdAt", "updatedAt"],
      "properties": {
        "id": { "type": "string" },
        "method": {
          "enum": [
            "none", "combined_pill", "progestin_only_pill", "patch", "ring", "injection", "implant",
            "hormonal_iud", "copper_iud"
          ]
        },
        "regimen": { "enum": ["cyclic", "continuous"] },
        "startDate": { "$ref": "#/$defs/date" },
        "endDate": { "$ref": "#/$defs/date" },
        "dailyTime": { "$ref": "#/$defs/time" },
        "replaceByDate": { "$ref": "#/$defs/date" },
        "checkupDate": { "$ref": "#/$defs/date" },
        "createdAt": { "$ref": "#/$defs/instant" },
        "updatedAt": { "$ref": "#/$defs/instant" },
        "deletedAt": { "$ref": "#/$defs/instant" }
      }
    },
    "customName": {
      "description": "One of the user's own symptoms or moods: 1 to 30 characters, no control characters and none of ; , | /. Names are compared ignoring case.",
      "type": "string",
      "minLength": 1,
      "maxLength": 30,
      "pattern": "^[^;,|/\\u0000-\\u001F\\u007F-\\u009F]+$"
    },
    "customItem": {
      "type": "object",
      "required": ["id", "kind", "name", "createdAt", "updatedAt"],
      "properties": {
        "id": { "type": "string" },
        "kind": { "enum": ["symptom", "mood"] },
        "name": { "$ref": "#/$defs/customName" },
        "createdAt": { "$ref": "#/$defs/instant" },
        "updatedAt": { "$ref": "#/$defs/instant" }
      }
    },
    "dayEntry": {
      "type": "object",
      "required": ["id", "date", "createdAt", "updatedAt"],
      "properties": {
        "id": { "type": "string" },
        "date": { "$ref": "#/$defs/date" },
        "flow": { "enum": ["none", "spotting", "light", "medium", "heavy"] },
        "temperatureC": { "type": "number" },
        "temperatureTime": { "$ref": "#/$defs/time" },
        "symptoms": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "enum": ["cramps", "headache", "bloating", "tender_breasts", "fatigue", "back_pain", "acne", "nausea"]
          }
        },
        "moods": {
          "type": "array",
          "uniqueItems": true,
          "items": { "enum": ["calm", "happy", "irritable", "anxious", "low", "energetic"] }
        },
        "customSymptoms": { "type": "array", "items": { "$ref": "#/$defs/customName" } },
        "customMoods": { "type": "array", "items": { "$ref": "#/$defs/customName" } },
        "cervicalMucus": { "enum": ["dry", "sticky", "creamy", "watery", "egg_white"] },
        "lhTest": { "enum": ["negative", "positive"] },
        "pregnancyTest": { "enum": ["negative", "positive"] },
        "sex": { "enum": ["none", "protected", "unprotected"] },
        "pillState": { "enum": ["taken", "late", "missed"] },
        "patchChanged": { "type": "boolean" },
        "ringState": { "enum": ["in", "out"] },
        "injectionGiven": { "type": "boolean" },
        "emergencyContraception": { "type": "boolean" },
        "sleepMinutes": { "type": "integer", "minimum": 0 },
        "weightKg": { "type": "number" },
        "notes": { "type": "string" },
        "createdAt": { "$ref": "#/$defs/instant" },
        "updatedAt": { "$ref": "#/$defs/instant" },
        "deletedAt": { "$ref": "#/$defs/instant" }
      }
    }
  }
}
