{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://sailtactician.com/formats/stx.schema.json",
  "title": "Sail Tactician Exchange (STX), format version 1",
  "description": "A Sail Tactician race export. Units: knots, metres, degrees true (0-360, from north). Absent fields mean unknown. Readers must ignore unknown keys. See https://sailtactician.com/formats/stx",
  "oneOf": [
    { "$ref": "#/$defs/bundle" },
    { "type": "array", "items": { "$ref": "#/$defs/sessionEntry" }, "description": "Legacy: a bare array of sessions." },
    { "$ref": "#/$defs/sessionEntry", "description": "Legacy: a single bare session." }
  ],
  "$defs": {
    "timestamp": {
      "type": "string",
      "description": "ISO 8601, e.g. 2026-08-23T17:52:01.000Z. The app writes milliseconds; versions up to 3.4.0 read only that form.",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})$"
    },
    "degrees": { "type": "number", "minimum": 0, "maximum": 360 },
    "latitude": { "type": "number", "minimum": -90, "maximum": 90 },
    "longitude": { "type": "number", "minimum": -180, "maximum": 180 },
    "uuid": {
      "type": "string",
      "pattern": "^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$"
    },

    "bundle": {
      "type": "object",
      "required": ["formatVersion", "exportedAt", "sessions"],
      "properties": {
        "formatVersion": { "const": 1 },
        "exportedAt": { "$ref": "#/$defs/timestamp" },
        "appVersion": { "type": "string" },
        "sessions": { "type": "array", "items": { "$ref": "#/$defs/sessionEntry" } }
      }
    },

    "sessionEntry": {
      "type": "object",
      "required": ["session", "trackPoints", "motionEvents"],
      "properties": {
        "session": { "$ref": "#/$defs/session" },
        "trackPoints": { "type": "array", "items": { "$ref": "#/$defs/trackPoint" }, "description": "Ascending time order." },
        "motionEvents": { "type": "array", "items": { "$ref": "#/$defs/motionEvent" } }
      }
    },

    "session": {
      "type": "object",
      "required": ["id", "sessionType", "startedAt", "endedAt", "tackCount", "gybeCount", "capsizeCount", "visibility"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid", "description": "Stable. Re-importing the same id is the same race." },
        "title": { "type": "string" },
        "sessionType": { "type": "string", "description": "race | practice (Training) | cruise. Treat unknown values as race.", "examples": ["race", "practice", "cruise"] },
        "startedAt": { "$ref": "#/$defs/timestamp", "description": "Recording start: the start of the countdown for a race, not the gun." },
        "endedAt": { "$ref": "#/$defs/timestamp" },
        "durationSeconds": { "type": "integer" },
        "notes": { "type": "string" },
        "finishPosition": { "type": "integer" },

        "raceStartAt": { "$ref": "#/$defs/timestamp", "description": "The gun. Absent for cruises, imports and abandoned countdowns." },
        "raceEndAt": { "$ref": "#/$defs/timestamp" },
        "countdownDurationSeconds": { "type": "integer" },

        "distanceMeters": { "type": "number", "description": "Cached; recomputed on import." },
        "avgSpeedKnots": { "type": "number", "description": "Cached; recomputed on import." },
        "maxSpeedKnots": { "type": "number", "description": "Cached; recomputed on import." },
        "tackCount": { "type": "integer", "description": "Cached; depends on the wind." },
        "gybeCount": { "type": "integer", "description": "Cached; depends on the wind." },
        "capsizeCount": { "type": "integer" },

        "windDirection": { "$ref": "#/$defs/degrees", "description": "Direction the wind blows FROM, degrees true. Absent = not set; 0 = due north." },
        "windSpeed": { "type": "number", "description": "Knots." },
        "gustSpeed": { "type": "number", "description": "Knots." },
        "temperature": { "type": "number", "description": "Degrees Celsius." },
        "weatherConditions": { "type": "string" },
        "estimatedWindDirectionAtStart": { "type": "number", "description": "Internal. Frozen at record time and often reversed; ignore." },

        "startLinePinLat": { "$ref": "#/$defs/latitude" },
        "startLinePinLng": { "$ref": "#/$defs/longitude" },
        "startLineBoatLat": { "$ref": "#/$defs/latitude" },
        "startLineBoatLng": { "$ref": "#/$defs/longitude" },
        "startLineBias": { "type": "number", "description": "Degrees off square to the wind." },

        "courseJSON": { "type": "string", "contentMediaType": "application/json", "contentSchema": { "$ref": "#/$defs/course" }, "description": "A JSON string; parse it again." },

        "workoutName": { "type": "string" },
        "workoutResultJSON": { "type": "string", "contentMediaType": "application/json", "description": "Internal shape; may change." },

        "boatClassId": { "type": "string", "description": "Built-in class id (e.g. mc-scow, ilca-7) or custom-<uuid>." },
        "venueId": { "type": "string", "description": "Unused." },
        "userLocationId": { "type": "string", "description": "Unused." },
        "visibility": { "type": "string", "examples": ["private"] },

        "recordingRateHz": { "type": "integer", "description": "Measured rate: 1, 2, 5 or 10." },
        "sourceRecordingHz": { "type": "number", "description": "An import's original rate when it was filled to 1 Hz." },
        "recordingSource": { "type": "string", "description": "watch | phone | sample | import-<format>. Absent on older watch recordings.", "examples": ["watch", "phone", "import-gpx", "import-fit"] },
        "deviceModel": { "type": "string" },
        "osVersion": { "type": "string" },
        "appVersion": { "type": "string" },
        "batteryAtStart": { "type": "integer", "minimum": 0, "maximum": 100 },
        "batteryAtEnd": { "type": "integer", "minimum": 0, "maximum": 100 },

        "trimmedStartAt": { "$ref": "#/$defs/timestamp", "description": "Inclusive start of the window the sailor keeps." },
        "trimmedEndAt": { "$ref": "#/$defs/timestamp", "description": "Inclusive end of the window the sailor keeps." },
        "editedAt": { "$ref": "#/$defs/timestamp" },

        "sailorName": { "type": "string", "description": "Comparison shares only, if the sailor opted in." },
        "boatName": { "type": "string", "description": "Comparison shares only." },
        "ownerId": { "type": "string", "description": "Comparison shares only. Opaque per-sailor id." }
      }
    },

    "trackPoint": {
      "type": "object",
      "required": ["timestamp", "latitude", "longitude"],
      "properties": {
        "timestamp": { "$ref": "#/$defs/timestamp" },
        "latitude": { "$ref": "#/$defs/latitude" },
        "longitude": { "$ref": "#/$defs/longitude" },
        "speedKnots": { "type": "number", "minimum": 0 },
        "cog": { "$ref": "#/$defs/degrees", "description": "Course over ground, degrees true." },
        "heading": { "$ref": "#/$defs/degrees", "description": "Bow direction. Mounted phone only." },
        "gpsAccuracy": { "type": "number", "minimum": 0, "description": "Horizontal accuracy, metres." },
        "heartRate": { "type": "number", "description": "BPM. Removed from comparison shares." },
        "heelAngle": { "type": "number", "description": "Degrees, positive = starboard side down. Mounted phone only." },
        "trimAngle": { "type": "number", "description": "Degrees, positive = bow up. Mounted phone only." }
      }
    },

    "motionEvent": {
      "type": "object",
      "required": ["eventType", "timestamp", "confidence", "detectionMethod"],
      "properties": {
        "eventType": { "type": "string", "examples": ["capsize", "knockdown"] },
        "timestamp": { "$ref": "#/$defs/timestamp" },
        "durationSeconds": { "type": "number" },
        "confidence": { "type": "string", "examples": ["high", "medium", "low"] },
        "peakAcceleration": { "type": "number" },
        "detectionMethod": { "type": "string", "examples": ["motion"] }
      }
    },

    "course": {
      "type": "object",
      "description": "Decoded courseJSON. Dates here are seconds since 2001-01-01T00:00:00Z (add 978307200 for Unix time).",
      "properties": {
        "schemaVersion": { "type": "integer" },
        "confirmedAt": { "type": "number" },
        "marks": { "type": "array", "items": { "$ref": "#/$defs/courseMark" } }
      }
    },

    "courseMark": {
      "type": "object",
      "required": ["latitude", "longitude"],
      "properties": {
        "id": { "$ref": "#/$defs/uuid" },
        "latitude": { "$ref": "#/$defs/latitude" },
        "longitude": { "$ref": "#/$defs/longitude" },
        "role": { "type": "string", "description": "Unknown values read as unknown.", "examples": ["windward", "offset", "wing", "reach", "leeward", "leewardGateLeft", "leewardGateRight", "windwardGateLeft", "windwardGateRight", "finish", "pin", "unknown"] },
        "source": { "type": "string", "description": "inferred = never confirmed; don't trust it. Unknown values read as estimated.", "examples": ["captured", "estimated", "confirmed", "inferred"] },
        "rounding": { "type": "string", "examples": ["port", "starboard"] },
        "capturedAt": { "type": "number" },
        "pinnedAt": { "type": "array", "items": { "type": "number" } }
      }
    }
  }
}
