<?php

namespace Extend\TencentIm;

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

/**
 * 账户资料
 */
class Profile extends Base
{
    private string $version = 'v4';

    /**
     * 设置账户资料
     * @param string $user_id 用户id
     * @param array $arg
     * 内置字段：https://cloud.tencent.com/document/product/269/1500#.E6.A0.87.E9.85.8D.E8.B5.84.E6.96.99.E5.AD.97.E6.AE.B5
     * 自定义字段：控制台->应用配置>功能配置->新增
     *  Tag_Profile_Custom_Hname:医院名称
     *  Tag_Profile_Custom_Title:医生职称
     * @return bool
     * @throws GuzzleException
     */
    public function setProfile(string $user_id,array $arg): bool
    {
        try {
            $profileItem = [];
            foreach ($arg as $key => $value){
                $profileItem[] = [
                    'Tag'   => $key,
                    'Value' => $value
                ];
            }

            $options = [
                "json"=> [
                    "From_Account" => $user_id,
                    "ProfileItem" => $profileItem,
                ]
            ];

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

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

    /**
     * 拉取一个账户的多个资料
     * 返回未设置的字段
     * @param string $user_id
     * @param array $arg
     * @return array|string[]
     * @throws GuzzleException
     */
    public function getOneAccountPortraitList(string $user_id,array $arg = ['Tag_Profile_IM_Gender','Tag_Profile_Custom_Hname','Tag_Profile_Custom_Title',"Tag_Profile_IM_Image"]): array
    {
        try {
            $options = [
                "json"=> [
                    "To_Account" => [$user_id],
                    "TagList" => $arg,
                ]
            ];

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

            $result_arg = [];
            if (!empty($result['UserProfileItem'])){
                foreach ($result['UserProfileItem'] as $user_profile_item){
                    if(!empty($user_profile_item['ProfileItem'])){
                        foreach ($user_profile_item['ProfileItem'] as $profile_item){
                            if (!empty($profile_item['Value'])){
                                // 性别为未知时跳过
                                if ($profile_item['Tag'] == "Tag_Profile_IM_Gender" && $profile_item['Value'] == "Gender_Type_Unknown"){
                                    continue;
                                }
                                $result_arg[] = $profile_item['Tag'];
                            }
                        }
                    }
                }
            }

            if (empty($result_arg)){
                return $arg;
            }

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