{
  "openapi": "3.0.3",
  "info": {
    "title": "Mailganer Countdown Timer API",
    "version": "0.1.0",
    "description": "Создание и раздача живых GIF/JPEG таймеров для email. Картинки всегда рендерятся при запросе (не статический файл). Персонализация даты — query `?end=` на URL картинки.\n\nПубличный create/preview (#21407): `POST /tools/email-countdown-timer/v1` и `/v1/preview` (nginx → WP REST). ЛК: тот же путь + `X-Countdown-Token`. Картинки в письмах — только `cdn-timer.mailganer.com` (`/t/{id}.gif|.jpg`), не путь кабинета на apex. UI: `/tools/email-countdown-timer`."
  },
  "servers": [
    {
      "url": "https://newland.mailganer.com",
      "description": "Create/preview (стенд)"
    },
    {
      "url": "https://mailganer.com",
      "description": "Create/preview (apex после катовера)"
    },
    {
      "url": "https://cdn-timer.mailganer.com",
      "description": "CDN — только раздача GIF/JPEG (`/t/…`)"
    }
  ],
  "tags": [
    { "name": "timers", "description": "Создание и превью" },
    { "name": "images", "description": "Раздача GIF/JPEG" },
    { "name": "system", "description": "Health" }
  ],
  "paths": {
    "/tools/email-countdown-timer/v1/status": {
      "get": {
        "tags": ["system"],
        "summary": "Статус витрины (квоты, капча). Без create.",
        "operationId": "publicStatus",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean", "example": true },
                    "service": { "type": "string", "example": "email-countdown-timer" },
                    "configured": { "type": "boolean" },
                    "dailyRemaining": { "type": "integer" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/health": {
      "get": {
        "tags": ["system"],
        "summary": "Health check",
        "operationId": "health",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean", "example": true },
                    "service": { "type": "string", "example": "countdown-timer" },
                    "version": { "type": "string", "example": "0.1.0" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/tools/email-countdown-timer/v1": {
      "post": {
        "tags": ["timers"],
        "summary": "Создать таймер",
        "description": "Сохраняет конфиг (TTL ~30 дней) и возвращает GIF/JPEG URL на CDN + HTML-сниппет. При включённой captcha нужен токен (тело `captcha_token` или заголовок `X-Captcha-Token`). Для ЛК — `X-Countdown-Token` (без captcha и WP-квоты). Выбирайте server витрины, не CDN.",
        "operationId": "createTimer",
        "parameters": [
          {
            "name": "X-Captcha-Token",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Токен SmartCaptcha (если captcha_mode ≠ off)"
          },
          {
            "name": "X-Countdown-Token",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Сервисный токен ЛК (#18988); bypass captcha/quota"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/TimerCreate" },
              "example": {
                "end": "2026-08-01T23:59:00+03:00",
                "timezone": "Europe/Moscow",
                "layout": "classic",
                "show_days": true,
                "show_hours": true,
                "show_minutes": true,
                "show_seconds": true,
                "bg_color": "#000000",
                "digit_color": "#FFFFFF",
                "label_color": "#AAAAAA",
                "ended_mode": "text",
                "ended_text": "Акция закончилась"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Таймер создан",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/TimerCreatedResponse" }
              }
            }
          },
          "403": { "description": "Captcha не пройдена" },
          "422": { "description": "Ошибка валидации тела" },
          "429": { "description": "Лимит IP или глобальный дневной лимит" }
        }
      }
    },
    "/tools/email-countdown-timer/v1/preview": {
      "post": {
        "tags": ["timers"],
        "summary": "Превью без сохранения",
        "description": "Короткий GIF (~10 с) для живого превью в UI. Без captcha; отдельный rate limit. Server — витрина, не CDN.",
        "operationId": "previewTimer",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/TimerCreate" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "image/gif",
            "content": {
              "image/gif": {
                "schema": { "type": "string", "format": "binary" }
              }
            }
          },
          "429": { "description": "Слишком много превью с IP" }
        }
      }
    },
    "/t/{timer_id}.gif": {
      "get": {
        "tags": ["images"],
        "summary": "Живой GIF",
        "description": "Анимация остатка времени (retina @2x). Query `end` — переопределение дедлайна (ISO 8601 или unix timestamp) для персонализации.",
        "operationId": "getGif",
        "parameters": [
          {
            "name": "timer_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          },
          {
            "name": "end",
            "in": "query",
            "required": false,
            "schema": { "type": "string" },
            "description": "Override дедлайна (merge-tag / персонализация)"
          }
        ],
        "responses": {
          "200": {
            "description": "image/gif",
            "content": {
              "image/gif": {
                "schema": { "type": "string", "format": "binary" }
              }
            }
          },
          "404": { "description": "Таймер не найден или истёк TTL" }
        }
      }
    },
    "/t/{timer_id}.jpg": {
      "get": {
        "tags": ["images"],
        "summary": "JPEG-фолбэк",
        "description": "Статичный кадр для клиентов без GIF. Тот же `?end=`.",
        "operationId": "getJpeg",
        "parameters": [
          {
            "name": "timer_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          },
          {
            "name": "end",
            "in": "query",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "image/jpeg",
            "content": {
              "image/jpeg": {
                "schema": { "type": "string", "format": "binary" }
              }
            }
          },
          "404": { "description": "Таймер не найден или истёк TTL" }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "TimerCreate": {
        "type": "object",
        "required": ["end"],
        "properties": {
          "end": {
            "type": "string",
            "format": "date-time",
            "description": "Целевая дата/время (ISO 8601)"
          },
          "timezone": {
            "type": "string",
            "default": "Europe/Moscow",
            "description": "IANA TZ из белого списка (РФ + UTC + соседние)"
          },
          "show_days": { "type": "boolean", "default": true },
          "show_hours": { "type": "boolean", "default": true },
          "show_minutes": { "type": "boolean", "default": true },
          "show_seconds": { "type": "boolean", "default": true },
          "bg_color": { "type": "string", "default": "#000000" },
          "digit_color": { "type": "string", "default": "#FFFFFF" },
          "label_color": { "type": "string", "default": "#AAAAAA" },
          "progress_color": { "type": "string", "default": "#0062AD" },
          "label_days": { "type": "string", "default": "Дни" },
          "label_hours": { "type": "string", "default": "Часы" },
          "label_minutes": { "type": "string", "default": "Минуты" },
          "label_seconds": { "type": "string", "default": "Секунды" },
          "layout": {
            "type": "string",
            "enum": ["classic", "compact", "progress"],
            "default": "classic"
          },
          "digit_size": {
            "type": "integer",
            "minimum": 24,
            "maximum": 120,
            "default": 48
          },
          "ended_mode": {
            "type": "string",
            "enum": ["text", "image"],
            "default": "text"
          },
          "ended_text": {
            "type": "string",
            "default": "Акция закончилась"
          },
          "ended_image_url": {
            "type": "string",
            "nullable": true,
            "description": "Обязателен при ended_mode=image (публичный http/https)"
          },
          "captcha_token": {
            "type": "string",
            "nullable": true
          }
        }
      },
      "TimerCreatedResponse": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "gif_url": {
            "type": "string",
            "format": "uri",
            "example": "https://cdn-timer.mailganer.com/t/abc123.gif"
          },
          "jpeg_url": {
            "type": "string",
            "format": "uri",
            "example": "https://cdn-timer.mailganer.com/t/abc123.jpg"
          },
          "expires_at": { "type": "string", "format": "date-time" },
          "html_snippet": {
            "type": "string",
            "description": "Готовый <img> для письма (width = логический 1×)"
          }
        }
      }
    }
  }
}
