{
    "openapi": "3.0.3",
    "info": {
        "title": "WisChat API",
        "version": "1.0.0",
        "description": "Envío de mensajes de WhatsApp (compatible con el formato de Meta), consulta de plantillas y estado de conexión de la línea."
    },
    "servers": [
        {
            "url": "http://v2.wis.chat"
        }
    ],
    "components": {
        "securitySchemes": {
            "bearerAuth": {
                "type": "http",
                "scheme": "bearer"
            }
        }
    },
    "security": [
        {
            "bearerAuth": []
        }
    ],
    "paths": {
        "/api/v22.0/{tid}/messages": {
            "post": {
                "summary": "Enviar un mensaje",
                "parameters": [
                    {
                        "name": "tid",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Phone Number ID o id interno de la línea"
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "to": {
                                        "type": "string",
                                        "example": "593999999999"
                                    },
                                    "type": {
                                        "type": "string",
                                        "enum": [
                                            "text",
                                            "template",
                                            "image",
                                            "document",
                                            "video",
                                            "audio"
                                        ]
                                    },
                                    "text": {
                                        "type": "object",
                                        "properties": {
                                            "body": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                },
                                "required": [
                                    "to",
                                    "type"
                                ]
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "Enviado",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "success": {
                                            "type": "boolean"
                                        },
                                        "wa_message_id": {
                                            "type": "string"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Token inválido"
                    }
                }
            }
        },
        "/api/v22.0/{tid}/templates": {
            "get": {
                "summary": "Listar las plantillas de la línea",
                "description": "Plantillas registradas en WisChat para la línea; reflejan el estado de aprobación que devuelve Meta.",
                "parameters": [
                    {
                        "name": "tid",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Phone Number ID o id interno de la línea"
                    },
                    {
                        "name": "status",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "string",
                            "enum": [
                                "approved",
                                "pending",
                                "rejected",
                                "disabled"
                            ]
                        }
                    },
                    {
                        "name": "limit",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 500,
                            "default": 200
                        }
                    },
                    {
                        "name": "refresh",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "boolean"
                        },
                        "description": "Resincroniza con Meta antes de responder (solo Cloud API; enfriamiento de 60 s)"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Listado de plantillas",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "success": {
                                            "type": "boolean"
                                        },
                                        "count": {
                                            "type": "integer"
                                        },
                                        "synced_at": {
                                            "type": "string",
                                            "nullable": true,
                                            "format": "date-time"
                                        },
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "type": "object",
                                                "properties": {
                                                    "name": {
                                                        "type": "string"
                                                    },
                                                    "language": {
                                                        "type": "string"
                                                    },
                                                    "category": {
                                                        "type": "string"
                                                    },
                                                    "status": {
                                                        "type": "string"
                                                    },
                                                    "meta_status": {
                                                        "type": "string",
                                                        "nullable": true
                                                    },
                                                    "body": {
                                                        "type": "string"
                                                    },
                                                    "variables": {
                                                        "type": "integer"
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Token inválido"
                    },
                    "403": {
                        "description": "Línea desactivada o API deshabilitado"
                    }
                }
            }
        },
        "/api/v22.0/{tid}/status": {
            "get": {
                "summary": "Estado de conexión de la línea",
                "description": "Solo lectura. Devuelve el estado que reporta Meta para la línea Cloud API.",
                "parameters": [
                    {
                        "name": "tid",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Phone Number ID o id interno de la línea"
                    },
                    {
                        "name": "refresh",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "type": "boolean"
                        },
                        "description": "Consulta a Meta en el momento (solo Cloud API; enfriamiento de 60 s)"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Estado de la línea",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "success": {
                                            "type": "boolean"
                                        },
                                        "connected": {
                                            "type": "boolean",
                                            "nullable": true,
                                            "description": "null cuando el estado es unknown"
                                        },
                                        "state": {
                                            "type": "string",
                                            "enum": [
                                                "connected",
                                                "disconnected",
                                                "pending",
                                                "unknown"
                                            ]
                                        },
                                        "checked_at": {
                                            "type": "string",
                                            "format": "date-time"
                                        },
                                        "line": {
                                            "type": "object",
                                            "properties": {
                                                "id": {
                                                    "type": "integer"
                                                },
                                                "name": {
                                                    "type": "string"
                                                },
                                                "phone_number": {
                                                    "type": "string"
                                                },
                                                "channel": {
                                                    "type": "string",
                                                    "enum": [
                                                        "cloud_api"
                                                    ]
                                                },
                                                "is_active": {
                                                    "type": "boolean"
                                                },
                                                "api_enabled": {
                                                    "type": "boolean"
                                                }
                                            }
                                        },
                                        "meta": {
                                            "type": "object",
                                            "description": "Estado que reporta Meta para la línea",
                                            "properties": {
                                                "meta_status": {
                                                    "type": "string",
                                                    "nullable": true
                                                },
                                                "quality_rating": {
                                                    "type": "string",
                                                    "nullable": true
                                                },
                                                "messaging_limit": {
                                                    "type": "string",
                                                    "nullable": true
                                                },
                                                "has_credentials": {
                                                    "type": "boolean"
                                                },
                                                "last_checked_at": {
                                                    "type": "string",
                                                    "nullable": true,
                                                    "format": "date-time"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Token inválido"
                    },
                    "403": {
                        "description": "Línea desactivada o API deshabilitado"
                    }
                }
            }
        }
    }
}