<?php

namespace App\Http\Controllers\Gteam;

use App\Http\Controllers\Miniprogram\Auth;
use App\Http\Resources\Gteam\RoomResource;
use App\Models\Gteam\CommentModel;
use App\Models\Gteam\RoomModel;
use App\Models\Gteam\RoomMemberModel;
use App\Models\Gteam\UserModel;
use App\Models\PartnerModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoomController extends Controller
{
    //
    private $type = [
        "wz" => ["wz_wx", "wz_qq"],
        "cj" => ["cj_wx", "cj_qq"],
    ];

    private $gametype = [
        "cj_wx" => [
            "game" => "cj",
            "area" => "wx",
        ],
        "cj_qq" => [
            "game" => "cj",
            "area" => "qq",
        ],
        "wz_wx" => [
            "game" => "wz",
            "area" => "wx",
        ],
        "wz_qq" => [
            "game" => "wz",
            "area" => "qq",
        ],
    ];


    public function index(Request $request)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'type' => 'required|in:wz,cj',
        ]);
        $builder = RoomModel::whereIn(
            'id',
            RoomMemberModel::where('uid', $uid)->whereIn('type', $this->type[$request->type])->pluck('room_id')
        );
        $rooms = $builder->orderBy('created_at', 'desc')->paginate($request->get('per_page', 20));
        foreach ($rooms as $room) {
            $room->game = $this->gametype[$room->type]['game'];
            $room->area = $this->gametype[$room->type]['area'];
            foreach ($room->members as $member) {
                $user = $member->user;
                $user_info = \App\Models\User\UserModel::select('sex', 'nickname', 'headimgurl')->where(
                    'uid',
                    $user->uid
                )->first();
                $user->nickname = $user_info->nickname;
                $user->headimgurl = $user_info->headimgurl;
                $user->sex = $user_info->sex;
            }
        }
        return response()->json([
            'code' => 200,
            'data' => $rooms
        ]);
    }


    public function show(Request $request, int $id)
    {
        $room = RoomModel::findOrFail($id);
        $room->game = $this->gametype[$room->type]['game'];
        $room->area = $this->gametype[$room->type]['area'];
        $members = $room->members;
        foreach ($members as $member) {
            $user = UserModel::find($member->uid);
            $user_info = \App\Models\User\UserModel::find($member->uid);
            $comments = CommentModel::where(['uid' => $member->uid, 'room_id' => $room->id])->get();
            $member->comments = $comments;
            $member->is_comment = $comments ? 1 : 0;
            $member->nickname = $user_info->nickname;
            $member->headimgurl = $user_info->headimgurl;
            $member->login_at = $user->login_at;
            $member->weixin = $user->weixin;
            $member->qq = $user->qq;
            $member->credit = $user->credit;
            $member->succ_cnt = $user->succ_cnt;
            $member->friend_cnt = $user->friend_cnt;
            $member->cj_wx_rank = $user->cj_wx_rank;
            $member->cj_qq_rank = $user->cj_qq_rank;
            $member->wz_wx_rank = $user->wz_wx_rank;
            $member->wz_qq_rank = $user->wz_qq_rank;
            $member->partner_id = PartnerModel::where('uid', $member->uid)->value('id');
        }
        $room->users = $members;
        return new RoomResource($room);
    }
}
