openapi: 3.1.0
info:
  title: Sofi2 API
  description: API genérica para operaciones CRUD sobre cualquier tabla
  version: 1.0.0
servers:
  - url: https://sofi2.onecloud.cl/api.php
paths:
  /{table}:
    parameters:
      - in: path
        name: table
        required: true
        schema:
          type: string
        description: Nombre de la tabla sobre la que operar
    get:
      summary: Leer registros
      description: Obtiene una lista de registros, con filtrado opcional por query-string
      operationId: readRecords
      parameters:
        - in: query
          name: id
          schema:
            type: integer
          description: Filtrar por ID
        - in: query
          name: sampleString
          schema:
            type: string
          description: Filtrar por campo sampleString
        - in: query
          name: sampleNumber
          schema:
            type: integer
          description: Filtrar por campo sampleNumber
        - in: query
          name: sampleDate
          schema:
            type: string
            format: date-time
          description: Filtrar por campo sampleDate
      responses:
        '200':
          description: Lista de registros
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ReadResponse'
    post:
      summary: Insertar, actualizar o eliminar registros
      description: |
        Operación genérica que determina la acción según el contenido del body:
        - Si el objeto contiene solo `id` → DELETE
        - Si el objeto contiene `id` y otros campos → UPDATE
        - Si el objeto NO contiene `id` → INSERT
      operationId: writeRecords
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: array
              items:
                oneOf:
                  - $ref: '#/components/schemas/InsertRecord'
                  - $ref: '#/components/schemas/UpdateRecord'
                  - $ref: '#/components/schemas/DeleteRecord'
      responses:
        '200':
          description: Resultado de las operaciones
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/WriteResponse'

components:
  schemas:
    InsertRecord:
      type: object
      properties:
        sampleString:
          type: string
        sampleNumber:
          type: integer
        sampleDate:
          type: string
          format: date-time
      required: []
      additionalProperties: true
      example:
        sampleString: "Primer Dato"
        sampleNumber: 123
        sampleDate: "2024-05-20T10:30:00Z"

    UpdateRecord:
      type: object
      properties:
        id:
          type: integer
        sampleString:
          type: string
        sampleNumber:
          type: integer
        sampleDate:
          type: string
          format: date-time
      required:
        - id
      additionalProperties: true
      example:
        id: 1
        sampleString: "Dato Actualizado"

    DeleteRecord:
      type: object
      properties:
        id:
          type: integer
      required:
        - id
      additionalProperties: false
      example:
        id: 2

    ReadResponse:
      type: object
      properties:
        success:
          type: boolean
        data:
          type: array
          items:
            type: object
            additionalProperties: true
        count:
          type: integer
      required:
        - success
        - data
        - count
      example:
        success: true
        data:
          - id: "1"
            sampleString: "Primer Dato"
            sampleNumber: "123"
            sampleDate: "2024-05-20 10:30:00"
        count: 1

    WriteResponse:
      type: object
      properties:
        success:
          type: boolean
        results:
          type: array
          items:
            type: object
            properties:
              success:
                type: boolean
              operation:
                type: string
                enum: [insert, update, delete]
              insert_id:
                type: integer
              affected_rows:
                type: integer
            required:
              - success
              - operation
              - affected_rows
      required:
        - success
        - results
      example:
        success: true
        results:
          - success: true
            operation: insert
            insert_id: 1
            affected_rows: 1
