<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\BaseDictionaryOptionModel;

use App\Models\DreamInfoModel;
use App\Models\SupportDreamModel;
use App\Models\UserCareUser;
use App\Models\UserInfoModel;
use Illuminate\Http\Request;
use App\Services\Base\ErrorCode;
class HomeController extends Controller
{
    /**
     * @api {get} /api/user/show/ 用户信息
     * @apiDescription 用户信息
     * @apiGroup Home
     * @apiPermission Passport
     * @apiVersion 0.1.0
     * @apiParam {int} user_id 用户ID
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     *{
     *   "status": true,
     *   "status_code": 0,
     *   "message": "",
     *  "data": {
     *      "user": {},             用户信息
     *      "near_dream": {},       当前梦想
     *      "sup_dreams": [],       支持的梦想
     *      "imgs": [],         封面图片
     *      "dreams": []            曾经的梦想
     *      "score": 2000           支持分数
     *  }
     *}
     * @apiErrorExample {json} Error-Response:
     *HTTP/1.1 400 Bad Request
     *{
     *  "status": false,
     *  " status_code": 1105,
     *  "message": "用户不存在",
     *  "data": null
     *  }
     */
    public function show(Request $request)
    {
        $user_id = $request->user_id;
//        获得的支持分数
        $info = SupportDreamModel::where('to_user_id',$user_id)->get();
        $score = 0;
        foreach ($info as $item) {
            $score += $item->score;
        }
        $care = UserCareUser::where('user_id',$user_id)->get();
        $fens = UserCareUser::where('other_user_id',$user_id)->get();
        $user = UserInfoModel::find($user_id);
        if (count($user) == 0) return $this->error(ErrorCode::USER_DOES_NOT_EXIST);
        $emotion = BaseDictionaryOptionModel::where(['dictionary_code' => 'emotion'])->
            where('dictionary_table_code','user_info')->
        where(['value' => $user->emotion])->first();
        $emotion = count($emotion) > 0 ? $emotion->name : '';
//        当前梦想
        $near_dream =DreamInfoModel::where('user_id',$user->id)->orderBy('id','desc')->first();
//         封面图片
        $imgs =$user->allImgs;
//        曾经的梦想
        $dreams = $user->UserDream;

        foreach ($dreams as $dream){
            $dream->dream_imgs = $dream->imgs;
        }

        $user->score = 1000;//自定义 算法
        $user->care = count($care);
        $user->fens = count($fens);
        $user->dreams =$dreams;
        $user->emotion = $emotion;
//         支持的梦想
        $sup_dreams = $user->supDream;

        foreach ($sup_dreams as $sup_dream){
            $sup_dream->pics = $sup_dream->imgs;
        }

        return $this->api(compact('user','near_dream','sup_dreams','imgs','dreams','score'));

    }
}
