<?php

namespace App\Services\Gteam;

use App\Models\Gteam\EnterModel;
use App\Models\Gteam\RoomMemberModel;
use App\Models\Gteam\RoomModel;
use App\Models\User\UserModel;

class RoomService
{

    public function create(array $member, $type)
    {
        $enters = EnterModel::whereIn('id', $member)->get();
        $users = UserModel::whereIn('uid', $enters->pluck('uid'))->pluck('nickname');

        // 创建房间
        $room = RoomModel::create([
            "name" => $users->implode("和") . "的开黑聊天室",
            "type" => $type,
            "number" => $enters->count(),
        ]);
        // 入房
        foreach ($enters as $enter) {
            RoomMemberModel::create([
                "uid" => $enter->uid,
                "room_id" => $room->id,
                "enter_id" => $enter->id,
                "type" => $type,
            ]);
            $enter->match_succ_at = time();
            $enter->room_id = $room->id;
            $enter->save();
        }

        return $room->id;
    }
}
