<?php

namespace Extend\TencentIm;

use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use GuzzleHttp\Exception\GuzzleException;

/**
 * 好友
 */
class Friend extends Base
{
    private string $version = 'v4';

    /**
     * 添加好友
     * 单个好友添加
     * @param string $user_id
     * @param string $friend_user_id
     * @param string $group_name
     * @return array
     * @throws GuzzleException
     */
    public function addFriend(string $user_id,string $friend_user_id,string $group_name): array
    {
        try {
            $options = [
                "json"=> [
                    "From_Account" => $user_id,
                    "AddFriendItem" => [
                        [
                            "To_Account" => $friend_user_id,
                            "AddSource" => "AddSource_Type_Applets", // 加好友来源字段
                        ]
                    ],
                    "ForceAddFlags" => 1, // 管理员强制加好友标记：1表示强制加好友，0表示常规加好友方式
                ]
            ];

            $path = $this->config['base_url'] . $this->version . "/profile/portrait_set?" . $this->buildRequestParams();
            $result = $this->postRequest($path,$options);

            if (isset($result['Fail_Account'])){
                // 返回处理失败的用户列表，仅当存在失败用户时才返回该字段
                throw new BusinessException("添加好友异常,未返回结果:" . json_encode($result['ResultItem'],JSON_UNESCAPED_UNICODE));
            }

            return $result;
        }catch (\Exception $e) {
            throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR);
        }
    }
}