{
  "openapi": "3.1.0",
  "info": {
    "title": "Outlook Reader API",
    "version": "1.0.0",
    "description": "Baca kode OTP dan isi inbox akun Outlook/Hotmail via Microsoft Graph. Semua endpoint /api/v1/* butuh header X-API-Key.",
    "contact": { "name": "Outlook Reader" }
  },
  "servers": [{ "url": "/", "description": "current host" }],
  "security": [{ "ApiKeyAuth": [] }],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "X-API-Key" }
    },
    "schemas": {
      "CredentialString": {
        "type": "string",
        "description": "email|password|refresh_token|client_id (password ignored by server)",
        "example": "user@outlook.com|pass|M.C5_SN1...|9e5f94bc-..."
      },
      "Message": {
        "type": "object",
        "properties": {
          "subject": { "type": "string" },
          "from": { "type": "string" },
          "fromEmail": { "type": "string" },
          "date": { "type": "string", "format": "date-time" },
          "preview": { "type": "string" },
          "body": { "type": "string" },
          "isRead": { "type": "boolean" }
        }
      },
      "OtpResult": {
        "type": "object",
        "properties": {
          "email": { "type": "string" },
          "success": { "type": "boolean" },
          "modeUsed": { "type": "string", "enum": ["oauth2", "graph"] },
          "code": { "type": ["string", "null"] },
          "codeSource": {
            "type": ["object", "null"],
            "properties": {
              "subject": { "type": "string" },
              "from": { "type": "string" },
              "fromEmail": { "type": "string" },
              "receivedAt": { "type": "string", "format": "date-time" }
            }
          },
          "messageCount": { "type": "integer" },
          "messages": { "type": "array", "items": { "$ref": "#/components/schemas/Message" } },
          "error": { "type": "string" }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "const": false },
          "error": { "type": "string" }
        }
      }
    }
  },
  "paths": {
    "/api/v1/otp": {
      "post": {
        "summary": "Baca OTP satu akun",
        "description": "Login, ambil pesan terbaru, pindai dari yang paling baru untuk kode verifikasi pertama.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "credentials": { "$ref": "#/components/schemas/CredentialString" },
                  "email": { "type": "string" },
                  "refresh_token": { "type": "string" },
                  "client_id": { "type": "string" },
                  "mode": { "type": "string", "enum": ["oauth2", "graph"], "default": "oauth2" },
                  "top": { "type": "integer", "minimum": 1, "maximum": 50, "default": 20 }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Berhasil diproses",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/OtpResult" }
              }
            }
          },
          "400": { "description": "Request tidak valid", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "401": { "description": "API key salah/dicabut", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "422": { "description": "Akun ditolak Microsoft", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "429": { "description": "Rate limit", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "502": { "description": "Graph API gagal", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
        }
      }
    },
    "/api/v1/otp/bulk": {
      "post": {
        "summary": "Baca OTP banyak akun (maks 25)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "credentials": { "type": "string", "description": "satu akun per baris, maks 25" },
                  "mode": { "type": "string", "enum": ["oauth2", "graph"], "default": "oauth2" },
                  "top": { "type": "integer", "minimum": 1, "maximum": 50, "default": 20 }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Batch diproses",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "count": { "type": "integer" },
                    "results": { "type": "array", "items": { "$ref": "#/components/schemas/OtpResult" } }
                  }
                }
              }
            }
          },
          "400": { "description": "Terlalu banyak akun / format salah" },
          "401": { "description": "API key salah" }
        }
      }
    },
    "/api/v1/check": {
      "post": {
        "summary": "Cek status akun (LIVE/DIE/UNKNOWN)",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "credentials": { "type": "string" },
                  "mode": { "type": "string", "enum": ["oauth2", "graph"], "default": "oauth2" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Hasil cek",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "total": { "type": "integer" },
                    "counts": {
                      "type": "object",
                      "properties": {
                        "live": { "type": "integer" },
                        "die": { "type": "integer" },
                        "unknown": { "type": "integer" }
                      }
                    },
                    "results": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "email": { "type": "string" },
                          "status": { "type": "string", "enum": ["LIVE", "DIE", "UNKNOWN"] },
                          "modeUsed": { "type": "string" },
                          "reason": { "type": "string" }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/renew": {
      "post": {
        "summary": "Perbarui refresh_token",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": { "credentials": { "type": "string" } }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Token baru (kalau ada)" }
        }
      }
    },
    "/api/v1/whoami": {
      "get": {
        "summary": "Status dan pemakaian key",
        "responses": {
          "200": {
            "description": "Info key",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": { "type": "boolean" },
                    "label": { "type": "string" },
                    "rateLimitPerMin": { "type": "integer" },
                    "requestCount": { "type": "integer" },
                    "lastUsedAt": { "type": ["string", "null"] },
                    "createdAt": { "type": "string" }
                  }
                }
              }
            }
          },
          "401": { "description": "API key salah" }
        }
      }
    },
    "/api/v1/keys/request": {
      "post": {
        "summary": "Ajukan API key baru (menunggu approval admin)",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["label"],
                "properties": {
                  "label": { "type": "string" },
                  "telegramId": { "type": "string" },
                  "telegramUser": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Permintaan tercatat" },
          "429": { "description": "Terlalu sering mengajukan" }
        }
      }
    }
  }
}
