openapi: 3.0.3
info:
  title: Easerix Croncrunch API
  description: |
    Time tracking (backend/services/products/croncrunch): clients, projects,
    project tasks, time entries, expenses with optional receipts, timesheet
    templates, a server-held running timer, report summaries, and team time
    off (policies, a holiday calendar, and requests).
    Responses are camelCase, hand-built DTOs. All /v1 routes require a bearer
    JWT with org claims and the "croncrunch" tool enabled (authkit).
    Authored from the shipping Gin handlers — this spec records reality.
  version: 1.0.0
  contact:
    name: Perizer Labs
    url: https://easerix.com
servers:
  - url: https://api.easerix.com/croncrunch
    description: Production
tags:
  - name: system
    description: Health and liveness
  - name: clients
    description: Billing clients
  - name: projects
    description: Projects time is tracked against
  - name: tasks
    description: Named tasks that belong to a project
  - name: entries
    description: Time entries
  - name: timer
    description: The single server-held running timer
  - name: reports
    description: Aggregated reporting
  - name: expenses
    description: >-
      Costs logged by the caller (software, travel, contractors). Private to
      the person who logged them, the same way time entries are. A receipt is
      an optional file in the same object storage screenshots use. Approval
      is not part of this surface.
  - name: timesheets
    description: >-
      Week submission and approval, plus templates and copying last week.
      Submit, list, and review are team workspaces only, and review is
      limited to an owner or admin. A submitted or approved week rejects
      entry writes, copy, and template apply with 409. Reject reopens the
      week. Templates and copy work in every workspace and stay private to
      the caller.
  - name: time-off
    description: >-
      Team time off. Policies are a yearly allowance (vacation, sick). The
      holiday calendar is the workspace's. A member requests a date range and
      an owner or admin approves or rejects it. Weekends and holidays are not
      deducted. There is no payroll export.
  - name: monitoring
    description: >-
      Opt-in desktop monitoring: per-user capture settings, activity events
      (foreground app/window), and screenshots. Everything is disabled until
      the user turns it on; enabling records consentAt.

security:
  - bearer: []

paths:
  /health:
    get:
      operationId: health
      tags: [system]
      summary: Liveness probe
      description: Unauthenticated liveness check used by the platform and monitors.
      security: []
      responses:
        "200":
          description: Service is up
          content:
            application/json:
              schema:
                type: object

  /v1/clients:
    get:
      operationId: listClients
      tags: [clients]
      summary: List clients
      description: >-
        Clients the caller can read: their own (active-org-scoped; legacy rows
        without org_id stay visible) plus any shared with the organization by a
        teammate. Ordered by name ascending.
      parameters:
        - name: scope
          in: query
          description: >-
            all (default) — everything readable · mine — only the caller's own
            · shared — only teammates' org-shared clients (excludes own).
          schema:
            type: string
            enum: [all, mine, shared]
        - name: crm_company_id
          in: query
          description: Only clients linked to this CRM company (the CRM company page reads the linkage this way).
          schema:
            type: string
      responses:
        "200":
          description: Clients
          content:
            application/json:
              schema:
                type: object
                required: [clients]
                properties:
                  clients:
                    type: array
                    items: { $ref: "#/components/schemas/Client" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: createClient
      tags: [clients]
      summary: Create a client
      description: Creates a client. Color defaults to "#c53a17" when omitted.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties:
                name: { type: string }
                color: { type: string }
                logo:
                  type: string
                  description: >-
                    Optional data URL (data:image/jpeg, image/png, or image/webp;
                    base64) of a company logo or client photo. Decoded image at
                    most 256 KB.
      responses:
        "201":
          description: Client created
          content:
            application/json:
              schema:
                type: object
                required: [client]
                properties:
                  client: { $ref: "#/components/schemas/Client" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/clients/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    patch:
      operationId: updateClient
      tags: [clients]
      summary: Rename, recolor, or share a client
      description: >-
        Owner-only. A client a teammate shared with the organization is
        readable but not editable — it answers 403, not 404.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                color: { type: string }
                visibility: { type: string, enum: [private, org] }
                crmCompanyId:
                  type: string
                  description: Link to a CRM company id; "" clears the link.
                logo:
                  type: string
                  description: >-
                    Data URL of a company logo or client photo. "" removes the
                    current logo. Omit the field to leave it unchanged.
      responses:
        "200":
          description: Updated client
          content:
            application/json:
              schema:
                type: object
                required: [client]
                properties:
                  client: { $ref: "#/components/schemas/Client" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }
    delete:
      operationId: deleteClient
      tags: [clients]
      summary: Delete a client
      description: >-
        Owner-only. A client owned by nobody the caller can see reads as not
        found; one shared with them by a teammate answers 403.
      responses:
        "200":
          description: Deleted
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/projects:
    get:
      operationId: listProjects
      tags: [projects]
      summary: List projects
      description: >-
        Projects the caller can read: their own (active-org-scoped; legacy rows
        without org_id stay visible) plus any shared with the organization by a
        teammate. Ordered by name ascending, with client names resolved.
      parameters:
        - name: scope
          in: query
          description: >-
            all (default) — everything readable · mine — only the caller's own
            · shared — only teammates' org-shared projects (excludes own).
          schema:
            type: string
            enum: [all, mine, shared]
      responses:
        "200":
          description: Projects
          content:
            application/json:
              schema:
                type: object
                required: [projects]
                properties:
                  projects:
                    type: array
                    items: { $ref: "#/components/schemas/Project" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: createProject
      tags: [projects]
      summary: Create a project
      description: >-
        Creates a project. Defaults: color "#16233f", billable true. clientId is
        optional and not validated against ownership.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties:
                name: { type: string }
                color: { type: string }
                clientId:
                  type: string
                  nullable: true
                hourlyRate: { type: number }
                billable: { type: boolean }
                budgetSeconds:
                  type: integer
                  description: Optional all-time hours budget; values <= 0 mean no budget
      responses:
        "201":
          description: Project created
          content:
            application/json:
              schema:
                type: object
                required: [project]
                properties:
                  project: { $ref: "#/components/schemas/Project" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/projects/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    patch:
      operationId: updateProject
      tags: [projects]
      summary: Edit or share a project
      description: >-
        Owner-only. A project a teammate shared with the organization can be
        read and logged against, but not edited — it answers 403, not 404.
        Sending clientId as an empty string clears the client.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                color: { type: string }
                clientId:
                  type: string
                  nullable: true
                hourlyRate: { type: number }
                billable: { type: boolean }
                visibility: { type: string, enum: [private, org] }
                budgetSeconds:
                  type: integer
                  description: All-time hours budget; 0 (or negative) clears it
      responses:
        "200":
          description: Updated project
          content:
            application/json:
              schema:
                type: object
                required: [project]
                properties:
                  project: { $ref: "#/components/schemas/Project" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }
    delete:
      operationId: deleteProject
      tags: [projects]
      summary: Delete a project
      description: >-
        Owner-only. A project shared with the caller by a teammate answers 403.
      responses:
        "200":
          description: Deleted
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/projects/{id}/favorite:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    put:
      operationId: setProjectFavorite
      tags: [projects]
      summary: Star or unstar a project
      description: >-
        Any project the caller can read may be favorited. Favorites sort to the
        top of project pickers for that user.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [favorite]
              properties:
                favorite: { type: boolean }
      responses:
        "200":
          description: Updated project
          content:
            application/json:
              schema:
                type: object
                required: [project]
                properties:
                  project: { $ref: "#/components/schemas/Project" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/projects/{id}/tasks:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    get:
      operationId: listProjectTasks
      tags: [tasks]
      summary: List a project's tasks
      description: >-
        Tasks on a project the caller can read, ordered by name. Archived tasks
        are omitted unless includeArchived is true. A teammate can list tasks
        on a project shared with the workspace. Only the project owner can
        change them.
      parameters:
        - name: includeArchived
          in: query
          schema: { type: string, enum: ["true"] }
          description: Pass true to include archived tasks.
      responses:
        "200":
          description: Tasks
          content:
            application/json:
              schema:
                type: object
                required: [tasks]
                properties:
                  tasks:
                    type: array
                    items: { $ref: "#/components/schemas/ProjectTask" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
    post:
      operationId: createProjectTask
      tags: [tasks]
      summary: Add a task to a project
      description: >-
        Owner-only. A project shared with the caller answers 403. billable and
        hourlyRate are optional overrides: omit them, or send null, to inherit
        the project. Names are unique per project, ignoring case, including
        archived tasks.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties:
                name: { type: string }
                billable:
                  type: boolean
                  nullable: true
                  description: Override. Null inherits the project's billable flag.
                hourlyRate:
                  type: number
                  nullable: true
                  description: Override. Null inherits the project's hourly rate. Must be >= 0.
      responses:
        "201":
          description: Task created
          content:
            application/json:
              schema:
                type: object
                required: [task]
                properties:
                  task: { $ref: "#/components/schemas/ProjectTask" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/projects/{id}/tasks/{taskId}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
      - { name: taskId, in: path, required: true, schema: { type: string } }
    patch:
      operationId: updateProjectTask
      tags: [tasks]
      summary: Edit or archive a task
      description: >-
        Owner-only. Send billable or hourlyRate as null to clear that override.
        Omit a field to leave it. archived hides the task from pickers. Existing
        entries keep the link, and the task can be chosen again after it is
        unarchived. There is no delete: archiving is how a task leaves the picker
        without breaking reports.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                billable:
                  type: boolean
                  nullable: true
                hourlyRate:
                  type: number
                  nullable: true
                archived: { type: boolean }
      responses:
        "200":
          description: Updated task
          content:
            application/json:
              schema:
                type: object
                required: [task]
                properties:
                  task: { $ref: "#/components/schemas/ProjectTask" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-entries:
    get:
      operationId: listEntries
      tags: [entries]
      summary: List time entries
      description: >-
        The caller's OWN entries only — sharing a project never exposes anyone's
        hours, so there is no scope lens here. Ordered started_at descending,
        hard limit 500. from/to filter on started_at. projectId, taskId and tagId
        narrow the list. billingStatus is unbilled, invoiced, or paid. source is
        timer, manual, or desktop.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date-time } }
        - { name: to, in: query, schema: { type: string, format: date-time } }
        - { name: projectId, in: query, schema: { type: string } }
        - { name: taskId, in: query, schema: { type: string } }
        - { name: tagId, in: query, schema: { type: string } }
        - { name: billingStatus, in: query, schema: { type: string, enum: [unbilled, invoiced, paid] } }
        - { name: source, in: query, schema: { type: string, enum: [timer, manual, desktop] } }
      responses:
        "200":
          description: Entries (max 500)
          content:
            application/json:
              schema:
                type: object
                required: [entries]
                properties:
                  entries:
                    type: array
                    maxItems: 500
                    items: { $ref: "#/components/schemas/TimeEntry" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: createEntry
      tags: [entries]
      summary: Log a time entry
      description: >-
        Logs time against any project the caller can read — their own or one
        shared with the organization by a teammate. The entry belongs to the
        caller and stays private to them. startedAt defaults to now. With
        endedAt, duration is derived; with only durationSeconds,
        endedAt = startedAt + duration. One of endedAt or durationSeconds is
        required (timers start via /v1/timer/start). Timestamps are strict
        RFC3339. taskId, when set, must be an active task on that project.
        isBillable defaults true, unless the task has a billable override.
        source is always manual. A source field in the body is ignored, so
        typed time cannot be labeled as tracked. 409 when the entry's time
        falls in a week the caller has submitted or had approved. A workspace
        can require project, description, task, or tag. A missing required
        field is 400 and the error names that field ("description is
        required"). A member cannot create an entry whose start falls in the
        workspace time lock (older than N days, or before a date). That
        refusal is also 400 and names the lock. An owner or admin can still
        create it.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [projectId]
              properties:
                projectId: { type: string }
                taskId:
                  type: string
                  description: Active task on projectId. Omit for no task.
                description: { type: string }
                startedAt: { type: string, format: date-time }
                endedAt: { type: string, format: date-time }
                durationSeconds: { type: integer }
                isBillable: { type: boolean }
                tagIds:
                  type: array
                  items: { type: string }
                  description: Tags to attach; every id must be the caller's
      responses:
        "201":
          description: Entry created
          content:
            application/json:
              schema:
                type: object
                required: [entry]
                properties:
                  entry: { $ref: "#/components/schemas/TimeEntry" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "409": { $ref: "#/components/responses/WeekLocked" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-entries/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    patch:
      operationId: updateEntry
      tags: [entries]
      summary: Update an entry
      description: >-
        Partial update. projectId must be readable by the caller (their own or
        an org-shared project). taskId must belong to the resulting project.
        An empty taskId clears it. Moving the entry to another project while it
        still points at the old project's task is rejected. An archived task
        can stay on the entry but cannot be newly chosen. startedAt/endedAt
        are strict RFC3339; durationSeconds recomputes endedAt from startedAt
        (rejected on a running timer). endedAt must not precede startedAt;
        duration_seconds is recomputed whenever times change. 409 when the
        entry's current or new time falls in a submitted or approved week.
        source is fixed at creation and cannot be changed here. The entry
        that would result must still satisfy the workspace's required fields;
        a 400 names the missing one. A member cannot change an entry whose
        current or new time falls in the workspace time lock. That refusal is
        400 and names the lock. An owner or admin can still change it.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                description: { type: string }
                projectId: { type: string }
                taskId:
                  type: string
                  description: Replaces the entry's task. An empty string clears it.
                isBillable: { type: boolean }
                startedAt: { type: string, format: date-time }
                endedAt: { type: string, format: date-time }
                durationSeconds: { type: integer }
                tagIds:
                  type: array
                  items: { type: string }
                  description: Replaces the entry's tag set; [] clears it
      responses:
        "200":
          description: Updated entry
          content:
            application/json:
              schema:
                type: object
                required: [entry]
                properties:
                  entry: { $ref: "#/components/schemas/TimeEntry" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/WeekLocked" }
    delete:
      operationId: deleteEntry
      tags: [entries]
      summary: Delete an entry
      description: >-
        Owner-scoped delete. 409 when the entry falls in a submitted or
        approved week. 400 when a member deletes an entry inside the workspace
        time lock. An owner or admin can still delete it.
      responses:
        "200":
          description: Deleted
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409": { $ref: "#/components/responses/WeekLocked" }

  /v1/timer:
    get:
      operationId: getTimer
      tags: [timer]
      summary: The running timer
      description: The caller's running entry with a live durationSeconds, or null when nothing is running.
      responses:
        "200":
          description: Running timer or null
          content:
            application/json:
              schema:
                type: object
                required: [timer]
                properties:
                  timer:
                    allOf: [{ $ref: "#/components/schemas/TimeEntry" }]
                    nullable: true
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }

  /v1/timer/last-entry:
    get:
      operationId: getLastEntry
      tags: [timer]
      summary: Most recent completed entry
      description: >-
        The caller's latest finished time entry, for Continue-last-entry flows.
        Returns entry null when none exist.
      responses:
        "200":
          description: Last entry or null
          content:
            application/json:
              schema:
                type: object
                required: [entry]
                properties:
                  entry:
                    allOf: [{ $ref: "#/components/schemas/TimeEntry" }]
                    nullable: true
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }

  /v1/timer/start:
    post:
      operationId: startTimer
      tags: [timer]
      summary: Start the timer
      description: >-
        Stops any running timer first, then starts a new running entry on any
        project the caller can read — their own or one shared with the
        organization. Responds 200 (not 201) with the running entry, which
        belongs to the caller. taskId, when set, must be an active task on
        that project. A billable override on the task sets isBillable. source
        is timer when omitted, or desktop when the desktop tracker starts the
        clock. manual is rejected. startedAt, when set, is the moment the
        clock started on the client (the desktop tracker syncing a timer that
        ran offline). It is strict RFC3339, must not be in the future, and
        must not be older than 14 days. Omit it and the start is now. A
        previous running timer is still stopped at the server's current time.
        409 when now, or a timer this start would
        stop, falls in a submitted or approved week. The running timer is left
        running in that case. A missing workspace-required field (project,
        description, task, or tag) is 400 and names the field, and the running
        timer is left running. A member cannot start when now, or a timer this
        start would stop, falls in the workspace time lock. That refusal is
        400, names the lock, and leaves the running timer running. An owner or
        admin can still start.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [projectId]
              properties:
                projectId: { type: string }
                taskId:
                  type: string
                  description: Active task on projectId. Its billable override applies when set.
                description: { type: string }
                tagIds:
                  type: array
                  items: { type: string }
                source:
                  type: string
                  enum: [timer, desktop]
                  description: timer when omitted. desktop marks tracker time. manual is rejected.
                startedAt:
                  type: string
                  format: date-time
                  description: When the clock started on the client. Omit for now. Not in the future, not older than 14 days.
      responses:
        "200":
          description: The running entry
          content:
            application/json:
              schema:
                type: object
                required: [timer]
                properties:
                  timer: { $ref: "#/components/schemas/TimeEntry" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "409": { $ref: "#/components/responses/WeekLocked" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timer/stop:
    post:
      operationId: stopTimer
      tags: [timer]
      summary: Stop the timer
      description: >-
        Stops the running entry and returns it finalized, or entry null when
        nothing was running. An optional endedAt backdates the stop (clamped to
        the entry's start, rejected if in the future) so clients can discard
        trailing idle time. 409 when the running entry's start or the stop
        time falls in a submitted or approved week. The timer stays running.
        400 when a member stops a timer whose start is inside the workspace
        time lock. The timer stays running. An owner or admin can still stop it.
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: object
              properties:
                endedAt: { type: string, format: date-time }
      responses:
        "200":
          description: The stopped entry or null
          content:
            application/json:
              schema:
                type: object
                required: [entry]
                properties:
                  entry:
                    allOf: [{ $ref: "#/components/schemas/TimeEntry" }]
                    nullable: true
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "409": { $ref: "#/components/responses/WeekLocked" }

  /v1/reports/summary:
    get:
      operationId: reportsSummary
      tags: [reports]
      summary: Time summary
      description: >-
        Totals over the caller's OWN completed entries only (running excluded);
        sharing a project never adds a teammate's hours here. amount sums
        billable entries as seconds/3600 times the rate, rounded to the nearest
        cent on each entry and then totaled. The rate is the one saved on the
        entry when it was created (or when it was moved to another project or
        task): the task's hourlyRate when that entry had a task rate, otherwise
        the project's. An entry with no saved rate, from before snapshots,
        uses the task or project rate as it is now, so existing totals do not
        move when snapshots ship. Editing a project or task rate later does
        not change amounts for entries that already saved a rate.
        byProject, byTask and byDay arrays are unordered. byTask splits entries
        with no task into a row whose taskId is null. billingStatus narrows the
        totals to that slice (unbilled, invoiced, or paid). source narrows the
        same totals to timer, manual, or desktop. unbilledSeconds,
        invoicedSeconds and paidSeconds always describe the whole date window.
        bySource describes that window after the billing lens, and the source
        query does not narrow it.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date-time } }
        - { name: to, in: query, schema: { type: string, format: date-time } }
        - { name: billingStatus, in: query, schema: { type: string, enum: [unbilled, invoiced, paid] } }
        - { name: source, in: query, schema: { type: string, enum: [timer, manual, desktop] } }
      responses:
        "200":
          description: Aggregates
          content:
            application/json:
              schema:
                type: object
                required: [totalSeconds, billableSeconds, amount, byProject, byTask, byDay, bySource]
                properties:
                  totalSeconds: { type: integer }
                  billableSeconds: { type: integer }
                  unbilledSeconds: { type: integer }
                  invoicedSeconds: { type: integer }
                  paidSeconds: { type: integer }
                  amount: { type: number }
                  byProject:
                    type: array
                    items:
                      type: object
                      required: [projectId, name, color, seconds]
                      properties:
                        projectId: { type: string }
                        name: { type: string, description: Empty string for a deleted project }
                        color: { type: string }
                        seconds: { type: integer }
                  byTask:
                    type: array
                    items:
                      type: object
                      required: [taskId, name, projectId, projectName, color, seconds]
                      properties:
                        taskId:
                          type: string
                          nullable: true
                          description: Null for time that was not logged against a task
                        name:
                          type: string
                          description: Empty string when taskId is null
                        projectId: { type: string }
                        projectName:
                          type: string
                          description: Empty string for a deleted project
                        color: { type: string }
                        seconds: { type: integer }
                  byDay:
                    type: array
                    items:
                      type: object
                      required: [date, seconds]
                      properties:
                        date: { type: string, description: "UTC day, YYYY-MM-DD" }
                        seconds: { type: integer }
                  bySource:
                    type: array
                    minItems: 3
                    maxItems: 3
                    description: >-
                      Always three rows, desktop then timer then manual, including
                      zeroes. The source query does not narrow this array.
                    items:
                      type: object
                      required: [source, seconds]
                      properties:
                        source: { type: string, enum: [desktop, timer, manual] }
                        seconds: { type: integer }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }

  /v1/reports/org/summary:
    get:
      operationId: reportsOrgSummary
      tags: [reports]
      summary: Team time summary
      description: >-
        Owner or admin of a team workspace only. Personal workspaces, members,
        and the billing role receive 403. Totals are completed entries in the
        caller's workspace (running timers excluded), aggregated in SQL by
        member and project. amount is seconds/3600 times the rate over billable
        entries, rounded to the nearest cent on each entry and then totaled.
        The rate is the one saved on the entry when it was created (or when it
        was moved to another project or task): the task's hourlyRate when that
        entry had a task rate, otherwise the project's. An entry with no saved
        rate, from before snapshots, uses the task or project rate as it is
        now, so existing totals do not move when snapshots ship. Editing a
        project or task rate later does not change amounts for entries that
        already saved a rate. Names come from the auth member
        directory using the caller's own token. A member with no time in the
        window is still listed, with zeros. Someone who logged time and has
        since left has an empty name. byMember and byProject are ordered by
        seconds descending. This route does not return entry rows. The entry
        list and GET /v1/reports/summary stay limited to the caller's own hours.
        A workspace id in the query is ignored; the workspace is the token's.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date-time } }
        - { name: to, in: query, schema: { type: string, format: date-time } }
      responses:
        "200":
          description: Team aggregates
          content:
            application/json:
              schema:
                type: object
                required: [totalSeconds, billableSeconds, amount, byMember, byProject]
                properties:
                  totalSeconds: { type: integer }
                  billableSeconds: { type: integer }
                  amount: { type: number }
                  byMember:
                    type: array
                    items:
                      type: object
                      required: [userId, name, seconds, billableSeconds, amount]
                      properties:
                        userId: { type: string }
                        name:
                          type: string
                          description: Empty string when the person is no longer in the workspace directory
                        seconds: { type: integer }
                        billableSeconds: { type: integer }
                        amount: { type: number }
                  byProject:
                    type: array
                    items:
                      type: object
                      required: [projectId, name, color, seconds, billableSeconds, amount]
                      properties:
                        projectId: { type: string }
                        name: { type: string, description: Empty string for a deleted project or a project in another workspace }
                        color: { type: string }
                        seconds: { type: integer }
                        billableSeconds: { type: integer }
                        amount: { type: number }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }
        "502":
          description: The auth directory could not be reached
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }

  /v1/expenses:
    get:
      operationId: listExpenses
      tags: [expenses]
      summary: List expenses
      description: >-
        The caller's own expenses, newest first, at most 500. from and to are
        inclusive calendar dates (YYYY-MM-DD) on spentOn. Sharing a project
        never adds a teammate's expenses.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date } }
        - { name: to, in: query, schema: { type: string, format: date } }
        - { name: clientId, in: query, schema: { type: string } }
        - { name: projectId, in: query, schema: { type: string } }
        - { name: type, in: query, schema: { type: string } }
        - { name: billable, in: query, schema: { type: string, enum: ["true", "false"] } }
      responses:
        "200":
          description: Expenses (max 500)
          content:
            application/json:
              schema:
                type: object
                required: [expenses]
                properties:
                  expenses:
                    type: array
                    maxItems: 500
                    items: { $ref: "#/components/schemas/Expense" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: createExpense
      tags: [expenses]
      summary: Log an expense
      description: >-
        pricing defaults to amount, which requires amount. pricing unit
        requires quantity and unitRate, and amount is stored as their product
        rounded to cents. billable defaults to true. An empty clientId or
        projectId is allowed. A project the caller can read may be a teammate's
        shared project. When that project belongs to a client and clientId is
        omitted, the expense takes the project's client. A clientId that
        disagrees with the project's client is rejected.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [spentOn, type]
              properties:
                clientId: { type: string }
                projectId: { type: string }
                spentOn: { type: string, format: date }
                pricing: { type: string, enum: [amount, unit] }
                amount: { type: number, minimum: 0 }
                quantity: { type: number, description: Greater than 0 when pricing is unit }
                unitRate: { type: number, minimum: 0 }
                type: { type: string, description: "Free-text category, at most 80 characters" }
                billable: { type: boolean }
                note: { type: string }
      responses:
        "201":
          description: Expense created
          content:
            application/json:
              schema:
                type: object
                required: [expense]
                properties:
                  expense: { $ref: "#/components/schemas/Expense" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/expenses/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    patch:
      operationId: updateExpense
      tags: [expenses]
      summary: Update an expense
      description: >-
        Owner-scoped. Omitted fields stay as they are. clientId or projectId
        of "" clears that link. Sending pricing, amount, quantity, or unitRate
        recomputes the total from the merged values.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                clientId: { type: string, description: Empty string clears the client }
                projectId: { type: string, description: Empty string clears the project }
                spentOn: { type: string, format: date }
                pricing: { type: string, enum: [amount, unit] }
                amount: { type: number, minimum: 0 }
                quantity: { type: number, description: Greater than 0 when pricing is unit }
                unitRate: { type: number, minimum: 0 }
                type: { type: string }
                billable: { type: boolean }
                note: { type: string }
      responses:
        "200":
          description: Updated expense
          content:
            application/json:
              schema:
                type: object
                required: [expense]
                properties:
                  expense: { $ref: "#/components/schemas/Expense" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }
    delete:
      operationId: deleteExpense
      tags: [expenses]
      summary: Delete an expense
      description: Owner-scoped. Also deletes the stored receipt, best effort.
      responses:
        "200":
          description: Deleted
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/expenses/{id}/receipt:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    post:
      operationId: uploadExpenseReceipt
      tags: [expenses]
      summary: Upload or replace a receipt
      description: >-
        Multipart file for an expense the caller owns. image/jpeg, image/png,
        image/webp, or application/pdf, at most 8 MB. The bytes are checked
        against the declared type. Replacing a receipt deletes the previous
        object. 503 when object storage is not configured.
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file: { type: string, format: binary }
      responses:
        "200":
          description: Receipt stored
          content:
            application/json:
              schema:
                type: object
                required: [expense]
                properties:
                  expense: { $ref: "#/components/schemas/Expense" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }
        "503":
          description: Receipt storage not configured
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
    delete:
      operationId: deleteExpenseReceipt
      tags: [expenses]
      summary: Remove a receipt
      description: Owner-scoped. No receipt is a success and leaves the expense in place.
      responses:
        "200":
          description: Receipt removed, or there was none
          content:
            application/json:
              schema:
                type: object
                required: [expense]
                properties:
                  expense: { $ref: "#/components/schemas/Expense" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/reports/expenses:
    get:
      operationId: reportsExpenses
      tags: [reports, expenses]
      summary: Expense report
      description: >-
        Totals over the caller's own expenses for the same filters as the list.
        Unlike the list, this is not capped at 500 (hard cap 10000). byClient,
        byProject and byType are ordered by total descending. byDay is ordered
        by date ascending. A missing client or project is one row named
        "No client" or "No project" with a null id.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date } }
        - { name: to, in: query, schema: { type: string, format: date } }
        - { name: clientId, in: query, schema: { type: string } }
        - { name: projectId, in: query, schema: { type: string } }
        - { name: type, in: query, schema: { type: string } }
        - { name: billable, in: query, schema: { type: string, enum: ["true", "false"] } }
      responses:
        "200":
          description: Aggregates
          content:
            application/json:
              schema:
                type: object
                required: [total, billable, nonBillable, count, byClient, byProject, byType, byDay]
                properties:
                  total: { type: number }
                  billable: { type: number }
                  nonBillable: { type: number }
                  count: { type: integer }
                  byClient:
                    type: array
                    items:
                      type: object
                      required: [clientId, name, total, billable, count]
                      properties:
                        clientId: { type: string, nullable: true }
                        name: { type: string }
                        total: { type: number }
                        billable: { type: number }
                        count: { type: integer }
                  byProject:
                    type: array
                    items:
                      type: object
                      required: [projectId, name, total, billable, count]
                      properties:
                        projectId: { type: string, nullable: true }
                        name: { type: string }
                        total: { type: number }
                        billable: { type: number }
                        count: { type: integer }
                  byType:
                    type: array
                    items:
                      type: object
                      required: [type, name, total, billable, count]
                      properties:
                        type: { type: string }
                        name: { type: string }
                        total: { type: number }
                        billable: { type: number }
                        count: { type: integer }
                  byDay:
                    type: array
                    items:
                      type: object
                      required: [date, total, count]
                      properties:
                        date: { type: string, format: date }
                        total: { type: number }
                        count: { type: integer }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/reports/expenses.csv:
    get:
      operationId: reportsExpensesCsv
      tags: [reports, expenses]
      summary: Expense report as CSV
      description: >-
        The same window as the JSON report, one row per expense, oldest first.
        Columns: date, type, client, project, pricing, quantity, unit_rate,
        amount, billable, note, receipt. A value that would be a spreadsheet
        formula is prefixed with an apostrophe.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date } }
        - { name: to, in: query, schema: { type: string, format: date } }
        - { name: clientId, in: query, schema: { type: string } }
        - { name: projectId, in: query, schema: { type: string } }
        - { name: type, in: query, schema: { type: string } }
        - { name: billable, in: query, schema: { type: string, enum: ["true", "false"] } }
      responses:
        "200":
          description: CSV attachment
          content:
            text/csv:
              schema: { type: string }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/billing/mark:
    post:
      operationId: markBilling
      tags: [reports]
      summary: Mark a window of time invoiced or paid
      description: >-
        Owner or admin only. Moves finished billable entries in the window one
        step: unbilled to invoiced, or invoiced to paid. In a team workspace
        that is every member's time in the workspace; elsewhere it is the
        caller's own.
        Exactly one of clientId or projectId. Running entries and non-billable
        entries are left alone. In a team workspace, unbilled time is invoiced
        only when its week is an approved timesheet. Other entries in the
        window are counted in skipped, and the call is 409 when none of them
        are approved. Does not number or send an invoice.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [from, to, status]
              properties:
                clientId: { type: string }
                projectId: { type: string }
                from: { type: string, format: date-time }
                to: { type: string, format: date-time }
                status: { type: string, enum: [invoiced, paid] }
      responses:
        "200":
          description: Entries updated
          content:
            application/json:
              schema:
                type: object
                required: [updated, skipped]
                properties:
                  updated: { type: integer }
                  skipped:
                    type: integer
                    description: Finished billable entries left unbilled because their week is not approved. Zero outside a team workspace.
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "409":
          description: In a team workspace, none of the time sits in an approved week
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheets/submit:
    post:
      operationId: submitTimesheet
      tags: [timesheets]
      summary: Submit a week for approval
      description: >-
        Team workspaces only. weekStart is YYYY-MM-DD and must be the first
        day of the caller's week (Monday, or Sunday when that is their
        preference) and not in the future. A rejected week can be submitted
        again. A week that is already submitted or approved is 409, and so is
        a week while a timer started in or before it is still running.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [weekStart]
              properties:
                weekStart: { type: string, format: date }
      responses:
        "200":
          description: A rejected week was submitted again
          content:
            application/json:
              schema:
                type: object
                required: [timesheet]
                properties:
                  timesheet: { $ref: "#/components/schemas/Timesheet" }
        "201":
          description: Week submitted
          content:
            application/json:
              schema:
                type: object
                required: [timesheet]
                properties:
                  timesheet: { $ref: "#/components/schemas/Timesheet" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "409": { $ref: "#/components/responses/WeekLocked" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheets:
    get:
      operationId: listTimesheets
      tags: [timesheets]
      summary: The caller's submitted weeks
      description: >-
        Team workspaces only. The caller's own rows in this workspace, newest
        week first, at most 104. weekStart limits the list to that week.
      parameters:
        - { name: weekStart, in: query, schema: { type: string, format: date } }
      responses:
        "200":
          description: Timesheets
          content:
            application/json:
              schema:
                type: object
                required: [timesheets]
                properties:
                  timesheets:
                    type: array
                    maxItems: 104
                    items: { $ref: "#/components/schemas/Timesheet" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheets/pending:
    get:
      operationId: listPendingTimesheets
      tags: [timesheets]
      summary: Weeks waiting for review
      description: >-
        Team workspaces only, and only an owner or admin (org.data.read_all).
        Submitted weeks in this workspace, oldest first, at most 50. Each row
        includes that member's finished entries for the week. This is the one
        place a teammate's hours are visible. The entry list stays private.
      responses:
        "200":
          description: The review queue
          content:
            application/json:
              schema:
                type: object
                required: [timesheets]
                properties:
                  timesheets:
                    type: array
                    maxItems: 50
                    items: { $ref: "#/components/schemas/TimesheetReview" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheets/{id}/approve:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    post:
      operationId: approveTimesheet
      tags: [timesheets]
      summary: Approve a submitted week
      description: >-
        Team workspaces only, owner or admin. The week must be submitted.
        Approved weeks stay locked.
      responses:
        "200":
          description: Approved
          content:
            application/json:
              schema:
                type: object
                required: [timesheet]
                properties:
                  timesheet: { $ref: "#/components/schemas/Timesheet" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: The timesheet is not waiting for review
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheets/{id}/reject:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    post:
      operationId: rejectTimesheet
      tags: [timesheets]
      summary: Reject a week and reopen it
      description: >-
        Team workspaces only, owner or admin. Submitted or approved weeks can
        be rejected, except an approved week with any invoiced or paid time,
        which is 409. note is required. The member can edit that week and
        submit it again.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [note]
              properties:
                note: { type: string, maxLength: 2000 }
      responses:
        "200":
          description: Rejected
          content:
            application/json:
              schema:
                type: object
                required: [timesheet]
                properties:
                  timesheet: { $ref: "#/components/schemas/Timesheet" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: The timesheet is already rejected, or the approved week has invoiced or paid time
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheets/copy:
    post:
      operationId: copyLastWeek
      tags: [timesheets]
      summary: Copy last week onto this week
      description: >-
        Copies the previous week's project and description rows onto
        weekStart. weekStart is YYYY-MM-DD and must be the first day of the
        caller's week. includeDurations also copies hours onto days that do
        not already have time. Days that already have time are left as they
        are and counted in skipped. A project the caller can no longer use
        is counted in unavailable. A submitted or approved week is 409 and
        is not changed. Last week having nothing to copy is 400.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [weekStart]
              properties:
                weekStart: { type: string, format: date }
                includeDurations:
                  type: boolean
                  description: When true, also copy hours onto empty days.
      responses:
        "200":
          description: Last week was copied, or there was nothing new to add
          content:
            application/json:
              schema:
                type: object
                required: [added, entries, skipped, unavailable]
                properties:
                  added:
                    type: integer
                    description: Activity rows created on the target week
                  entries:
                    type: integer
                    description: Time entries created. Zero when hours were not requested.
                  skipped:
                    type: integer
                    description: Days left unchanged because they already had time or are locked
                  unavailable:
                    type: integer
                    description: Activities skipped because the project or task is no longer available
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "409": { $ref: "#/components/responses/WeekLocked" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheet-rows:
    get:
      operationId: listTimesheetRows
      tags: [timesheets]
      summary: Activity rows for one week
      description: >-
        The caller's project and description rows for weekStart, including
        rows that do not have hours yet. At most 80.
      parameters:
        - { name: weekStart, in: query, required: true, schema: { type: string, format: date } }
      responses:
        "200":
          description: Rows for the week
          content:
            application/json:
              schema:
                type: object
                required: [rows]
                properties:
                  rows:
                    type: array
                    maxItems: 80
                    items: { $ref: "#/components/schemas/TimesheetActivity" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheet-rows/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    delete:
      operationId: deleteTimesheetRow
      tags: [timesheets]
      summary: Remove an activity row that has no time
      description: >-
        Removes one of the caller's activity rows. A row that already has
        time in that week is 409. A submitted or approved week is 409. Another
        person's row, or a row in another workspace, is 404.
      responses:
        "200":
          description: Row removed
          content:
            application/json:
              schema:
                type: object
                required: [deleted]
                properties:
                  deleted: { type: boolean }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: The row has time logged, or the week is submitted or approved
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheet-templates:
    get:
      operationId: listTimesheetTemplates
      tags: [timesheets]
      summary: The caller's timesheet templates
      description: >-
        Named project and description lists saved by the caller in this
        workspace. At most 40. A teammate's templates are not included.
      responses:
        "200":
          description: Templates
          content:
            application/json:
              schema:
                type: object
                required: [templates]
                properties:
                  templates:
                    type: array
                    maxItems: 40
                    items: { $ref: "#/components/schemas/TimesheetTemplate" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }
    post:
      operationId: createTimesheetTemplate
      tags: [timesheets]
      summary: Save a week as a template
      description: >-
        Stores the week's project and description rows under name. Hours are
        not stored. name is at most 80 characters and unique for the caller
        in this workspace, ignoring case. A week with no activities is 400.
        A submitted or approved week can still be saved, because saving does
        not change it.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name, weekStart]
              properties:
                name: { type: string, maxLength: 80 }
                weekStart: { type: string, format: date }
      responses:
        "201":
          description: Template saved
          content:
            application/json:
              schema:
                type: object
                required: [template]
                properties:
                  template: { $ref: "#/components/schemas/TimesheetTemplate" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "409":
          description: A template with that name already exists
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheet-templates/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    delete:
      operationId: deleteTimesheetTemplate
      tags: [timesheets]
      summary: Delete a timesheet template
      description: >-
        Deletes one of the caller's templates in this workspace. Weeks the
        template was already applied to keep their rows. Another person's
        template is 404.
      responses:
        "200":
          description: Template deleted
          content:
            application/json:
              schema:
                type: object
                required: [deleted]
                properties:
                  deleted: { type: boolean }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/timesheet-templates/{id}/apply:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    post:
      operationId: applyTimesheetTemplate
      tags: [timesheets]
      summary: Apply a template to an empty week
      description: >-
        Places the template's project and description rows on weekStart.
        The week must have no time and no activity rows. A submitted or
        approved week is 409. Hours are not copied. Activities whose project
        or task is no longer available are counted in unavailable and not
        placed. When none can be placed, the call is 400 and the week is
        unchanged.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [weekStart]
              properties:
                weekStart: { type: string, format: date }
      responses:
        "201":
          description: Template applied
          content:
            application/json:
              schema:
                type: object
                required: [added, unavailable]
                properties:
                  added: { type: integer }
                  unavailable: { type: integer }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: The week is submitted, approved, or already has time or activities
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/policies:
    get:
      operationId: listTimeOffPolicies
      tags: [time-off]
      summary: List time off policies
      description: >-
        Team workspaces only. Active policies, ordered by name. An owner or
        admin also receives archived policies. At most 100.
      responses:
        "200":
          description: Policies
          content:
            application/json:
              schema:
                type: object
                required: [policies]
                properties:
                  policies:
                    type: array
                    maxItems: 100
                    items: { $ref: "#/components/schemas/TimeOffPolicy" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }
    post:
      operationId: createTimeOffPolicy
      tags: [time-off]
      summary: Create a time off policy
      description: >-
        Team workspaces only, owner or admin. daysPerYear is the allowance
        for each calendar year, from 0 to 366. Names are unique among active
        policies, ignoring case.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name, daysPerYear]
              properties:
                name: { type: string, maxLength: 80 }
                daysPerYear: { type: integer, minimum: 0, maximum: 366 }
      responses:
        "201":
          description: Policy created
          content:
            application/json:
              schema:
                type: object
                required: [policy]
                properties:
                  policy: { $ref: "#/components/schemas/TimeOffPolicy" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "409":
          description: An active policy already uses that name
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/policies/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    patch:
      operationId: updateTimeOffPolicy
      tags: [time-off]
      summary: Update a time off policy
      description: >-
        Team workspaces only, owner or admin. Archived policies keep their
        requests and accept no new ones. A missing policy in this workspace
        is 404.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string, maxLength: 80 }
                daysPerYear: { type: integer, minimum: 0, maximum: 366 }
                archived: { type: boolean }
      responses:
        "200":
          description: Policy updated
          content:
            application/json:
              schema:
                type: object
                required: [policy]
                properties:
                  policy: { $ref: "#/components/schemas/TimeOffPolicy" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: An active policy already uses that name
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/holidays:
    get:
      operationId: listTimeOffHolidays
      tags: [time-off]
      summary: List holiday definitions
      description: >-
        Team workspaces only. The stored calendar, ordered by date. Yearly
        holidays are one row, not one row per year. At most 400.
      responses:
        "200":
          description: Holidays
          content:
            application/json:
              schema:
                type: object
                required: [holidays]
                properties:
                  holidays:
                    type: array
                    maxItems: 400
                    items: { $ref: "#/components/schemas/TimeOffHoliday" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }
    post:
      operationId: createTimeOffHoliday
      tags: [time-off]
      summary: Add a holiday
      description: >-
        Team workspaces only, owner or admin. date is YYYY-MM-DD.
        repeatsYearly uses that month and day every year. A yearly holiday
        blocks any other holiday on that month and day. Two one-off holidays
        can share a month and day when the dates differ. The same date cannot
        be stored twice.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name, date]
              properties:
                name: { type: string, maxLength: 80 }
                date: { type: string, format: date }
                repeatsYearly: { type: boolean }
      responses:
        "201":
          description: Holiday created
          content:
            application/json:
              schema:
                type: object
                required: [holiday]
                properties:
                  holiday: { $ref: "#/components/schemas/TimeOffHoliday" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "409":
          description: That date is taken, or a yearly holiday already covers that month and day
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/holidays/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    delete:
      operationId: deleteTimeOffHoliday
      tags: [time-off]
      summary: Remove a holiday
      description: >-
        Team workspaces only, owner or admin. Working-day counts are computed
        from the calendar as it stands, so removing a holiday can increase
        the days a request uses.
      responses:
        "200":
          description: Deleted
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/requests:
    get:
      operationId: listTimeOffRequests
      tags: [time-off]
      summary: The caller's time off requests
      description: >-
        Team workspaces only. The caller's own requests, newest first, at
        most 100. dayCount is Monday to Friday in the range, excluding holidays.
      responses:
        "200":
          description: Requests
          content:
            application/json:
              schema:
                type: object
                required: [requests]
                properties:
                  requests:
                    type: array
                    maxItems: 100
                    items: { $ref: "#/components/schemas/TimeOffRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }
    post:
      operationId: createTimeOffRequest
      tags: [time-off]
      summary: Request time off
      description: >-
        Team workspaces only. Opens a pending request for the caller.
        startDate and endDate are inclusive YYYY-MM-DD, at most a year apart,
        and between 2000 and 2100. Working days are Monday to Friday excluding
        holidays. A range with none is 400. Pending days count against the
        policy, and the range cannot overlap one of the caller's pending or
        approved requests.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [policyId, startDate, endDate]
              properties:
                policyId: { type: string }
                startDate: { type: string, format: date }
                endDate: { type: string, format: date }
                note: { type: string, maxLength: 2000 }
      responses:
        "201":
          description: Request opened
          content:
            application/json:
              schema:
                type: object
                required: [request]
                properties:
                  request: { $ref: "#/components/schemas/TimeOffRequest" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: The range overlaps, the policy is archived, or the allowance is used up
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/requests/pending:
    get:
      operationId: listPendingTimeOffRequests
      tags: [time-off]
      summary: Time off waiting for review
      description: >-
        Team workspaces only, owner or admin. Pending requests in this
        workspace, oldest first, at most 50. Each row adds remainingAfter,
        the tightest days left on that policy if this request is approved.
        This queue shows the requester and the days, not their time entries.
      responses:
        "200":
          description: The review queue
          content:
            application/json:
              schema:
                type: object
                required: [requests]
                properties:
                  requests:
                    type: array
                    maxItems: 50
                    items: { $ref: "#/components/schemas/TimeOffRequestReview" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/requests/{id}/approve:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    post:
      operationId: approveTimeOffRequest
      tags: [time-off]
      summary: Approve a time off request
      description: >-
        Team workspaces only, owner or admin. The request must be pending.
        The allowance is checked again, so a policy lowered after the request
        was made can still refuse it. A holiday added over the dates can
        leave no working day, which is also refused.
      responses:
        "200":
          description: Approved
          content:
            application/json:
              schema:
                type: object
                required: [request]
                properties:
                  request: { $ref: "#/components/schemas/TimeOffRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: The request is not waiting, it no longer fits the allowance, or it has no working days
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/requests/{id}/reject:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    post:
      operationId: rejectTimeOffRequest
      tags: [time-off]
      summary: Reject a time off request
      description: >-
        Team workspaces only, owner or admin. The request must be pending.
        note is required. The days return to the balance.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [note]
              properties:
                note: { type: string, maxLength: 2000 }
      responses:
        "200":
          description: Rejected
          content:
            application/json:
              schema:
                type: object
                required: [request]
                properties:
                  request: { $ref: "#/components/schemas/TimeOffRequest" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: The request is not waiting for review
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/requests/{id}/cancel:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    post:
      operationId: cancelTimeOffRequest
      tags: [time-off]
      summary: Cancel the caller's pending request
      description: >-
        Team workspaces only. The caller can cancel their own pending
        request. Someone else's request is 404. The days return to the balance.
        An approved request cannot be cancelled, and its days stay counted
        for the year.
      responses:
        "200":
          description: Cancelled
          content:
            application/json:
              schema:
                type: object
                required: [request]
                properties:
                  request: { $ref: "#/components/schemas/TimeOffRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "404": { $ref: "#/components/responses/NotFound" }
        "409":
          description: The request is not waiting for review
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/time-off/coverage:
    get:
      operationId: timeOffCoverage
      tags: [time-off]
      summary: Holidays, the caller's time off, and balances for a window
      description: >-
        Team workspaces only. from and to are inclusive YYYY-MM-DD and can
        cover at most a year. holidays are the calendar expanded into the
        window, including yearly repeats. A one-off holiday on a date wins
        over a yearly one. timeOff is the caller's pending and approved
        requests, one object per calendar day in the window, and never
        another member's. balances are the caller's active policies for each
        calendar year the window touches. Remaining days can be negative when
        an allowance was lowered after time was approved.
      parameters:
        - { name: from, in: query, required: true, schema: { type: string, format: date } }
        - { name: to, in: query, required: true, schema: { type: string, format: date } }
      responses:
        "200":
          description: The window
          content:
            application/json:
              schema: { $ref: "#/components/schemas/TimeOffCoverage" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/tags:
    get:
      operationId: listTags
      tags: [entries]
      summary: List tags
      description: The caller's tags in the org window, ordered by name ascending.
      responses:
        "200":
          description: Tags
          content:
            application/json:
              schema:
                type: object
                required: [tags]
                properties:
                  tags:
                    type: array
                    items: { $ref: "#/components/schemas/Tag" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: createTag
      tags: [entries]
      summary: Create a tag
      description: >-
        Creates a tag. Names are unique per caller (case-insensitive); color
        defaults to "#5b4a7d".
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name]
              properties:
                name: { type: string }
                color: { type: string }
      responses:
        "201":
          description: Tag created
          content:
            application/json:
              schema:
                type: object
                required: [tag]
                properties:
                  tag: { $ref: "#/components/schemas/Tag" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/tags/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    patch:
      operationId: updateTag
      tags: [entries]
      summary: Rename or recolor a tag
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                color: { type: string }
      responses:
        "200":
          description: Updated tag
          content:
            application/json:
              schema:
                type: object
                required: [tag]
                properties:
                  tag: { $ref: "#/components/schemas/Tag" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }
    delete:
      operationId: deleteTag
      tags: [entries]
      summary: Delete a tag
      description: Removes the tag and detaches it from every entry.
      responses:
        "200":
          description: Deleted
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/preferences:
    get:
      operationId: getPreferences
      tags: [reports]
      summary: Display preferences
      description: >-
        The caller's per-user display/tracking preferences. Defaults when never
        configured: weekStart monday, roundingMinutes 0, currency USD.
      responses:
        "200":
          description: Preferences
          content:
            application/json:
              schema:
                type: object
                required: [preferences]
                properties:
                  preferences: { $ref: "#/components/schemas/UserPreferences" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    put:
      operationId: updatePreferences
      tags: [reports]
      summary: Update display preferences
      description: >-
        Partial upsert. weekStart is monday or sunday; roundingMinutes one of
        0, 5, 6, 10, 15, 30 (display-level rounding); currency a 3-letter ISO
        code. Preferences are per-user, not per-org.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                weekStart: { type: string, enum: [monday, sunday] }
                roundingMinutes: { type: integer, enum: [0, 5, 6, 10, 15, 30] }
                currency: { type: string, minLength: 3, maxLength: 3 }
      responses:
        "200":
          description: Updated preferences
          content:
            application/json:
              schema:
                type: object
                required: [preferences]
                properties:
                  preferences: { $ref: "#/components/schemas/UserPreferences" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/workspace-settings:
    get:
      operationId: getWorkspaceSettings
      tags: [entries]
      summary: Workspace time-entry rules
      description: >-
        Required fields and the time lock for the caller's workspace. Any
        member can read them. Defaults when never configured: no field is
        required, and neither lock is set. A project is still required by the
        time-entry routes when requireProject is false.
      responses:
        "200":
          description: Workspace settings
          content:
            application/json:
              schema:
                type: object
                required: [settings]
                properties:
                  settings: { $ref: "#/components/schemas/WorkspaceSettings" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    put:
      operationId: updateWorkspaceSettings
      tags: [entries]
      summary: Update workspace time-entry rules
      description: >-
        Partial update. Only an owner or admin may change these rules. A
        missing boolean is left as it is. lockOlderThanDays is a whole number
        from 1 to 3650, or null to turn that lock off. lockBefore is a
        YYYY-MM-DD date, or null to turn that lock off. The two locks are
        independent: an entry is locked when either one matches. "Older than
        N days" is a rolling UTC window. "Before a date" locks instants before
        that date at 00:00 UTC, so the date itself stays open. Required fields
        apply to everyone, including an owner or admin. The time lock does not:
        an owner or admin can still create, edit, delete, and stop locked time.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                requireProject: { type: boolean }
                requireDescription: { type: boolean }
                requireTag: { type: boolean }
                requireTask: { type: boolean }
                lockOlderThanDays:
                  type: integer
                  minimum: 1
                  maximum: 3650
                  nullable: true
                lockBefore:
                  type: string
                  format: date
                  nullable: true
                  description: YYYY-MM-DD. Null clears the date lock.
      responses:
        "200":
          description: Updated workspace settings
          content:
            application/json:
              schema:
                type: object
                required: [settings]
                properties:
                  settings: { $ref: "#/components/schemas/WorkspaceSettings" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/Forbidden" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/monitoring/settings:
    get:
      operationId: getMonitoringSettings
      tags: [monitoring]
      summary: Monitoring settings
      description: The caller's capture settings. Defaults to everything disabled with a 300s interval when never configured.
      responses:
        "200":
          description: Settings
          content:
            application/json:
              schema:
                type: object
                required: [settings]
                properties:
                  settings: { $ref: "#/components/schemas/MonitoringSettings" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    put:
      operationId: updateMonitoringSettings
      tags: [monitoring]
      summary: Update monitoring settings
      description: >-
        Partial upsert. screenshotIntervalSeconds must be 120-1800. consentAt is
        stamped the first time screenshots, activity, or auto tracker is enabled.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                screenshotsEnabled: { type: boolean }
                screenshotIntervalSeconds: { type: integer, minimum: 120, maximum: 1800 }
                activityEnabled: { type: boolean }
                autoTrackEnabled:
                  type: boolean
                  description: Record app and site spans while no timer is running. Off until the caller turns it on.
      responses:
        "200":
          description: Updated settings
          content:
            application/json:
              schema:
                type: object
                required: [settings]
                properties:
                  settings: { $ref: "#/components/schemas/MonitoringSettings" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/activity/events:
    get:
      operationId: listActivityEvents
      tags: [monitoring]
      summary: List activity events
      description: Events ordered started_at descending, hard limit 2000. from/to filter on started_at.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date-time } }
        - { name: to, in: query, schema: { type: string, format: date-time } }
      responses:
        "200":
          description: Events (max 2000)
          content:
            application/json:
              schema:
                type: object
                required: [events]
                properties:
                  events:
                    type: array
                    maxItems: 2000
                    items: { $ref: "#/components/schemas/ActivityEvent" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: createActivityEvents
      tags: [monitoring]
      summary: Record activity events
      description: >-
        Batch insert (max 500 per call) from the desktop app. A span with an
        entryId requires activityEnabled. A span without one is an auto-tracker
        suggestion and requires autoTrackEnabled. entryId, when given, must
        belong to the caller. appName is truncated to 200 chars, windowTitle to 500.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [events]
              properties:
                events:
                  type: array
                  minItems: 1
                  maxItems: 500
                  items:
                    type: object
                    required: [startedAt, endedAt]
                    properties:
                      appName: { type: string }
                      windowTitle: { type: string }
                      startedAt: { type: string, format: date-time }
                      endedAt: { type: string, format: date-time }
                      entryId: { type: string, nullable: true }
      responses:
        "201":
          description: Events stored
          content:
            application/json:
              schema:
                type: object
                required: [created]
                properties:
                  created: { type: integer }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/activity/suggestions:
    post:
      operationId: convertActivitySuggestion
      tags: [monitoring]
      summary: Log a suggested span
      description: >-
        Turns one continuous run of the same app into a time entry on a project
        the caller can read. The spans must belong to the caller, must not
        already be logged, and must not contain a hole longer than 2 minutes.
        The entry is never billable; the caller can mark it billable afterwards.
        The spans are then linked to the entry so they leave the suggestion
        timeline. 409 when the spans were already logged, or the time falls in
        a submitted or approved week.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [eventIds, projectId]
              properties:
                eventIds:
                  type: array
                  minItems: 1
                  maxItems: 500
                  items: { type: string }
                projectId: { type: string }
                description:
                  type: string
                  description: Defaults to the app name. Truncated to 500 characters.
      responses:
        "201":
          description: Entry created from the spans
          content:
            application/json:
              schema:
                type: object
                required: [entry]
                properties:
                  entry: { $ref: "#/components/schemas/TimeEntry" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "409":
          description: The spans were already logged, or the time falls in a submitted or approved week
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/activity/clicks:
    get:
      operationId: listClickCounts
      tags: [monitoring]
      summary: List mouse-click counts
      description: Counts ordered ended_at descending, hard limit 2000. from/to filter on ended_at.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date-time } }
        - { name: to, in: query, schema: { type: string, format: date-time } }
      responses:
        "200":
          description: Click counts (max 2000)
          content:
            application/json:
              schema:
                type: object
                required: [clickCounts]
                properties:
                  clickCounts:
                    type: array
                    maxItems: 2000
                    items: { $ref: "#/components/schemas/ClickCount" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: createClickCount
      tags: [monitoring]
      summary: Record mouse clicks
      description: >-
        One span of mouse clicks from the desktop tracker. Rejected with 403
        unless screenshots or activity recording is enabled. entryId, when
        given, must belong to the caller.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [clicks, startedAt, endedAt]
              properties:
                clicks: { type: integer, minimum: 0 }
                startedAt: { type: string, format: date-time }
                endedAt: { type: string, format: date-time }
                entryId: { type: string, nullable: true }
      responses:
        "201":
          description: Clicks stored
          content:
            application/json:
              schema:
                type: object
                required: [clickCount]
                properties:
                  clickCount: { $ref: "#/components/schemas/ClickCount" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/activity/minutes:
    get:
      operationId: listActivityMinutes
      tags: [monitoring]
      summary: List per-minute activity counts
      description: >-
        The caller's own minute buckets, ordered minute_at descending, hard
        limit 2000. Counts only — never key identities or click targets.
        from/to filter on minute_at.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date-time } }
        - { name: to, in: query, schema: { type: string, format: date-time } }
      responses:
        "200":
          description: Minute counts (max 2000)
          content:
            application/json:
              schema:
                type: object
                required: [minutes]
                properties:
                  minutes:
                    type: array
                    maxItems: 2000
                    items: { $ref: "#/components/schemas/ActivityMinute" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: createActivityMinutes
      tags: [monitoring]
      summary: Record per-minute activity counts
      description: >-
        Batch upsert (max 500) from the desktop app. A repeated minute replaces
        the counts. Rejected with 403 unless screenshots or activity recording
        is enabled. entryId, when given, must belong to the caller. The payload
        is counts only.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [minutes]
              properties:
                minutes:
                  type: array
                  minItems: 1
                  maxItems: 500
                  items:
                    type: object
                    required: [minuteAt]
                    properties:
                      minuteAt: { type: string, format: date-time }
                      keystrokes: { type: integer, minimum: 0 }
                      clicks: { type: integer, minimum: 0 }
                      scrolls: { type: integer, minimum: 0 }
                      discarded: { type: boolean }
                      entryId: { type: string, nullable: true }
      responses:
        "201":
          description: Minutes stored
          content:
            application/json:
              schema:
                type: object
                required: [saved]
                properties:
                  saved: { type: integer }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }

  /v1/screenshots:
    get:
      operationId: listScreenshots
      tags: [monitoring]
      summary: List screenshots
      description: >-
        Screenshot records ordered captured_at descending, hard limit 500.
        from/to filter on captured_at. url is a presigned GET link valid ~10
        minutes, or null when storage is not configured.
      parameters:
        - { name: from, in: query, schema: { type: string, format: date-time } }
        - { name: to, in: query, schema: { type: string, format: date-time } }
      responses:
        "200":
          description: Screenshots (max 500)
          content:
            application/json:
              schema:
                type: object
                required: [screenshots]
                properties:
                  screenshots:
                    type: array
                    maxItems: 500
                    items: { $ref: "#/components/schemas/Screenshot" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
    post:
      operationId: uploadScreenshot
      tags: [monitoring]
      summary: Upload a screenshot
      description: >-
        Multipart upload from the desktop app. Rejected with 403 unless the
        caller has screenshotsEnabled, and 503 when R2 storage is not
        configured. file must be image/jpeg, image/png, or image/webp, at most
        5 MB. capturedAt defaults to now; entryId must belong to the caller.
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file: { type: string, format: binary }
                capturedAt: { type: string, format: date-time }
                entryId: { type: string }
                clickCount:
                  type: integer
                  minimum: 0
                  description: Mouse clicks since the previous screenshot. Omitted when the client did not count.
      responses:
        "201":
          description: Screenshot stored
          content:
            application/json:
              schema:
                type: object
                required: [screenshot]
                properties:
                  screenshot: { $ref: "#/components/schemas/Screenshot" }
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "500": { $ref: "#/components/responses/ServerError" }
        "503":
          description: Screenshot storage not configured
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }

  /v1/screenshots/{id}:
    parameters:
      - { name: id, in: path, required: true, schema: { type: string } }
    delete:
      operationId: deleteScreenshot
      tags: [monitoring]
      summary: Delete a screenshot
      description: >-
        Owner-scoped delete of the record plus a best-effort delete of the
        stored object. Users can always delete their own captures.
      responses:
        "200":
          description: Deleted
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403": { $ref: "#/components/responses/ToolDisabled" }
        "404": { $ref: "#/components/responses/NotFound" }
        "500": { $ref: "#/components/responses/ServerError" }

components:
  securitySchemes:
    bearer:
      type: http
      scheme: bearer
      bearerFormat: JWT
  schemas:
    Client:
      type: object
      required: [id, name, color, created, visibility, ownerId]
      properties:
        id: { type: string }
        name: { type: string }
        color: { type: string }
        created: { type: string, format: date-time }
        visibility:
          type: string
          description: private = owner only · org = readable by everyone in the organization
        ownerId: { type: string }
        crmCompanyId:
          type: string
          nullable: true
          description: >-
            Optional link to the canonical CRM company (easerix://crm/company/<id>) —
            link, don't merge (docs/crm/RESEARCH.md §3a). Opaque here; CRM enforces
            access at resolve time.
        logoUrl:
          type: string
          nullable: true
          description: Data URL of the company logo or client photo, or null when none is set.
    Project:
      type: object
      required: [id, name, color, hourlyRate, billable, created, visibility, ownerId, isFavorite]
      properties:
        id: { type: string }
        name: { type: string }
        color: { type: string }
        hourlyRate: { type: number }
        billable: { type: boolean }
        clientId: { type: string, nullable: true }
        clientName:
          type: string
          nullable: true
          description: Null when the client is private to another member of the organization
        created: { type: string, format: date-time }
        visibility:
          type: string
          description: >-
            private = owner only · org = readable by everyone in the
            organization, who may log time against it but not edit it
        ownerId: { type: string }
        budgetSeconds:
          type: integer
          nullable: true
          description: Optional all-time hours budget (null = no budget)
        isFavorite:
          type: boolean
          description: Starred by the caller. Favorites sort first, most recently starred at the front.
    ProjectTask:
      type: object
      required: [id, projectId, name, billable, hourlyRate, archived, created]
      properties:
        id: { type: string }
        projectId: { type: string }
        name: { type: string }
        billable:
          type: boolean
          nullable: true
          description: Null inherits the project's billable flag
        hourlyRate:
          type: number
          nullable: true
          description: Null inherits the project's hourly rate
        archived: { type: boolean }
        created: { type: string, format: date-time }
    TimeEntry:
      type: object
      required: [id, projectId, description, startedAt, durationSeconds, isRunning, isBillable, billingStatus, source]
      properties:
        id: { type: string }
        projectId: { type: string }
        projectName: { type: string, nullable: true }
        projectColor: { type: string, nullable: true }
        clientName: { type: string, nullable: true }
        taskId:
          type: string
          nullable: true
          description: Null when the entry was not logged against a task
        taskName:
          type: string
          nullable: true
          description: Null when taskId is null, or the task row is gone
        description: { type: string }
        startedAt: { type: string, format: date-time }
        endedAt: { type: string, format: date-time, nullable: true }
        updatedAt:
          type: string
          format: date-time
          description: Last time the entry itself was changed. Used to mark a diary segment that was edited after it was tracked.
        durationSeconds:
          type: integer
          description: Live-computed while running
        isRunning: { type: boolean }
        isBillable: { type: boolean }
        billingStatus:
          type: string
          enum: [unbilled, invoiced, paid]
          description: unbilled until an owner or admin marks the window invoiced, then paid.
        source:
          type: string
          enum: [timer, manual, desktop]
          description: >-
            How the time was recorded. timer is the web timer, manual is time
            typed in, desktop is the desktop tracker. Set when the entry is
            created. A later edit does not change it.
        tags:
          type: array
          description: >-
            Present on list/create/update responses (always an array there);
            timer endpoints omit it.
          items: { $ref: "#/components/schemas/Tag" }
    Tag:
      type: object
      required: [id, name, color, created]
      properties:
        id: { type: string }
        name: { type: string }
        color: { type: string }
        created: { type: string, format: date-time }
    UserPreferences:
      type: object
      required: [weekStart, roundingMinutes, currency]
      properties:
        weekStart: { type: string, enum: [monday, sunday] }
        roundingMinutes: { type: integer }
        currency: { type: string }
    WorkspaceSettings:
      type: object
      required: [requireProject, requireDescription, requireTag, requireTask, lockOlderThanDays, lockBefore]
      properties:
        requireProject:
          type: boolean
          description: When true, a time entry or timer must name a project.
        requireDescription:
          type: boolean
          description: When true, the description must be non-blank.
        requireTag:
          type: boolean
          description: When true, the entry must have at least one tag.
        requireTask:
          type: boolean
          description: When true, the entry must name a task on its project.
        lockOlderThanDays:
          type: integer
          nullable: true
          description: >-
            When set, a member cannot change an entry that started more than
            this many days ago. Null means this lock is off.
        lockBefore:
          type: string
          format: date
          nullable: true
          description: >-
            When set, a member cannot change an entry that started before this
            date (00:00 UTC). The date itself stays open. Null means this lock
            is off.
    MonitoringSettings:
      type: object
      required: [screenshotsEnabled, screenshotIntervalSeconds, activityEnabled, autoTrackEnabled]
      properties:
        screenshotsEnabled: { type: boolean }
        screenshotIntervalSeconds: { type: integer }
        activityEnabled: { type: boolean }
        autoTrackEnabled:
          type: boolean
          description: When true, the desktop app records app and site spans while no timer is running. Off by default. Adding a span is a separate, confirmed step and never creates a billable entry.
        consentAt:
          type: string
          format: date-time
          nullable: true
          description: When the user first enabled any capture channel
    ClickCount:
      type: object
      required: [id, clicks, startedAt, endedAt]
      properties:
        id: { type: string }
        entryId: { type: string, nullable: true }
        clicks: { type: integer }
        startedAt: { type: string, format: date-time }
        endedAt: { type: string, format: date-time }
    ActivityMinute:
      type: object
      required: [id, minuteAt, keystrokes, clicks, scrolls, discarded]
      properties:
        id: { type: string }
        entryId: { type: string, nullable: true }
        minuteAt: { type: string, format: date-time }
        keystrokes: { type: integer }
        clicks: { type: integer }
        scrolls: { type: integer }
        discarded:
          type: boolean
          description: True when the user discarded this minute as idle time.
    ActivityEvent:
      type: object
      required: [id, appName, windowTitle, startedAt, endedAt]
      properties:
        id: { type: string }
        entryId: { type: string, nullable: true }
        appName: { type: string }
        windowTitle: { type: string }
        startedAt: { type: string, format: date-time }
        endedAt: { type: string, format: date-time }
    Screenshot:
      type: object
      required: [id, contentType, sizeBytes, capturedAt]
      properties:
        id: { type: string }
        entryId: { type: string, nullable: true }
        contentType: { type: string }
        sizeBytes: { type: integer }
        capturedAt: { type: string, format: date-time }
        clickCount:
          type: integer
          nullable: true
          description: Mouse clicks in the interval this screenshot stands for. Null when that client did not count.
        url:
          type: string
          nullable: true
          description: Presigned GET URL valid ~10 minutes, null when storage is disabled
    Timesheet:
      type: object
      required: [id, ownerId, weekStart, status, submittedAt, note]
      properties:
        id: { type: string }
        ownerId: { type: string }
        weekStart: { type: string, format: date, description: "YYYY-MM-DD, the first day of the week" }
        status: { type: string, enum: [submitted, approved, rejected] }
        submittedAt: { type: string, format: date-time }
        reviewedBy: { type: string, nullable: true }
        reviewedAt: { type: string, format: date-time, nullable: true }
        note: { type: string }
    TimesheetLine:
      type: object
      required: [id, projectId, startedAt, durationSeconds, isBillable]
      properties:
        id: { type: string }
        projectId: { type: string }
        projectName: { type: string }
        description: { type: string }
        startedAt: { type: string, format: date-time }
        durationSeconds: { type: integer }
        isBillable: { type: boolean }
    TimesheetReview:
      allOf:
        - { $ref: "#/components/schemas/Timesheet" }
        - type: object
          required: [totalSeconds, entries]
          properties:
            totalSeconds: { type: integer }
            entries:
              type: array
              maxItems: 200
              items: { $ref: "#/components/schemas/TimesheetLine" }
    TimesheetActivity:
      type: object
      required: [id, weekStart, projectId, description]
      properties:
        id: { type: string }
        weekStart: { type: string, format: date }
        projectId: { type: string }
        projectName: { type: string }
        taskId: { type: string }
        taskName: { type: string }
        description: { type: string }
    TimesheetTemplateLine:
      type: object
      required: [projectId, description]
      properties:
        projectId: { type: string }
        projectName: { type: string }
        taskId: { type: string }
        taskName: { type: string }
        description: { type: string }
    TimesheetTemplate:
      type: object
      required: [id, name, lines]
      properties:
        id: { type: string }
        name: { type: string }
        lines:
          type: array
          maxItems: 80
          items: { $ref: "#/components/schemas/TimesheetTemplateLine" }
    Expense:
      type: object
      required: [id, spentOn, pricing, amount, type, billable, note, hasReceipt, created]
      properties:
        id: { type: string }
        clientId: { type: string, nullable: true }
        clientName: { type: string, nullable: true }
        projectId: { type: string, nullable: true }
        projectName: { type: string, nullable: true }
        projectColor: { type: string, nullable: true }
        spentOn: { type: string, format: date }
        pricing: { type: string, enum: [amount, unit] }
        amount: { type: number, description: "Total in the workspace currency, rounded to cents" }
        quantity: { type: number, nullable: true, description: Set when pricing is unit }
        unitRate: { type: number, nullable: true, description: Set when pricing is unit }
        type: { type: string }
        billable: { type: boolean }
        note: { type: string }
        hasReceipt: { type: boolean }
        receipt:
          type: object
          nullable: true
          description: Null when no receipt is attached
          required: [contentType, sizeBytes, filename]
          properties:
            contentType: { type: string }
            sizeBytes: { type: integer }
            filename: { type: string }
            url:
              type: string
              nullable: true
              description: Presigned GET URL valid about 10 minutes, null when storage is disabled
        created: { type: string, format: date-time }
    TimeOffPolicy:
      type: object
      required: [id, name, daysPerYear, archived]
      properties:
        id: { type: string }
        name: { type: string }
        daysPerYear: { type: integer, description: Allowance for each calendar year }
        archived: { type: boolean }
    TimeOffHoliday:
      type: object
      required: [id, name, date, repeatsYearly]
      properties:
        id: { type: string }
        name: { type: string }
        date: { type: string, format: date }
        repeatsYearly: { type: boolean }
    TimeOffRequest:
      type: object
      required: [id, ownerId, policyId, policyName, startDate, endDate, dayCount, status, note, reviewNote, createdAt]
      properties:
        id: { type: string }
        ownerId: { type: string }
        policyId: { type: string }
        policyName: { type: string }
        startDate: { type: string, format: date }
        endDate: { type: string, format: date }
        dayCount:
          type: integer
          description: Monday to Friday in the range, excluding holidays
        status: { type: string, enum: [pending, approved, rejected, cancelled] }
        note: { type: string }
        reviewNote: { type: string }
        reviewedBy: { type: string, nullable: true }
        reviewedAt: { type: string, format: date-time, nullable: true }
        createdAt: { type: string, format: date-time }
    TimeOffRequestReview:
      allOf:
        - { $ref: "#/components/schemas/TimeOffRequest" }
        - type: object
          required: [remainingAfter]
          properties:
            remainingAfter:
              type: integer
              description: Tightest days left on the policy if this request is approved. Negative when it no longer fits.
    TimeOffCoverage:
      type: object
      required: [holidays, timeOff, balances]
      properties:
        holidays:
          type: array
          maxItems: 400
          items:
            type: object
            required: [date, name]
            properties:
              date: { type: string, format: date }
              name: { type: string }
        timeOff:
          type: array
          maxItems: 400
          items:
            type: object
            required: [date, policyName, status]
            properties:
              date: { type: string, format: date }
              policyName: { type: string }
              status: { type: string, enum: [pending, approved] }
        balances:
          type: array
          maxItems: 200
          items:
            type: object
            required: [policyId, name, year, daysPerYear, usedDays, pendingDays, remainingDays]
            properties:
              policyId: { type: string }
              name: { type: string }
              year: { type: integer }
              daysPerYear: { type: integer }
              usedDays: { type: integer }
              pendingDays: { type: integer }
              remainingDays: { type: integer }
    Error:
      type: object
      required: [error]
      properties:
        error: { type: string }
  responses:
    BadRequest:
      description: Invalid or incomplete request body
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    Unauthorized:
      description: >-
        Missing/invalid bearer token, or a token without org context
        ("token missing org context — refresh your session")
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    ToolDisabled:
      description: The croncrunch tool is turned off for the caller's organization
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    Forbidden:
      description: >-
        Either the croncrunch tool is turned off for the caller's organization,
        or the client/project is shared with the caller by a teammate — visible,
        but only its owner can change it.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    NotFound:
      description: Resource not found or not owned by the caller
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    WeekLocked:
      description: The time falls in a week that is submitted or approved
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
    ServerError:
      description: Internal error
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
