{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "imports-v2/iterations.json",
  "title": "Iterations",
  "description": "An iteration (sprint) of a space, such as \"Sprint 1\", with a goal and dates. The iterations of a space keep the order of their records. Its cards are `iteration_cards`. `status` is planned, active or closed; a closed iteration can also have `actual_finish_date`. A `planned` iteration may have no dates; an `active` or `closed` one needs `start_date` and `finish_date`, a `closed` one also `actual_finish_date`. `finish_date` and `actual_finish_date` must be later than `start_date`.",
  "type": "object",
  "properties": {
    "external_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 512,
      "description": "Your own identifier of the record, for example `k-mrr`, unique within the resource across all of its files and case sensitive. Other records refer to this one by it."
    },
    "space_ref": {
      "type": "string",
      "minLength": 1,
      "maxLength": 512,
      "x-ref": "spaces",
      "description": "The space the iteration belongs to. Give the external_id of a `spaces` record."
    },
    "title": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "description": "Name of the iteration, for example `Sprint 1`. Up to 256 characters."
    },
    "goal": {
      "type": [
        "string",
        "null"
      ],
      "description": "What the iteration is meant to achieve, in a sentence or two."
    },
    "status": {
      "type": "string",
      "enum": [
        "planned",
        "active",
        "closed"
      ],
      "description": "Stage of the iteration: `planned` (the default), `active` or `closed`.",
      "default": "planned"
    },
    "start_date": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})$",
      "description": "When the iteration starts, for example `2026-01-12T09:00:00Z`. Required for an `active` or `closed` iteration.",
      "x-required-when": "status is active or closed"
    },
    "finish_date": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})$",
      "description": "When the iteration is planned to finish; later than `start_date`. Required for an `active` or `closed` iteration.",
      "x-required-when": "status is active or closed"
    },
    "actual_finish_date": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})$",
      "description": "When a closed iteration really finished; later than `start_date`. Required for a `closed` iteration, and needs `start_date` and `finish_date`.",
      "x-required-when": "status is closed"
    },
    "author_ref": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1,
      "maxLength": 512,
      "x-ref": "users",
      "description": "The user who created the record, by the external_id of a `users` record, for example `u-ava`. Leave it out or use null when the author is unknown or has left."
    },
    "updated_by_ref": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1,
      "maxLength": 512,
      "x-ref": "users",
      "description": "The user who made the last change, by the external_id of a `users` record. Leave it out or use null when unknown."
    },
    "created_at": {
      "type": "string",
      "format": "date-time",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})$",
      "description": "When the record was created, with a time zone, for example `2026-01-12T09:30:00Z`.",
      "x-default-text": "source.exported_at of the manifest"
    }
  },
  "required": [
    "external_id",
    "space_ref",
    "title"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "status": {
            "enum": [
              "active",
              "closed"
            ]
          }
        },
        "required": [
          "status"
        ]
      },
      "then": {
        "required": [
          "start_date",
          "finish_date"
        ],
        "properties": {
          "start_date": {
            "type": "string"
          },
          "finish_date": {
            "type": "string"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "status": {
            "enum": [
              "closed"
            ]
          }
        },
        "required": [
          "status"
        ]
      },
      "then": {
        "required": [
          "actual_finish_date"
        ],
        "properties": {
          "actual_finish_date": {
            "type": "string"
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "actual_finish_date"
        ],
        "properties": {
          "actual_finish_date": {
            "type": "string"
          }
        }
      },
      "then": {
        "required": [
          "start_date",
          "finish_date"
        ],
        "properties": {
          "start_date": {
            "type": "string"
          },
          "finish_date": {
            "type": "string"
          }
        }
      }
    }
  ]
}
