{
  "openapi": "3.0.3",
  "info": {
    "title": "INKZ OTP Reseller API",
    "version": "1.0.0",
    "description": "API สำหรับ reseller: เช็คยอดเงิน เลือกประเทศ เลือกแอพที่เปิดขาย ซื้อเบอร์ OTP เช็ค SMS ยกเลิก/คืนเงิน และดูประวัติออเดอร์",
    "contact": {
      "name": "INKZ OTP",
      "url": "https://home.inkz.shop"
    }
  },
  "servers": [
    {
      "url": "https://home.inkz.shop/api/reseller",
      "description": "Production"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "tags": [
    { "name": "Balance", "description": "ยอดเงิน" },
    { "name": "Catalog", "description": "ประเทศและบริการที่เปิดขาย" },
    { "name": "Orders", "description": "สั่งซื้อและจัดการออเดอร์" }
  ],
  "paths": {
    "/get_balance.php": {
      "get": {
        "tags": ["Balance"],
        "summary": "เช็คยอดเงินคงเหลือ",
        "operationId": "getBalance",
        "responses": {
          "200": {
            "description": "สำเร็จ",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BalanceResponse" },
                "example": {
                  "success": true,
                  "message": "ดึงยอดเงินสำเร็จ",
                  "data": {
                    "user_id": 42,
                    "balance": 1500.0,
                    "currency": "THB"
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" }
        }
      }
    },
    "/get_countries.php": {
      "get": {
        "tags": ["Catalog"],
        "summary": "ดูรหัสประเทศที่เลือกได้",
        "operationId": "getCountries",
        "parameters": [
          {
            "name": "server",
            "in": "query",
            "required": false,
            "schema": { "type": "string", "enum": ["server1", "server2"], "default": "server1" }
          }
        ],
        "responses": {
          "200": {
            "description": "สำเร็จ",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CountriesResponse" },
                "example": {
                  "success": true,
                  "message": "ดึงรายชื่อประเทศสำเร็จ",
                  "data": {
                    "server": "server1",
                    "source": "provider",
                    "count": 2,
                    "countries": [
                      { "id": "52", "name": "Thailand", "name_th": "ไทย" },
                      { "id": "4", "name": "Philippines", "name_th": "ฟิลิปปินส์" }
                    ]
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" }
        }
      }
    },
    "/get_services.php": {
      "get": {
        "tags": ["Catalog"],
        "summary": "ดูแอพ/บริการที่เปิดขาย พร้อมราคาและสต็อก",
        "operationId": "getServices",
        "parameters": [
          {
            "name": "server",
            "in": "query",
            "required": false,
            "schema": { "type": "string", "enum": ["server1", "server2"], "default": "server1" }
          },
          {
            "name": "country",
            "in": "query",
            "required": false,
            "description": "รหัสประเทศจาก get_countries.php เช่น 52",
            "schema": { "type": "string", "example": "52" }
          },
          {
            "name": "in_stock",
            "in": "query",
            "required": false,
            "description": "1 = เฉพาะบริการที่มีสต็อกมากกว่า 0",
            "schema": { "type": "string", "enum": ["0", "1"], "default": "0" }
          }
        ],
        "responses": {
          "200": {
            "description": "สำเร็จ",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ServicesResponse" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "502": {
            "description": "ตรวจสต็อกจากผู้ให้บริการไม่ได้",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/buy_otp.php": {
      "post": {
        "tags": ["Orders"],
        "summary": "ซื้อเบอร์ OTP",
        "operationId": "buyOtp",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BuyRequest" },
              "example": {
                "service_id": 1,
                "server": "server1",
                "quantity": 1,
                "country": "52"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "สำเร็จ",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BuyResponse" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": {
            "description": "ยอดเงินไม่เพียงพอ",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" }
              }
            }
          },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/check_sms.php": {
      "post": {
        "tags": ["Orders"],
        "summary": "เช็ค SMS / OTP ของออเดอร์",
        "operationId": "checkSms",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["order_id"],
                "properties": {
                  "order_id": { "type": "integer", "example": 501 }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "สำเร็จ",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CheckSmsResponse" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/activation_action.php": {
      "post": {
        "tags": ["Orders"],
        "summary": "ยกเลิก / ขอ SMS ซ้ำ / จบงาน",
        "operationId": "activationAction",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["order_id", "action"],
                "properties": {
                  "order_id": { "type": "integer", "example": 501 },
                  "action": { "type": "integer", "enum": [1, 3, 6, 8], "example": 8 }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "สำเร็จหรือคำสั่งถูกปฏิเสธตามเงื่อนไขผู้ให้บริการ",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ActionResponse" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" }
        }
      }
    },
    "/get_orders.php": {
      "get": {
        "tags": ["Orders"],
        "summary": "ดูประวัติออเดอร์",
        "operationId": "getOrders",
        "parameters": [
          {
            "name": "page",
            "in": "query",
            "required": false,
            "schema": { "type": "integer", "minimum": 1, "default": 1 }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 20 }
          },
          {
            "name": "status",
            "in": "query",
            "required": false,
            "schema": { "type": "string", "enum": ["WAITING", "COMPLETED", "CANCELLED"] }
          }
        ],
        "responses": {
          "200": {
            "description": "สำเร็จ",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/OrdersResponse" }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "ส่ง API Key เป็น Authorization: Bearer YOUR_API_KEY"
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "API Key ไม่ถูกต้อง",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" }
          }
        }
      },
      "BadRequest": {
        "description": "ข้อมูลไม่ถูกต้อง",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" }
          }
        }
      },
      "NotFound": {
        "description": "ไม่พบข้อมูล",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/ErrorResponse" }
          }
        }
      }
    },
    "schemas": {
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": false },
          "message": { "type": "string", "example": "ยอดเงินไม่เพียงพอ" },
          "data": { "nullable": true, "type": "object", "additionalProperties": true }
        }
      },
      "BalanceResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "message": { "type": "string" },
          "data": {
            "type": "object",
            "properties": {
              "user_id": { "type": "integer" },
              "balance": { "type": "number", "format": "float" },
              "currency": { "type": "string", "example": "THB" }
            }
          }
        }
      },
      "Country": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "example": "52" },
          "name": { "type": "string", "example": "Thailand" },
          "name_th": { "type": "string", "example": "ไทย" }
        }
      },
      "CountriesResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "message": { "type": "string" },
          "data": {
            "type": "object",
            "properties": {
              "server": { "type": "string", "enum": ["server1", "server2"] },
              "source": { "type": "string", "enum": ["provider", "fallback"] },
              "count": { "type": "integer" },
              "countries": {
                "type": "array",
                "items": { "$ref": "#/components/schemas/Country" }
              },
              "warning": { "type": "string" }
            }
          }
        }
      },
      "Service": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "code": { "type": "string" },
          "name": { "type": "string" },
          "original_price": { "type": "number", "format": "float" },
          "price": { "type": "number", "format": "float" },
          "discount": { "type": "number", "format": "float" },
          "image_url": { "type": "string", "nullable": true },
          "category": { "type": "string", "nullable": true },
          "server": { "type": "string", "enum": ["server1", "server2"] },
          "stock_count": { "type": "integer", "nullable": true },
          "available": { "type": "boolean", "nullable": true }
        }
      },
      "ServicesResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "message": { "type": "string" },
          "data": {
            "type": "object",
            "properties": {
              "server": { "type": "string" },
              "country": { "type": "string", "nullable": true },
              "in_stock": { "type": "boolean" },
              "count": { "type": "integer" },
              "services": {
                "type": "array",
                "items": { "$ref": "#/components/schemas/Service" }
              },
              "warning": { "type": "string" }
            }
          }
        }
      },
      "BuyRequest": {
        "type": "object",
        "required": ["service_id"],
        "properties": {
          "service_id": { "type": "integer", "example": 1 },
          "server": { "type": "string", "enum": ["server1", "server2"], "default": "server1" },
          "quantity": { "type": "integer", "minimum": 1, "maximum": 15, "default": 1 },
          "country": { "type": "string", "example": "52", "description": "รหัสประเทศจาก get_countries.php" }
        }
      },
      "Order": {
        "type": "object",
        "properties": {
          "order_id": { "type": "integer" },
          "activation_id": { "type": "string", "nullable": true },
          "phone_number": { "type": "string" },
          "price": { "type": "number", "format": "float" }
        }
      },
      "BuyResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "message": { "type": "string" },
          "data": {
            "type": "object",
            "properties": {
              "quantity": { "type": "integer" },
              "unit_price": { "type": "number", "format": "float" },
              "total_price": { "type": "number", "format": "float" },
              "original_unit_price": { "type": "number", "format": "float" },
              "discount_per_unit": { "type": "number", "format": "float" },
              "remaining_balance": { "type": "number", "format": "float" },
              "server": { "type": "string" },
              "country": { "type": "string" },
              "order_ids": { "type": "array", "items": { "type": "integer" } },
              "orders": { "type": "array", "items": { "$ref": "#/components/schemas/Order" } }
            }
          }
        }
      },
      "CheckSmsResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "message": { "type": "string" },
          "data": {
            "type": "object",
            "properties": {
              "order_id": { "type": "integer" },
              "phone_number": { "type": "string" },
              "sms_code": { "type": "string", "nullable": true },
              "status": { "type": "string" },
              "service_name": { "type": "string" }
            }
          }
        }
      },
      "ActionResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean" },
          "message": { "type": "string" },
          "data": { "type": "object", "nullable": true }
        }
      },
      "OrderHistory": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "service_id": { "type": "integer" },
          "activation_id": { "type": "string", "nullable": true },
          "phone_number": { "type": "string" },
          "sms_code": { "type": "string", "nullable": true },
          "price": { "type": "string" },
          "status": { "type": "string" },
          "server": { "type": "string" },
          "created_at": { "type": "string" },
          "updated_at": { "type": "string" },
          "service_name": { "type": "string" }
        }
      },
      "OrdersResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "message": { "type": "string" },
          "data": {
            "type": "object",
            "properties": {
              "page": { "type": "integer" },
              "limit": { "type": "integer" },
              "total": { "type": "integer" },
              "total_pages": { "type": "integer" },
              "orders": {
                "type": "array",
                "items": { "$ref": "#/components/schemas/OrderHistory" }
              }
            }
          }
        }
      }
    }
  }
}
