{
  "openapi": "3.1.0",
  "info": {
    "title": "nlink API",
    "version": "1.0.0",
    "description": "nlink.tr link kısaltma API'si. Gold ve Enterprise planlarında kullanılabilir. Doküman: https://nlink.tr/tr/app/developers",
    "contact": { "email": "bilgi@nlink.tr", "url": "https://nlink.tr/tr/app/developers" }
  },
  "servers": [{ "url": "https://api.nlink.tr/api/v1" }],
  "security": [{ "bearerAuth": [] }, { "apiKeyHeader": [] }],
  "paths": {
    "/me": {
      "get": {
        "summary": "Hesap, plan ve kullanım",
        "operationId": "getMe",
        "responses": {
          "200": { "description": "Hesap bilgisi", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Me" } } } },
          "401": { "$ref": "#/components/responses/Error" },
          "403": { "$ref": "#/components/responses/Error" },
          "429": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/links": {
      "get": {
        "summary": "Linkleri listele (en yeni önce)",
        "operationId": "listLinks",
        "parameters": [
          { "name": "page", "in": "query", "schema": { "type": "integer", "minimum": 1, "default": 1 } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 50 } },
          { "name": "q", "in": "query", "description": "Hedef adres, kısa ad ya da başlıkta arama", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Sayfa", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/LinkPage" } } } },
          "401": { "$ref": "#/components/responses/Error" },
          "429": { "$ref": "#/components/responses/Error" }
        }
      },
      "post": {
        "summary": "Link oluştur",
        "operationId": "createLink",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateLink" } } }
        },
        "responses": {
          "201": { "description": "Oluşturuldu", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Link" } } } },
          "400": { "$ref": "#/components/responses/Error" },
          "401": { "$ref": "#/components/responses/Error" },
          "403": { "$ref": "#/components/responses/Error" },
          "409": { "$ref": "#/components/responses/Error" },
          "422": { "$ref": "#/components/responses/Error" },
          "429": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/links/{id}": {
      "parameters": [{ "$ref": "#/components/parameters/LinkId" }],
      "get": {
        "summary": "Tek link",
        "operationId": "getLink",
        "responses": {
          "200": { "description": "Link", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Link" } } } },
          "404": { "$ref": "#/components/responses/Error" }
        }
      },
      "patch": {
        "summary": "Hedef adresi ve/veya kısa adı güncelle",
        "operationId": "updateLink",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/UpdateLink" } } }
        },
        "responses": {
          "200": { "description": "Güncel link", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Link" } } } },
          "400": { "$ref": "#/components/responses/Error" },
          "404": { "$ref": "#/components/responses/Error" },
          "409": { "$ref": "#/components/responses/Error" },
          "422": { "$ref": "#/components/responses/Error" }
        }
      },
      "delete": {
        "summary": "Linki kalıcı olarak sil",
        "operationId": "deleteLink",
        "responses": {
          "204": { "description": "Silindi" },
          "404": { "$ref": "#/components/responses/Error" }
        }
      }
    },
    "/links/{id}/stats": {
      "parameters": [{ "$ref": "#/components/parameters/LinkId" }],
      "get": {
        "summary": "Tıklama istatistikleri (botlar hariç)",
        "operationId": "getLinkStats",
        "parameters": [{ "name": "days", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 365, "default": 30 } }],
        "responses": {
          "200": {
            "description": "İstatistik",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "link": { "$ref": "#/components/schemas/Link" },
                    "days": { "type": "integer" },
                    "stats": { "type": "object", "description": "Toplamlar, günlük seri ve kırılımlar (plan kademesine göre)" }
                  }
                }
              }
            }
          },
          "404": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "description": "Authorization: Bearer nlk_…" },
      "apiKeyHeader": { "type": "apiKey", "in": "header", "name": "X-API-Key" }
    },
    "parameters": {
      "LinkId": { "name": "id", "in": "path", "required": true, "description": "Link kimliği ya da kısa ad", "schema": { "type": "string" } }
    },
    "responses": {
      "Error": {
        "description": "Hata",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    },
    "schemas": {
      "Link": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "slug": { "type": "string" },
          "short_url": { "type": "string", "format": "uri" },
          "url": { "type": "string", "format": "uri" },
          "domain": { "type": "string" },
          "created_at": { "type": ["string", "null"], "format": "date-time" },
          "created_by": { "type": ["integer", "null"], "description": "Enterprise ekipte linki oluşturan üye" },
          "clicks": { "type": "integer" },
          "last_click_at": { "type": ["string", "null"], "format": "date-time" },
          "group_ids": { "type": "array", "items": { "type": "string" } }
        }
      },
      "LinkPage": {
        "type": "object",
        "properties": {
          "data": { "type": "array", "items": { "$ref": "#/components/schemas/Link" } },
          "page": { "type": "integer" },
          "limit": { "type": "integer" },
          "total": { "type": "integer" },
          "has_more": { "type": "boolean" }
        }
      },
      "CreateLink": {
        "type": "object",
        "required": ["url"],
        "properties": {
          "url": { "type": "string", "format": "uri" },
          "slug": { "type": "string", "description": "Özel kısa ad" },
          "domain": { "type": "string", "description": "nlink.tr ya da hesabınızın özel alan adı" }
        }
      },
      "UpdateLink": {
        "type": "object",
        "properties": {
          "url": { "type": "string", "format": "uri" },
          "slug": { "type": "string" }
        }
      },
      "Me": {
        "type": "object",
        "properties": {
          "member_id": { "type": "integer" },
          "name": { "type": "string" },
          "email": { "type": "string" },
          "plan": { "type": "string" },
          "custom_domain": { "type": ["string", "null"] },
          "limits": {
            "type": "object",
            "properties": {
              "links_per_month": { "type": "integer" },
              "clicks_per_month": { "type": "integer" },
              "requests_per_minute": { "type": "integer" }
            }
          },
          "usage": { "type": "object", "properties": { "clicks_this_month": { "type": "integer" } } }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string",
                "enum": ["unauthorized", "invalid_api_key", "plan_required", "account_deletion_pending", "rate_limited", "quota_exceeded", "invalid_url", "unsafe_url", "invalid_domain", "invalid_slug", "slug_taken", "slug_reserved", "slug_retired", "not_found", "internal"]
              },
              "message": { "type": "string" }
            }
          }
        }
      }
    }
  }
}
