openapi: "3.0.3"
info:
  title: Firma Todo API
  version: "1.0.0"
  description: API REST para integrar firma electrónica en tu sistema.
  contact:
    email: contacto@firmatodo.cl
  license:
    name: MIT
servers:
  - url: https://firmatodo.cl
    description: Producción

security:
  - cookieAuth: []

components:
  securitySchemes:
    cookieAuth:
      type: apiKey
      in: cookie
      name: session
  schemas:
    EnrolledSigner:
      type: object
      properties:
        id: { type: integer }
        external_id: { type: string, example: signer_abc123 }
        full_name: { type: string, example: Pedro González }
        rut: { type: string, example: 12345678-9 }
        email: { type: string, format: email }
        status:
          type: string
          enum: [pending, enrolled, inactive]
        enrolled_at: { type: string, format: date-time }
    Document:
      type: object
      properties:
        id: { type: integer }
        title: { type: string }
        status: { type: string, enum: [pending, completed, cancelled] }
        source: { type: string, enum: [manual, api] }
        created_at: { type: string, format: date-time }
        signers_count: { type: integer }
        signed_count: { type: integer }

paths:
  /api/auth/login:
    post:
      summary: Iniciar sesión
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [email, password]
              properties:
                email: { type: string, format: email }
                password: { type: string }
      responses:
        "200":
          description: Sesión iniciada
          content:
            application/json:
              schema:
                type: object
                properties:
                  user:
                    type: object
                    properties:
                      id: { type: integer }
                      name: { type: string }
                      email: { type: string }
                      role: { type: string }

  /api/enrolled-signers:
    get:
      summary: Listar firmantes enrolados
      responses:
        "200":
          description: Lista de firmantes
          content:
            application/json:
              schema:
                type: object
                properties:
                  signers:
                    type: array
                    items:
                      $ref: "#/components/schemas/EnrolledSigner"
    post:
      summary: Crear firmante enrolado
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [full_name, rut, email]
              properties:
                full_name: { type: string }
                rut: { type: string }
                email: { type: string, format: email }
      responses:
        "200":
          description: Firmante creado con link de enrolamiento
          content:
            application/json:
              schema:
                type: object
                properties:
                  id: { type: integer }
                  external_id: { type: string }
                  enrollment_url: { type: string }
                  status: { type: string }

  /api/enrolled-signers/{id}:
    delete:
      summary: Eliminar firmante enrolado
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: integer }
      responses:
        "200":
          description: Firmante eliminado
          content:
            application/json:
              schema:
                type: object
                properties:
                  ok: { type: boolean }

  /api/firmaya/sign:
    post:
      summary: Firmar documento automáticamente
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [document_id, signer_id]
              properties:
                document_id: { type: string }
                signer_id: { type: string, description: external_id del firmante enrolado }
      responses:
        "200":
          description: Documento firmado
          content:
            application/json:
              schema:
                type: object
                properties:
                  ok: { type: boolean }
                  document_id: { type: string }
                  signed_at: { type: string, format: date-time }
                  remaining_credits: { type: integer }

  /api/firmaya/create-group:
    post:
      summary: Crear grupo de firma
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [document_id, signers]
              properties:
                document_id: { type: string }
                signers:
                  type: array
                  items:
                    type: object
                    properties:
                      signer_id: { type: string }
                      email: { type: string }
                      name: { type: string }
                      type:
                        type: string
                        enum: [enrolled, external]
                callback_url: { type: string, format: uri }
      responses:
        "200":
          description: Grupo creado
          content:
            application/json:
              schema:
                type: object
                properties:
                  group_id: { type: string }
                  status: { type: string }
                  signers_count: { type: integer }

  /api/files:
    get:
      summary: Listar documentos del usuario
      responses:
        "200":
          description: Lista de documentos
          content:
            application/json:
              schema:
                type: object
                properties:
                  documents:
                    type: array
                    items:
                      $ref: "#/components/schemas/Document"
