<?php

namespace App\Models;

use App\Services\ChatService;
use cccdl\yunxin_sdk\Im\Msg;
use cccdl\yunxin_sdk\Im\Team;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class GroupUser extends BaseModel
{
    use HasFactory;

    const TYPE_NORMAL = 0;
    const TYPE_OWNER = 1;
    const TYPE_MANAGE = 2;

    public function chatUser()
    {
        return $this->hasOne(WangYiYun::class, 'user_id', 'user_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public static function addGroupUser($group, $service_user, $role_ids)
    {
        $imTeam = new Team(config('chat.im.app_id'), config('chat.im.app_secret'));
        $owner_accid = make_wangyiyun_accid($group->owner_id);
        $user_accid = make_wangyiyun_accid($service_user->user_id);
        //拉人入群
        $check = GroupUser::where('group_id', $group->id)->where('user_id', $service_user->user_id)->first();
        Log::info("addGroupUser group:" . json_encode($group) . "| service_user:" . json_encode($service_user) . "|owner_accid:" . $owner_accid . "|user_accid:" . $user_accid . "|check:" . $check);

        $role = ServiceRole::where('id', $role_ids[0] ?? 0)->first();
        $imTeam->add($group->im_chat_id, $owner_accid, [$user_accid], 0, '邀请进群');
        if (empty($check)) {
            //            $role = ServiceRole::where('id',$role_ids[0]??0)->first();
//            $imTeam->add($group->im_chat_id,$owner_accid,[$user_accid],0,'邀请进群');

            $group_user_map['group_id'] = $group->id;
            $group_user_map['user_id'] = $service_user->user_id;
            $group_user_map['role_id'] = $role->id ?? null;
            $group_user_map['role_name'] = $role->name ?? null;
            $group_user_map['nickname'] = $service_user->name ?? null;
            $group_user_map['type'] = GroupUser::TYPE_NORMAL;
            GroupUser::create($group_user_map);
        } else {
            GroupUser::where('group_id', $group->id)->where('user_id', $service_user->user_id)->update(['role_id' => $role->id ?? null, 'role_name' => $role->name ?? null, 'nickname' => $service_user->name ?? null, 'type' => GroupUser::TYPE_NORMAL]);
        }
        $map = [];
        $order_role_ids = ServiceRoleOrder::where("order_id", $group->order_id)->where("user_id", $service_user->user_id)->pluck("role_id")->toArray();
        $map['role'] = $order_role_ids;
        $options = ['custom' => json_encode($map), 'nick' => $service_user->name];
        $imTeam->updateTeamNick($group->im_chat_id, $owner_accid, $user_accid, $options);
        $order = Order::where('id', operator: $group->order_id)->first();
        //查看是否存在三个身份数据
        // $count = ServiceRoleOrder::where('order_id',$group->order_id)
        //     ->whereIn('role_id',[ServiceRole::MAIN_COACH,ServiceRole::COACH,ServiceRole::CUSTOMER])
        //     ->pluck('user_id')->unique()->count();
        // if($count == 3){
        //     $chatService = new ChatService();
        //     $chatService->sendImMsgUserToUser($group->order_id,'系统通知','请所有服务人员与用户'.$order->name.'进行第一次视频通话','https://image.fulllinkai.com/202403/29/dea3e0c27107cdf178635d2a41199e5e.png','',[ServiceRole::MAIN_COACH,ServiceRole::COACH,ServiceRole::CUSTOMER]);
        // }
        // 群公告通知
        // $imMsg = new Msg(config('chat.im.app_id'), config('chat.im.app_secret'));
        // $imMsg->sendMsg($owner_accid,1,$group->im_chat_id,0,json_encode(['msg'=>$group->announcement]));
        // 系统通知 通知已入群
        $chatService = new ChatService();
        $chatService->sendImMsgUserToUserV2($group->order_id, $service_user->user_id, '系统通知', '您已进入新用户[' . $order->name . ']服务群，请关注群聊信息，感谢！', 'https://image.fulllinkai.com/202403/29/dea3e0c27107cdf178635d2a41199e5e.png', 'yfheal://app/push/IMGroup');
    }
}
