openapi: 3.0.3
info:
  title: Insforge Tables API
  version: 1.0.0
paths:
  /api/database/tables:
    get:
      summary: List Tables
      tags:
        - Admin
      security:
        - bearerAuth: []
        - apiKey: []
      responses:
        '200':
          description: List of table names
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
              example:
                - users
                - posts
                - comments
                - categories
    post:
      summary: Create Table
      tags:
        - Admin
      security:
        - bearerAuth: []
        - apiKey: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - tableName
                - columns
              properties:
                tableName:
                  type: string
                columns:
                  type: array
                  items:
                    type: object
                    required:
                      - name
                      - type
                      - nullable
                    properties:
                      name:
                        type: string
                      type:
                        type: string
                        enum:
                          - string
                          - datetime
                          - integer
                          - float
                          - boolean
                          - uuid
                          - json
                          - file
                      nullable:
                        type: boolean
                      unique:
                        type: boolean
                      defaultValue:
                        type: string
                      foreignKey:
                        type: object
                        properties:
                          table:
                            type: string
                          column:
                            type: string
                          onDelete:
                            type: string
                            enum:
                              - CASCADE
                              - SET NULL
                              - NO ACTION
                              - RESTRICT
                            default: NO ACTION
      responses:
        '201':
          description: Table created
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                  table_name:
                    type: string
              example:
                message: Table created successfully
                tableName: posts
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: VALIDATION_ERROR
                message: Table name already exists
                statusCode: 400
                nextActions: Choose a different table name
        '422':
          description: Unprocessable entity
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: INVALID_FIELD_TYPE
                message: >-
                  Invalid field type: 'text'. Valid types are: string, integer,
                  float, boolean, datetime, uuid, json, file
                statusCode: 422
                nextActions: Use one of the valid field types
  /api/database/tables/{tableName}:
    get:
      summary: Get Table Schema
      tags:
        - Admin
      security:
        - bearerAuth: []
        - apiKey: []
      parameters:
        - name: tableName
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Table schema
          content:
            application/json:
              schema:
                type: object
                properties:
                  table_name:
                    type: string
                  columns:
                    type: array
                    items:
                      type: object
                      properties:
                        name:
                          type: string
                        type:
                          type: string
                        nullable:
                          type: boolean
                        unique:
                          type: boolean
                        default:
                          type: string
                          nullable: true
                        isPrimaryKey:
                          type: boolean
                        foreignKey:
                          type: object
                          nullable: true
                          properties:
                            table:
                              type: string
                            column:
                              type: string
                            on_delete:
                              type: string
              example:
                tableName: posts
                columns:
                  - name: id
                    type: uuid
                    nullable: false
                    unique: true
                    default: gen_random_uuid()
                    isPrimaryKey: true
                    foreignKey: null
                  - name: title
                    type: string
                    nullable: false
                    unique: false
                    default: null
                    isPrimaryKey: false
                    foreignKey: null
                  - name: userId
                    type: uuid
                    nullable: false
                    unique: false
                    default: null
                    isPrimaryKey: false
                    foreignKey:
                      table: users
                      column: id
                      on_delete: CASCADE
        '404':
          description: Table not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: TABLE_NOT_FOUND
                message: Table 'nonexistent' does not exist
                statusCode: 404
                nextActions: Check table name and try again
    patch:
      summary: Update Table Schema
      tags:
        - Admin
      security:
        - bearerAuth: []
        - apiKey: []
      parameters:
        - name: tableName
          in: path
          required: true
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                addColumns:
                  type: array
                  description: Add new columns to the table
                  items:
                    type: object
                    required:
                      - columnName
                      - type
                    properties:
                      columnName:
                        type: string
                        description: Name of the new column
                      type:
                        type: string
                        enum:
                          - string
                          - integer
                          - float
                          - boolean
                          - datetime
                          - uuid
                          - json
                          - file
                        description: Data type of the column
                      isNullable:
                        type: boolean
                        default: true
                        description: Whether the column allows NULL values
                      isUnique:
                        type: boolean
                        default: false
                        description: Whether the column values must be unique
                      defaultValue:
                        type: string
                        description: Default value for the column
                dropColumns:
                  type: array
                  description: Remove columns from the table
                  items:
                    type: string
                    description: Name of the column to drop
                updateColumns:
                  type: array
                  description: Modify existing columns (rename or change default)
                  items:
                    type: object
                    required:
                      - columnName
                    properties:
                      columnName:
                        type: string
                        description: Current name of the column
                      newColumnName:
                        type: string
                        description: New name for the column (optional)
                        minLength: 1
                        maxLength: 64
                      defaultValue:
                        type: string
                        description: New default value for the column (optional)
                addForeignKeys:
                  type: array
                  description: Add foreign key constraints to existing columns
                  items:
                    type: object
                    required:
                      - columnName
                      - foreignKey
                    properties:
                      columnName:
                        type: string
                        description: Name of the column to add foreign key to
                      foreignKey:
                        type: object
                        required:
                          - referenceTable
                          - referenceColumn
                        properties:
                          referenceTable:
                            type: string
                            description: Table being referenced
                          referenceColumn:
                            type: string
                            description: Column being referenced
                          onDelete:
                            type: string
                            enum:
                              - CASCADE
                              - SET NULL
                              - NO ACTION
                              - RESTRICT
                            default: RESTRICT
                            description: Action on delete
                          onUpdate:
                            type: string
                            enum:
                              - CASCADE
                              - SET NULL
                              - NO ACTION
                              - RESTRICT
                            default: RESTRICT
                            description: Action on update
                dropForeignKeys:
                  type: array
                  description: Remove foreign key constraints from columns
                  items:
                    type: string
                    description: Name of the column with foreign key to drop
                renameTable:
                  type: object
                  description: Rename the table
                  required:
                    - newTableName
                  properties:
                    newTableName:
                      type: string
                      description: New name for the table
                      minLength: 1
                      maxLength: 64
      responses:
        '200':
          description: Table schema updated successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    description: Success message
                  tableName:
                    type: string
                    description: Name of the updated table
                  operations:
                    type: array
                    description: List of operations performed
                    items:
                      type: string
              example:
                message: Table schema updated successfully
                tableName: posts
                operations:
                  - added 2 columns
                  - dropped 1 columns
                  - renamed 1 columns
                  - added 1 foreign keys
                  - dropped 1 foreign keys
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                columnExists:
                  value:
                    error: COLUMN_EXISTS
                    message: Column 'title' already exists in table 'posts'
                    statusCode: 400
                    nextActions: Choose a different column name or drop it first
                columnNotFound:
                  value:
                    error: COLUMN_NOT_FOUND
                    message: Column 'nonexistent' not found in table 'posts'
                    statusCode: 400
                    nextActions: Check column name with GET /api/tables/{tableName}/schema
                foreignKeyExists:
                  value:
                    error: FOREIGN_KEY_EXISTS
                    message: Foreign key on column 'user_id' already exists
                    statusCode: 400
                    nextActions: >-
                      Drop the existing foreign key first or use a different
                      column
        '404':
          description: Table not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: TABLE_NOT_FOUND
                message: Table 'nonexistent' does not exist
                statusCode: 404
                nextActions: Check table name and try again
    delete:
      summary: Delete Table
      tags:
        - Admin
      security:
        - bearerAuth: []
        - apiKey: []
      parameters:
        - name: tableName
          in: path
          required: true
          schema:
            type: string
      responses:
        '200':
          description: Table deleted
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                  table_name:
                    type: string
              example:
                message: Table deleted successfully
                tableName: posts
        '404':
          description: Table not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              example:
                error: TABLE_NOT_FOUND
                message: Table 'nonexistent' does not exist
                statusCode: 404
                nextActions: Check table name and try again
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
    apiKey:
      type: apiKey
      in: header
      name: X-API-Key
  schemas:
    ErrorResponse:
      type: object
      required:
        - error
        - message
        - statusCode
      properties:
        error:
          type: string
          description: Error code for programmatic handling
        message:
          type: string
          description: Human-readable error message
        statusCode:
          type: integer
          description: HTTP status code
        nextActions:
          type: string
          description: Suggested action to resolve the error
