<?php

declare(strict_types=1);

namespace PortalPeople\mail\controllers\api;

use PortalPeople\mail\Module;
use Yii;
use yii\rest\Controller;

/**
 * @OA\Tag(
 *     name="Mail",
 *     description="Mail queue status and management"
 * )
 */
class NgaugeController extends Controller
{
    /**
     * @OA\Get(
     *     path="/v1/ngauge/pending-mail",
     *     tags={"Mail"},
     *     summary="Count of pending unsent messages in the mail queue",
     *     @OA\Response(
     *         response=200,
     *         description="Pending message count",
     *         @OA\JsonContent(type="integer")
     *     ),
     *     @OA\Response(response=503, description="Module unavailable")
     * )
     */
    public function actionPendingMail(): int|string
    {
        /** @var Module|null $module */
        $module = Yii::$app->getModule('ngauge-mail');

        if ($module === null || !method_exists($module, 'getMailQueueService')) {
            Yii::error('ngauge-mail module unavailable in NgaugeController::actionPendingMail', __CLASS__);
            Yii::$app->response->statusCode = 503;
            return 0;
        }

        return $module->getMailQueueService()->pendingCount();
    }
}
