{
  "openapi": "3.1.0",
  "info": {
    "title": "Praktik.se Public API",
    "version": "1.0.0",
    "description": "REST-API för att hantera praktikannonser på Praktik.se via tredjepartsystem. Autentisering sker med API-nyckel som genereras i företagsinställningarna.",
    "contact": { "name": "Praktik.se", "url": "https://praktik.se" }
  },
  "servers": [
    {
      "url": "https://app.praktik.se/functions/v1/api/v1",
      "description": "Produktion (Edge Functions)"
    }
  ],
  "security": [{ "ApiKeyAuth": [] }],
  "tags": [
    { "name": "Meta", "description": "Publika metadata-endpoints (ingen auth)." },
    { "name": "Internships", "description": "Hantera praktikannonser för det företag som äger API-nyckeln." }
  ],
  "paths": {
    "/meta": {
      "get": {
        "tags": ["Meta"],
        "summary": "API-metadata & hälsokontroll",
        "security": [],
        "responses": {
          "200": {
            "description": "Returnerar version och tillgängliga endpoints.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "name": { "type": "string" },
                    "version": { "type": "string" },
                    "endpoints": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "method": { "type": "string" },
                          "path": { "type": "string" }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/internships": {
      "get": {
        "tags": ["Internships"],
        "summary": "Lista företagets annonser",
        "parameters": [
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 50 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "minimum": 0, "default": 0 } }
        ],
        "responses": {
          "200": {
            "description": "Lista av annonser.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Internship" } },
                    "pagination": {
                      "type": "object",
                      "properties": {
                        "limit": { "type": "integer" },
                        "offset": { "type": "integer" },
                        "total": { "type": "integer" }
                      }
                    }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      },
      "post": {
        "tags": ["Internships"],
        "summary": "Skapa ny annons",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/InternshipCreate" }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Annonsen skapades.",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Internship" } } }
          },
          "400": { "$ref": "#/components/responses/ValidationError" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "422": { "$ref": "#/components/responses/TierLimit" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    },
    "/internships/{id}": {
      "parameters": [
        { "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }
      ],
      "get": {
        "tags": ["Internships"],
        "summary": "Hämta en annons",
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Internship" } } } },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      },
      "patch": {
        "tags": ["Internships"],
        "summary": "Uppdatera en annons",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/InternshipPatch" } } }
        },
        "responses": {
          "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Internship" } } } },
          "400": { "$ref": "#/components/responses/ValidationError" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      },
      "delete": {
        "tags": ["Internships"],
        "summary": "Ta bort en annons",
        "responses": {
          "204": { "description": "Annonsen togs bort." },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "Authorization",
        "description": "Skicka API-nyckeln som `Authorization: Bearer pk_live_...`."
      }
    },
    "schemas": {
      "Internship": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "job_title": { "type": "string", "maxLength": 200 },
          "description": { "type": "string", "maxLength": 20000 },
          "about_company": { "type": "string", "nullable": true, "maxLength": 10000 },
          "location": { "type": "string", "maxLength": 120 },
          "start_date": { "type": "string", "format": "date", "nullable": true },
          "deadline": { "type": "string", "format": "date" },
          "paid": { "type": "string", "enum": ["Betald", "Obetald"] },
          "logo_url": { "type": "string", "format": "uri", "nullable": true },
          "image_url": { "type": "string", "format": "uri", "nullable": true },
          "fields": { "type": "array", "items": { "type": "string", "maxLength": 100 }, "maxItems": 20 },
          "contact_person": { "type": "object", "additionalProperties": { "type": "string" } },
          "external_application_url": { "type": "string", "format": "uri", "nullable": true },
          "status": { "type": "string", "enum": ["draft", "published"] },
          "published_at": { "type": "string", "format": "date-time", "nullable": true },
          "created_at": { "type": "string", "format": "date-time" },
          "updated_at": { "type": "string", "format": "date-time" }
        }
      },
      "InternshipCreate": {
        "type": "object",
        "required": ["job_title", "description", "location", "deadline"],
        "properties": {
          "job_title": { "type": "string", "minLength": 2, "maxLength": 200 },
          "description": { "type": "string", "minLength": 10, "maxLength": 20000 },
          "about_company": { "type": "string", "maxLength": 10000, "nullable": true },
          "location": { "type": "string", "maxLength": 120 },
          "start_date": { "type": "string", "format": "date", "nullable": true },
          "deadline": { "type": "string", "format": "date" },
          "paid": { "type": "string", "enum": ["Betald", "Obetald"] },
          "logo_url": { "type": "string", "format": "uri", "nullable": true },
          "image_url": { "type": "string", "format": "uri", "nullable": true },
          "fields": { "type": "array", "items": { "type": "string", "maxLength": 100 }, "maxItems": 20 },
          "contact_person": { "type": "object", "additionalProperties": { "type": "string" } },
          "external_application_url": { "type": "string", "format": "uri", "nullable": true },
          "status": { "type": "string", "enum": ["draft", "published"], "default": "draft" },
          "publish": { "type": "boolean", "description": "Genväg: sätt true för att direktpublicera." },
          "published_at": { "type": "string", "format": "date-time", "nullable": true }
        }
      },
      "InternshipPatch": {
        "type": "object",
        "description": "Skicka endast de fält som ska uppdateras. Samma valideringsregler som vid skapande.",
        "properties": {
          "job_title": { "type": "string", "minLength": 2, "maxLength": 200 },
          "description": { "type": "string", "minLength": 10, "maxLength": 20000 },
          "about_company": { "type": "string", "maxLength": 10000, "nullable": true },
          "location": { "type": "string", "maxLength": 120 },
          "start_date": { "type": "string", "format": "date", "nullable": true },
          "deadline": { "type": "string", "format": "date" },
          "paid": { "type": "string", "enum": ["Betald", "Obetald"] },
          "logo_url": { "type": "string", "format": "uri", "nullable": true },
          "image_url": { "type": "string", "format": "uri", "nullable": true },
          "fields": { "type": "array", "items": { "type": "string", "maxLength": 100 }, "maxItems": 20 },
          "contact_person": { "type": "object", "additionalProperties": { "type": "string" } },
          "external_application_url": { "type": "string", "format": "uri", "nullable": true },
          "status": { "type": "string", "enum": ["draft", "published"] },
          "publish": { "type": "boolean" },
          "published_at": { "type": "string", "format": "date-time", "nullable": true }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" },
          "message": { "type": "string" },
          "fields": {
            "type": "object",
            "additionalProperties": { "type": "array", "items": { "type": "string" } }
          }
        }
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "API-nyckel saknas eller är ogiltig.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "NotFound": {
        "description": "Resursen hittades inte.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "ValidationError": {
        "description": "Ogiltig request-body.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "TierLimit": {
        "description": "Prenumerationens annonsgräns är nådd.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "RateLimited": {
        "description": "För många anrop. Max 60/min per nyckel, 600/min per företag.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    }
  }
}
