openapi: 3.0.3
info:
  title: Todo App API
  description: A simple Todo application with CRUD operations and file attachments
  version: 1.0.0

servers:
  - url: http://localhost:5001

paths:
  /health:
    get:
      summary: Health check
      operationId: healthCheck
      responses:
        '200':
          description: API is healthy
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                  timestamp:
                    type: string

  /todos:
    get:
      summary: List all todos
      operationId: listTodos
      parameters:
        - name: page
          in: query
          schema:
            type: integer
            default: 1
        - name: limit
          in: query
          schema:
            type: integer
            default: 10
        - name: completed
          in: query
          schema:
            type: string
            enum: ['true', 'false']
        - name: search
          in: query
          schema:
            type: string
      responses:
        '200':
          description: List of todos
          content:
            application/json:
              schema:
                type: object
                properties:
                  todos:
                    type: array
                    items:
                      $ref: '#/components/schemas/Todo'
                  pagination:
                    $ref: '#/components/schemas/Pagination'

    post:
      summary: Create a new todo
      operationId: createTodo
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TodoCreate'
            example:
              title: "Buy groceries"
              description: "Milk, eggs, bread"
      responses:
        '201':
          description: Todo created
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Todo'
        '422':
          description: Validation error

  /todos/{todoId}:
    get:
      summary: Get a specific todo
      operationId: getTodo
      parameters:
        - name: todoId
          in: path
          required: true
          schema:
            type: integer
      responses:
        '200':
          description: Todo found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Todo'
        '404':
          description: Todo not found

    put:
      summary: Update a todo
      operationId: updateTodo
      parameters:
        - name: todoId
          in: path
          required: true
          schema:
            type: integer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TodoUpdate'
      responses:
        '200':
          description: Todo updated
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Todo'
        '404':
          description: Todo not found

    delete:
      summary: Delete a todo
      operationId: deleteTodo
      parameters:
        - name: todoId
          in: path
          required: true
          schema:
            type: integer
      responses:
        '204':
          description: Todo deleted
        '404':
          description: Todo not found

  /todos/{todoId}/attachments:
    post:
      summary: Upload an attachment
      operationId: uploadAttachment
      parameters:
        - name: todoId
          in: path
          required: true
          schema:
            type: integer
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
      responses:
        '201':
          description: Attachment uploaded
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Attachment'
        '404':
          description: Todo not found

  /todos/{todoId}/attachments/{fileId}:
    get:
      summary: Download an attachment
      operationId: downloadAttachment
      parameters:
        - name: todoId
          in: path
          required: true
          schema:
            type: integer
        - name: fileId
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: File content
        '404':
          description: Attachment not found

    delete:
      summary: Delete an attachment
      operationId: deleteAttachment
      parameters:
        - name: todoId
          in: path
          required: true
          schema:
            type: integer
        - name: fileId
          in: path
          required: true
          schema:
            type: string
      responses:
        '204':
          description: Attachment deleted
        '404':
          description: Attachment not found

components:
  schemas:
    Todo:
      type: object
      properties:
        id:
          type: integer
        title:
          type: string
        description:
          type: string
        completed:
          type: boolean
        created_at:
          type: string
          format: date-time
        attachments:
          type: array
          items:
            $ref: '#/components/schemas/Attachment'

    TodoCreate:
      type: object
      required:
        - title
      properties:
        title:
          type: string
          minLength: 1
          maxLength: 200
        description:
          type: string
        completed:
          type: boolean
          default: false

    TodoUpdate:
      type: object
      properties:
        title:
          type: string
          minLength: 1
          maxLength: 200
        description:
          type: string
        completed:
          type: boolean

    Attachment:
      type: object
      properties:
        id:
          type: string
        filename:
          type: string
        size:
          type: integer
        content_type:
          type: string
        todo_id:
          type: integer

    Pagination:
      type: object
      properties:
        page:
          type: integer
        limit:
          type: integer
        total:
          type: integer
        pages:
          type: integer
