<?php
declare(strict_types=1);

namespace app\api\controller;

use app\api\util\ApiLang;
use plugin\saiadmin\basic\OpenController;
use support\Response;

/**
 * API 控制器基类：根据请求头 lang（en=英文，zh=中文）对返回 message 做双语适配
 */
class BaseController extends OpenController
{
    /**
     * 成功返回，message 按请求头 lang（en/zh）翻译
     */
    public function success(array|string $data = [], string $msg = 'success', int $option = JSON_UNESCAPED_UNICODE): Response
    {
        if (is_string($data)) {
            $msg = $data;
            $data = [];
        }
        $msg = ApiLang::translate((string) $msg);
        return parent::success($data, $msg, $option);
    }

    /**
     * 失败返回，message 按 lang 翻译
     */
    public function fail(string $msg = 'fail', int $code = 400): Response
    {
        $msg = ApiLang::translate($msg);
        return parent::fail($msg, $code);
    }
}
