{
  "openapi": "3.1.0",
  "info": {
    "title": "Observatorio PR Posts API",
    "version": "1.0.0",
    "description": "Read published community reports and submit retrospective reports for moderation."
  },
  "servers": [
    { "url": "/" }
  ],
  "paths": {
    "/api/posts": {
      "get": {
        "operationId": "listPosts",
        "summary": "List published reports",
        "description": "Reports appear in descending order by submission time. This endpoint is public.",
        "security": [],
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "description": "Maximum number of reports to return.",
            "schema": { "$ref": "#/components/schemas/Limit" }
          },
          {
            "name": "offset",
            "in": "query",
            "description": "Number of matching reports to skip.",
            "schema": { "$ref": "#/components/schemas/Offset" }
          },
          {
            "name": "municipality",
            "in": "query",
            "description": "Exact name of a Puerto Rico municipality, preserving case and accents.",
            "schema": { "type": "string", "examples": ["San Juan", "Bayamón"] }
          },
          {
            "name": "type",
            "in": "query",
            "description": "Limit results to this report category.",
            "schema": { "$ref": "#/components/schemas/ReportType" }
          }
        ],
        "responses": {
          "200": {
            "description": "A page of published reports and site settings.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PostPage" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "429": { "$ref": "#/components/responses/HighTraffic" },
          "500": { "$ref": "#/components/responses/InternalError" },
          "503": { "$ref": "#/components/responses/ServiceUnavailable" }
        }
      },
      "post": {
        "operationId": "submitPost",
        "summary": "Submit a report for moderation",
        "description": "External integrations send the configured API_WRITE_KEY as a bearer token. The site's own browser form submits from the same origin. Reports remain private until a moderator publishes them.",
        "security": [
          { "writeKey": [] }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ReportFields" },
              "example": {
                "title": "Ejemplo de reporte retrospectivo",
                "description": "Este texto muestra el formato de un reporte para pruebas locales.",
                "municipality": "San Juan",
                "type": "otro",
                "occurredOn": "2025-01-01"
              }
            },
            "multipart/form-data": {
              "schema": { "$ref": "#/components/schemas/ReportWithMedia" },
              "encoding": {
                "media": {
                  "contentType": "image/jpeg, image/png, image/webp, video/mp4, video/webm"
                }
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Report accepted into the private moderation queue.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AcceptedReport" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/InvalidRequest" },
          "401": {
            "description": "The external request lacks a valid write key.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" }
              }
            }
          },
          "413": {
            "description": "The request exceeds the accepted body size. JSON bodies are limited to 16 KiB. Multipart bodies are limited to 60 MiB plus 64 KiB with media uploads enabled, or 64 KiB with uploads disabled.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" }
              }
            }
          },
          "415": {
            "description": "Use application/json or multipart/form-data.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Error" }
              }
            }
          },
          "429": { "$ref": "#/components/responses/HighTraffic" },
          "500": { "$ref": "#/components/responses/InternalError" },
          "503": { "$ref": "#/components/responses/ServiceUnavailable" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "writeKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "A site-specific API_WRITE_KEY shared with authorized server-side integrations."
      }
    },
    "schemas": {
      "Limit": { "type": "integer", "minimum": 1, "maximum": 50, "default": 20 },
      "Offset": { "type": "integer", "minimum": 0, "maximum": 10000, "default": 0 },
      "ReportType": {
        "type": "string",
        "enum": ["operativo", "detencion", "presencia", "otro"]
      },
      "MediaType": {
        "type": "string",
        "enum": ["image/jpeg", "image/png", "image/webp", "video/mp4", "video/webm"]
      },
      "ReportFields": {
        "type": "object",
        "required": ["title", "description", "municipality", "type", "occurredOn"],
        "properties": {
          "title": { "type": "string", "minLength": 8, "maxLength": 140 },
          "description": {
            "type": "string",
            "minLength": 20,
            "maxLength": 2000,
            "description": "Give a municipality-level account and omit personal or precise location details."
          },
          "municipality": {
            "type": "string",
            "description": "Exact name of a Puerto Rico municipality, preserving case and accents.",
            "examples": ["San Juan", "Bayamón"]
          },
          "type": { "$ref": "#/components/schemas/ReportType" },
          "occurredOn": {
            "type": "string",
            "format": "date",
            "description": "The event date in YYYY-MM-DD format. It must precede today's date in Puerto Rico."
          }
        }
      },
      "ReportWithMedia": {
        "allOf": [
          { "$ref": "#/components/schemas/ReportFields" },
          {
            "type": "object",
            "properties": {
              "media": {
                "type": "array",
                "maxItems": 3,
                "description": "Send one part named media per file. JPEG, PNG, WebP, MP4, and WebM are supported. Each file must contain at least 8 bytes and may be up to 20 MiB. Uploads require mediaUploadsEnabled to be true.",
                "items": { "type": "string", "format": "binary" }
              }
            }
          }
        ]
      },
      "Media": {
        "type": "object",
        "required": ["url", "type", "name"],
        "properties": {
          "url": {
            "type": "string",
            "format": "uri",
            "description": "Signed media URL valid for 15 minutes. Fetch the report list again for a fresh link."
          },
          "type": { "$ref": "#/components/schemas/MediaType" },
          "name": { "type": "string", "description": "Display label generated by the site." }
        }
      },
      "Post": {
        "allOf": [
          { "$ref": "#/components/schemas/ReportFields" },
          {
            "type": "object",
            "required": ["id", "createdAt", "media"],
            "properties": {
              "id": { "type": "string", "format": "uuid" },
              "createdAt": { "type": "string", "format": "date-time" },
              "media": {
                "type": "array",
                "items": { "$ref": "#/components/schemas/Media" }
              }
            }
          }
        ]
      },
      "PostPage": {
        "type": "object",
        "required": ["posts", "limit", "offset", "hasMore", "demo", "mediaUploadsEnabled"],
        "properties": {
          "posts": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/Post" }
          },
          "limit": { "$ref": "#/components/schemas/Limit" },
          "offset": { "$ref": "#/components/schemas/Offset" },
          "hasMore": { "type": "boolean" },
          "demo": { "type": "boolean", "description": "The site is running in local development mode." },
          "mediaUploadsEnabled": { "type": "boolean", "description": "The server has Supabase configured and accepts attachments." }
        }
      },
      "AcceptedReport": {
        "type": "object",
        "required": ["id", "status"],
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "status": { "type": "string", "const": "pending" }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error", "message"],
        "properties": {
          "error": { "type": "string", "description": "Machine-readable error code." },
          "message": { "type": "string", "description": "Display message in Spanish." }
        }
      }
    },
    "responses": {
      "InvalidRequest": {
        "description": "A field or query parameter is invalid.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "HighTraffic": {
        "description": "The app or Supabase is rate limited. Retry after the delay specified by the response header.",
        "headers": {
          "Retry-After": {
            "description": "Number of seconds to wait before retrying.",
            "required": true,
            "schema": { "type": "integer", "minimum": 1 }
          }
        },
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": {
              "error": "HIGH_TRAFFIC",
              "message": "El sitio está experimentando mucho tráfico."
            }
          }
        }
      },
      "InternalError": {
        "description": "The application encountered an unexpected error.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "ServiceUnavailable": {
        "description": "Supabase is unavailable, or an attachment was submitted with media uploads disabled.",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      }
    }
  }
}
