{
  "openapi": "3.0.3",
  "info": {
    "title": "LinkPulse API",
    "version": "1.0.0",
    "description": "RESTful API for the LinkPulse URL shortening and analytics platform. Provides endpoints for user registration, link management, analytics retrieval, and QR code generation.",
    "contact": {
      "name": "Marian Oboroceanu",
      "url": "https://github.com/Bossiq"
    },
    "license": {
      "name": "MIT",
      "url": "https://opensource.org/licenses/MIT"
    }
  },
  "servers": [
    {
      "url": "https://bossiq-linkpulse.vercel.app",
      "description": "Production"
    },
    {
      "url": "http://localhost:3000",
      "description": "Local development"
    }
  ],
  "tags": [
    { "name": "Auth", "description": "User registration and authentication" },
    { "name": "Links", "description": "Link CRUD operations" },
    { "name": "QR", "description": "QR code generation" }
  ],
  "paths": {
    "/api/auth/register": {
      "post": {
        "tags": ["Auth"],
        "summary": "Register a new user",
        "description": "Creates a new user account with email and password credentials.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RegisterInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "User created successfully",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UserResponse"
                }
              }
            }
          },
          "409": {
            "description": "Email already in use",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          },
          "422": {
            "description": "Validation failed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ValidationError" }
              }
            }
          }
        }
      }
    },
    "/api/links": {
      "get": {
        "tags": ["Links"],
        "summary": "List user's links",
        "description": "Returns paginated list of links for the authenticated user, newest first.",
        "security": [{ "session": [] }],
        "parameters": [
          {
            "name": "page",
            "in": "query",
            "schema": { "type": "integer", "default": 1, "minimum": 1 },
            "description": "Page number"
          },
          {
            "name": "limit",
            "in": "query",
            "schema": { "type": "integer", "default": 20, "minimum": 1, "maximum": 100 },
            "description": "Items per page"
          },
          {
            "name": "search",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Filter by URL, slug, or title"
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated links list",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/LinksListResponse" }
              }
            }
          },
          "401": {
            "description": "Authentication required",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          }
        }
      },
      "post": {
        "tags": ["Links"],
        "summary": "Create a shortened link",
        "description": "Creates a new shortened link. Auto-generates a slug if not provided. Supports optional expiration, click limits, and password protection.",
        "security": [{ "session": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreateLinkInput" }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Link created successfully",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/LinkResponse" }
              }
            }
          },
          "401": {
            "description": "Authentication required"
          },
          "409": {
            "description": "Slug already taken or reserved"
          },
          "422": {
            "description": "Validation failed"
          }
        }
      }
    },
    "/api/links/{id}": {
      "delete": {
        "tags": ["Links"],
        "summary": "Delete a link",
        "description": "Deletes a link and all its associated click analytics. Only the link owner can delete.",
        "security": [{ "session": [] }],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "Link ID (CUID)"
          }
        ],
        "responses": {
          "200": {
            "description": "Link deleted successfully",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean", "example": true }
                  }
                }
              }
            }
          },
          "401": { "description": "Authentication required" },
          "403": { "description": "Forbidden — not the link owner" },
          "404": { "description": "Link not found" }
        }
      }
    },
    "/api/links/{id}/qr": {
      "get": {
        "tags": ["QR"],
        "summary": "Generate QR code for a link",
        "description": "Returns a QR code image for the specified link in SVG or PNG format. Only accessible by the link owner.",
        "security": [{ "session": [] }],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" },
            "description": "Link ID (CUID)"
          },
          {
            "name": "size",
            "in": "query",
            "schema": { "type": "integer", "default": 300, "minimum": 100, "maximum": 1000 },
            "description": "QR code size in pixels"
          },
          {
            "name": "format",
            "in": "query",
            "schema": { "type": "string", "enum": ["svg", "png"], "default": "svg" },
            "description": "Output format"
          }
        ],
        "responses": {
          "200": {
            "description": "QR code image",
            "content": {
              "image/svg+xml": {
                "schema": { "type": "string" }
              },
              "image/png": {
                "schema": { "type": "string", "format": "binary" }
              }
            }
          },
          "401": { "description": "Authentication required" },
          "403": { "description": "Forbidden" },
          "404": { "description": "Link not found" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "session": {
        "type": "apiKey",
        "in": "cookie",
        "name": "authjs.session-token",
        "description": "Auth.js session cookie. Obtained by signing in via /login or OAuth."
      }
    },
    "schemas": {
      "RegisterInput": {
        "type": "object",
        "required": ["name", "email", "password", "confirmPassword"],
        "properties": {
          "name": { "type": "string", "minLength": 2, "maxLength": 50, "example": "Jane Doe" },
          "email": { "type": "string", "format": "email", "example": "jane@example.com" },
          "password": { "type": "string", "minLength": 8, "example": "SecurePass123" },
          "confirmPassword": { "type": "string", "example": "SecurePass123" }
        }
      },
      "UserResponse": {
        "type": "object",
        "properties": {
          "user": {
            "type": "object",
            "properties": {
              "id": { "type": "string", "example": "cm5abc123" },
              "email": { "type": "string", "example": "jane@example.com" },
              "name": { "type": "string", "example": "Jane Doe" }
            }
          }
        }
      },
      "CreateLinkInput": {
        "type": "object",
        "required": ["url"],
        "properties": {
          "url": { "type": "string", "format": "uri", "example": "https://example.com/long-article-about-nextjs" },
          "slug": { "type": "string", "pattern": "^[a-zA-Z0-9_-]+$", "example": "my-link" },
          "title": { "type": "string", "example": "Next.js Article" },
          "description": { "type": "string", "example": "A comprehensive guide to Next.js" },
          "expiresAt": { "type": "string", "format": "date-time", "example": "2026-12-31T23:59:59Z" },
          "maxClicks": { "type": "integer", "minimum": 1, "example": 1000 },
          "password": { "type": "string", "example": "secret123" }
        }
      },
      "LinkResponse": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "example": "cm5xyz789" },
          "url": { "type": "string", "example": "https://example.com/long-article-about-nextjs" },
          "slug": { "type": "string", "example": "my-link" },
          "shortUrl": { "type": "string", "example": "https://bossiq-linkpulse.vercel.app/my-link" },
          "title": { "type": "string", "nullable": true },
          "isActive": { "type": "boolean", "example": true },
          "clicks": { "type": "integer", "example": 0 },
          "createdAt": { "type": "string", "format": "date-time" }
        }
      },
      "LinksListResponse": {
        "type": "object",
        "properties": {
          "links": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/LinkResponse" }
          },
          "pagination": {
            "type": "object",
            "properties": {
              "page": { "type": "integer", "example": 1 },
              "limit": { "type": "integer", "example": 20 },
              "total": { "type": "integer", "example": 42 },
              "totalPages": { "type": "integer", "example": 3 }
            }
          }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "error": { "type": "string", "example": "Something went wrong. Please try again." }
        }
      },
      "ValidationError": {
        "type": "object",
        "properties": {
          "error": { "type": "string", "example": "Validation failed" },
          "details": { "type": "object" }
        }
      }
    }
  }
}
