openapi: 3.0.3
info:
  title: TaskFlow API
  description: TaskFlow REST API. All IDs are UUIDs. Dates are ISO 8601. Rate limit 60 req/min per user.
  version: 1.0.0

servers:
  - url: /api/v1
    description: API v1

security:
  - BearerAuth: []

paths:
  /auth/login:
    post:
      summary: Authenticate
      description: Returns Bearer token and user.
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [email, password, device_name]
              properties:
                email: { type: string, format: email }
                password: { type: string }
                device_name: { type: string }
      responses:
        '201':
          description: Success
          content:
            application/json:
              schema:
                type: object
                properties:
                  token: { type: string }
                  user: { $ref: '#/components/schemas/User' }
        '422':
          $ref: '#/components/responses/ValidationError'

  /auth/logout:
    post:
      summary: Logout
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
        '401':
          $ref: '#/components/responses/Unauthorized'

  /auth/me:
    get:
      summary: Current user
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  user: { $ref: '#/components/schemas/User' }
        '401':
          $ref: '#/components/responses/Unauthorized'

  /users:
    get:
      summary: List users (admin/owner)
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array, items: { $ref: '#/components/schemas/User' } }
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'

  /users/{user}:
    get:
      summary: Show user
      parameters:
        - $ref: '#/components/parameters/UuidUser'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/User' }
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
    patch:
      summary: Update user
      parameters:
        - $ref: '#/components/parameters/UuidUser'
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                role: { type: string, enum: [owner, admin, member] }
                supervisor_id: { type: string, format: uuid, nullable: true }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/User' }
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '422':
          $ref: '#/components/responses/ValidationError'

  /projects:
    get:
      summary: List projects
      parameters:
        - name: archived
          in: query
          schema: { type: boolean }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array, items: { $ref: '#/components/schemas/Project' } }
        '401':
          $ref: '#/components/responses/Unauthorized'
    post:
      summary: Create project
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name, visibility]
              properties:
                name: { type: string }
                description: { type: string, nullable: true }
                color: { type: string, nullable: true }
                visibility: { type: string, enum: [private, shared] }
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Project' }
        '401':
          $ref: '#/components/responses/Unauthorized'
        '422':
          $ref: '#/components/responses/ValidationError'

  /projects/{project}:
    get:
      summary: Show project
      parameters:
        - $ref: '#/components/parameters/UuidProject'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Project' }
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
    patch:
      summary: Update project
      parameters:
        - $ref: '#/components/parameters/UuidProject'
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                description: { type: string, nullable: true }
                color: { type: string, nullable: true }
                visibility: { type: string, enum: [private, shared] }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Project' }
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '422':
          $ref: '#/components/responses/ValidationError'
    delete:
      summary: Delete project
      parameters:
        - $ref: '#/components/parameters/UuidProject'
      responses:
        '204':
          description: No content
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '422':
          description: Default project cannot be deleted

  /tasks:
    get:
      summary: List tasks
      parameters:
        - name: project_id
          in: query
          schema: { type: string, format: uuid }
        - name: list_id
          in: query
          schema: { type: string, format: uuid }
        - name: status
          in: query
          schema: { type: string, enum: [todo, in_progress, review, done] }
        - name: assignee
          in: query
          schema: { type: string, format: uuid }
        - name: per_page
          in: query
          schema: { type: integer, default: 20 }
        - name: cursor
          in: query
          schema: { type: string }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array, items: { $ref: '#/components/schemas/Task' } }
                  meta:
                    type: object
                    properties:
                      pagination:
                        type: object
                        properties:
                          per_page: { type: integer }
                          next_cursor: { type: string, nullable: true }
                          prev_cursor: { type: string, nullable: true }
                          has_more_pages: { type: boolean }
        '401':
          $ref: '#/components/responses/Unauthorized'
    post:
      summary: Create task
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [task_list_id, title]
              properties:
                task_list_id: { type: string, format: uuid }
                title: { type: string }
                description: { type: string, nullable: true }
                status: { type: string, enum: [todo, in_progress, review, done], default: todo }
                priority: { type: string, enum: [low, medium, high, urgent], default: medium }
                due_date: { type: string, format: date, nullable: true }
                due_time: { type: string, nullable: true }
                is_private: { type: boolean, default: true }
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Task' }
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '422':
          $ref: '#/components/responses/ValidationError'

  /tasks/{task}:
    get:
      summary: Show task
      parameters:
        - name: task
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Task' }
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
    patch:
      summary: Update task
      parameters:
        - name: task
          in: path
          required: true
          schema: { type: string, format: uuid }
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                title: { type: string }
                description: { type: string, nullable: true }
                status: { type: string, enum: [todo, in_progress, review, done] }
                priority: { type: string, enum: [low, medium, high, urgent] }
                due_date: { type: string, format: date, nullable: true }
                due_time: { type: string, nullable: true }
                task_list_id: { type: string, format: uuid }
                tag_ids: { type: array, items: { type: string, format: uuid } }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Task' }
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '422':
          $ref: '#/components/responses/ValidationError'
    delete:
      summary: Delete task
      parameters:
        - name: task
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '204':
          description: No content
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'

  /webhooks:
    get:
      summary: List webhooks (admin/owner)
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array, items: { $ref: '#/components/schemas/Webhook' } }
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
    post:
      summary: Create webhook
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name, url, events]
              properties:
                name: { type: string }
                url: { type: string, format: uri }
                events: { type: array, items: { type: string }, minItems: 1 }
      responses:
        '201':
          description: Created (includes secret once)
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Webhook' }
                  secret: { type: string }
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '422':
          $ref: '#/components/responses/ValidationError'

  /webhooks/{webhook}:
    get:
      summary: Show webhook
      parameters:
        - name: webhook
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Webhook' }
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
    patch:
      summary: Update webhook
      parameters:
        - name: webhook
          in: path
          required: true
          schema: { type: string, format: uuid }
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                url: { type: string, format: uri }
                events: { type: array, items: { type: string } }
                is_active: { type: boolean }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: '#/components/schemas/Webhook' }
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'
        '422':
          $ref: '#/components/responses/ValidationError'
    delete:
      summary: Delete webhook
      parameters:
        - name: webhook
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '204':
          description: No content
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'

  /webhooks/{webhook}/deliveries:
    get:
      summary: List webhook deliveries (last 50)
      parameters:
        - name: webhook
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { type: array }
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'

components:
  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

  parameters:
    UuidUser:
      name: user
      in: path
      required: true
      schema: { type: string, format: uuid }
    UuidProject:
      name: project
      in: path
      required: true
      schema: { type: string, format: uuid }

  responses:
    Unauthorized:
      description: Unauthenticated
      content:
        application/json:
          schema:
            type: object
            properties:
              message: { type: string, example: Unauthenticated. }
    Forbidden:
      description: Forbidden
      content:
        application/json:
          schema:
            type: object
            properties:
              message: { type: string, example: Forbidden. }
    NotFound:
      description: Not found
      content:
        application/json:
          schema:
            type: object
            properties:
              message: { type: string, example: Not found. }
    ValidationError:
      description: Validation error
      content:
        application/json:
          schema:
            type: object
            properties:
              message: { type: string }
              errors: { type: object, additionalProperties: { type: array, items: { type: string } } }

  schemas:
    User:
      type: object
      properties:
        id: { type: string, format: uuid }
        name: { type: string }
        email: { type: string, format: email }
        role: { type: string }
        status: { type: string }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    Project:
      type: object
      properties:
        id: { type: string, format: uuid }
        name: { type: string }
        description: { type: string, nullable: true }
        visibility: { type: string, enum: [private, shared] }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    Task:
      type: object
      properties:
        id: { type: string, format: uuid }
        title: { type: string }
        status: { type: string }
        priority: { type: string }
        due_date: { type: string, format: date, nullable: true }
        project_id: { type: string, format: uuid }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
    Webhook:
      type: object
      properties:
        id: { type: string, format: uuid }
        name: { type: string }
        url: { type: string }
        events: { type: array, items: { type: string } }
        is_active: { type: boolean }
        last_triggered_at: { type: string, format: date-time, nullable: true }
        last_status: { type: string, nullable: true }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }
