{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://elysium.is/opentime/schemas/v0.2/opentime-schema.json",
  "title": "OpenTime Document",
  "description": "OpenTime (.ot) file format specification v0.2 - The Markdown of Time",
  "type": "object",
  "required": ["opentime_version", "items"],
  "properties": {
    "opentime_version": {
      "type": "string",
      "description": "Semantic-style version of the OpenTime format",
      "pattern": "^\\d+\\.\\d+(\\.\\d+)?$",
      "examples": ["0.2", "0.2.1"]
    },
    "default_timezone": {
      "type": "string",
      "description": "IANA timezone name used as default for items without explicit timezone",
      "examples": ["Asia/Tokyo", "America/New_York", "Europe/London"]
    },
    "generated_by": {
      "type": "string",
      "description": "Application name and version that generated this file",
      "examples": ["Elysium 0.3"]
    },
    "created_at": {
      "type": "string",
      "format": "date-time",
      "description": "ISO8601 UTC timestamp when the file was created",
      "examples": ["2025-12-02T12:00:00Z"]
    },
    "items": {
      "type": "array",
      "description": "List of schedule items",
      "items": {
        "$ref": "#/definitions/item"
      }
    }
  },
  "additionalProperties": true,
  "definitions": {
    "item": {
      "type": "object",
      "required": ["type", "id", "title"],
      "discriminator": {
        "propertyName": "type"
      },
      "oneOf": [
        { "$ref": "#/definitions/goal" },
        { "$ref": "#/definitions/task" },
        { "$ref": "#/definitions/habit" },
        { "$ref": "#/definitions/reminder" },
        { "$ref": "#/definitions/event" },
        { "$ref": "#/definitions/appointment" },
        { "$ref": "#/definitions/project" }
      ]
    },
    "commonFields": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "enum": ["goal", "task", "habit", "reminder", "event", "appointment", "project"]
        },
        "id": {
          "type": "string",
          "description": "Unique identifier within the file",
          "minLength": 1
        },
        "title": {
          "type": "string",
          "description": "Human-readable title",
          "minLength": 1
        },
        "tags": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Free-form tags for categorization"
        },
        "notes": {
          "type": "string",
          "description": "Free-form notes (supports multiline)"
        },
        "links": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/link"
          },
          "description": "Links to URLs or other items"
        }
      }
    },
    "link": {
      "type": "object",
      "required": ["kind", "value"],
      "properties": {
        "kind": {
          "type": "string",
          "enum": ["url", "ref"],
          "description": "Type of link: 'url' for external URLs, 'ref' for internal item references"
        },
        "value": {
          "type": "string",
          "description": "URL or item ID being referenced"
        }
      }
    },
    "date": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}$",
      "description": "Date in YYYY-MM-DD format",
      "examples": ["2025-12-03"]
    },
    "datetime": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}([+-]\\d{2}:\\d{2}|Z)$",
      "description": "ISO8601 datetime with timezone offset",
      "examples": ["2025-12-03T09:00:00+09:00", "2025-12-03T00:00:00Z"]
    },
    "time": {
      "type": "string",
      "pattern": "^\\d{2}:\\d{2}$",
      "description": "Time in HH:MM format (24-hour)",
      "examples": ["09:00", "14:30"]
    },
    "progress": {
      "type": "number",
      "minimum": 0.0,
      "maximum": 1.0,
      "description": "Progress value from 0.0 (not started) to 1.0 (complete)"
    },
    "goal": {
      "type": "object",
      "required": ["type", "id", "title", "kind"],
      "properties": {
        "type": { "const": "goal" },
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "kind": { "const": "goal" },
        "target_date": { "$ref": "#/definitions/date" },
        "progress": { "$ref": "#/definitions/progress" },
        "tags": { "type": "array", "items": { "type": "string" } },
        "notes": { "type": "string" },
        "links": { "type": "array", "items": { "$ref": "#/definitions/link" } },
        "project_id": {
          "type": "string",
          "description": "ID of the project this goal belongs to"
        }
      },
      "additionalProperties": true
    },
    "task": {
      "type": "object",
      "required": ["type", "id", "title", "status"],
      "properties": {
        "type": { "const": "task" },
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "status": {
          "type": "string",
          "enum": ["todo", "in_progress", "done", "cancelled"],
          "description": "Current status of the task"
        },
        "due": { "$ref": "#/definitions/date" },
        "scheduled_start": { "$ref": "#/definitions/datetime" },
        "estimate_minutes": {
          "type": "integer",
          "minimum": 0,
          "description": "Estimated time to complete in minutes"
        },
        "actual_minutes": {
          "type": "integer",
          "minimum": 0,
          "description": "Actual time spent in minutes"
        },
        "priority": {
          "type": "number",
          "description": "Priority level (e.g., 1-10)"
        },
        "goal_id": {
          "type": "string",
          "description": "ID of the goal this task supports"
        },
        "project_id": {
          "type": "string",
          "description": "ID of the project this task belongs to"
        },
        "tags": { "type": "array", "items": { "type": "string" } },
        "notes": { "type": "string" },
        "links": { "type": "array", "items": { "$ref": "#/definitions/link" } }
      },
      "additionalProperties": true
    },
    "habitPattern": {
      "type": "object",
      "properties": {
        "freq": {
          "type": "string",
          "enum": ["daily", "weekly", "custom"],
          "description": "Frequency of the habit"
        },
        "days_of_week": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
          },
          "description": "Days of week for weekly/custom frequency"
        }
      }
    },
    "habitWindow": {
      "type": "object",
      "properties": {
        "start_time": { "$ref": "#/definitions/time" },
        "end_time": { "$ref": "#/definitions/time" }
      }
    },
    "habitStreak": {
      "type": "object",
      "properties": {
        "current": {
          "type": "integer",
          "minimum": 0,
          "description": "Current streak count"
        },
        "longest": {
          "type": "integer",
          "minimum": 0,
          "description": "Longest streak ever achieved"
        }
      }
    },
    "habit": {
      "type": "object",
      "required": ["type", "id", "title"],
      "properties": {
        "type": { "const": "habit" },
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "pattern": { "$ref": "#/definitions/habitPattern" },
        "window": { "$ref": "#/definitions/habitWindow" },
        "streak": { "$ref": "#/definitions/habitStreak" },
        "goal_id": { "type": "string" },
        "project_id": { "type": "string" },
        "tags": { "type": "array", "items": { "type": "string" } },
        "notes": { "type": "string" },
        "links": { "type": "array", "items": { "$ref": "#/definitions/link" } }
      },
      "additionalProperties": true
    },
    "reminder": {
      "type": "object",
      "required": ["type", "id", "title", "time"],
      "properties": {
        "type": { "const": "reminder" },
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "time": { "$ref": "#/definitions/datetime" },
        "repeat": {
          "type": "string",
          "description": "RRULE-style recurrence rule",
          "examples": ["FREQ=DAILY;COUNT=5", "FREQ=HOURLY;COUNT=8"]
        },
        "link": {
          "type": "string",
          "description": "ID of related task/event/habit/goal"
        },
        "tags": { "type": "array", "items": { "type": "string" } },
        "notes": { "type": "string" },
        "links": { "type": "array", "items": { "$ref": "#/definitions/link" } }
      },
      "additionalProperties": true
    },
    "event": {
      "type": "object",
      "required": ["type", "id", "title", "start", "end"],
      "properties": {
        "type": { "const": "event" },
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "start": { "$ref": "#/definitions/datetime" },
        "end": { "$ref": "#/definitions/datetime" },
        "all_day": {
          "type": "boolean",
          "description": "Whether this is an all-day event"
        },
        "timezone": {
          "type": "string",
          "description": "IANA timezone name; falls back to default_timezone if omitted"
        },
        "location": { "type": "string" },
        "recurrence": {
          "type": "string",
          "description": "RRULE-style recurrence rule"
        },
        "goal_id": { "type": "string" },
        "project_id": { "type": "string" },
        "tags": { "type": "array", "items": { "type": "string" } },
        "notes": { "type": "string" },
        "links": { "type": "array", "items": { "$ref": "#/definitions/link" } }
      },
      "additionalProperties": true
    },
    "appointment": {
      "type": "object",
      "required": ["type", "id", "title", "start", "end", "attendees"],
      "properties": {
        "type": { "const": "appointment" },
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "start": { "$ref": "#/definitions/datetime" },
        "end": { "$ref": "#/definitions/datetime" },
        "attendees": {
          "type": "array",
          "items": { "type": "string" },
          "minItems": 1,
          "description": "Names or emails of attendees"
        },
        "location": { "type": "string" },
        "provider": {
          "type": "string",
          "description": "Service provider (e.g., 'dentist', 'therapist', 'Zoom')"
        },
        "goal_id": { "type": "string" },
        "project_id": { "type": "string" },
        "tags": { "type": "array", "items": { "type": "string" } },
        "notes": { "type": "string" },
        "links": { "type": "array", "items": { "$ref": "#/definitions/link" } }
      },
      "additionalProperties": true
    },
    "project": {
      "type": "object",
      "required": ["type", "id", "title", "kind"],
      "properties": {
        "type": { "const": "project" },
        "id": { "type": "string", "minLength": 1 },
        "title": { "type": "string", "minLength": 1 },
        "kind": { "const": "project" },
        "children": {
          "type": "array",
          "items": { "type": "string" },
          "description": "List of item IDs that belong to this project"
        },
        "progress": { "$ref": "#/definitions/progress" },
        "target_date": { "$ref": "#/definitions/date" },
        "tags": { "type": "array", "items": { "type": "string" } },
        "notes": { "type": "string" },
        "links": { "type": "array", "items": { "$ref": "#/definitions/link" } }
      },
      "additionalProperties": true
    }
  }
}
