<?php

namespace app\controller\v2;

use app\extra\basic\Base;
use app\extra\tools\DouyinUserInfo;
use app\middleware\WxMiddleware;
use app\model\blue\BlueCategory;
use app\model\blue\BlueExpertPlatform;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Middleware;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;


#[Controller(prefix: "/wx_v2/platform"),Middleware(WxMiddleware::class)]
class Platform extends Base
{


    /**
     * 用户绑定平台列表
     * @param Request $request
     * @return Response
     */
    #[Route(path: "list",methods: "get")]
    public function getPlatformList(Request $request): Response
    {
        try {
            $platform = (new BlueCategory)->where(["type" =>1,"status" => 1])->with(['platform' => function($query) use($request){
                $query->where("openid",$request->user['openid'])->field("source,nickname,avatar,follower_count,unique_id");
            }])->field("name,icon,sign")->select();
            if ($platform->isEmpty()) return errorTrans("error.data");
            return successTrans("success.data",$platform->toArray());
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    #[Route(path: "bind",methods: "post")]
    public function bindPlatform(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "url.require"       => trans("empty.require"),
                "platform.require"  => trans("empty.require"),
            ],$request->method());
            if (!is_array($param)) return error($param);
            $url = get_first_url($param['url']);
            if (empty($url)) return error("主页链接格式错误");
            $platform = (new BlueExpertPlatform)->where(["openid" => $request->user['openid'],'source' => $param['platform']])->findOrEmpty();
            if (!$platform->isEmpty()) return error("请勿重复绑定");
            switch ($param['platform'])
            {
                case "kuaishou":
                    $userData = [];
                    break;
                case "douyin":
                default:
                    [$state,$platformData] = (new DouyinUserInfo)->getDyInfo($url);
                    if (!$state) return error($platformData);
                    $userData = [
                        "openid"            => $request->user['openid'],
                        "source"            => $param['platform'],
                        "nickname"          => $platformData['nickname'],
                        "avatar"            => $platformData['avatar_larger']['url_list'][0]??'',
                        "aweme_count"       => $platformData['aweme_count'],
                        "follower_count"    => $platformData['follower_count'],
                        "sec_uid"           => $platformData['sec_uid'],
                        "signature"         => $platformData['signature'],
                        "unique_id"         => $platformData['unique_id'],
                        "uid"               => $platformData['uid'],
                        "total_favorited"   => $platformData['total_favorited'],
                        "url"               => $url,
//                        "star_id"           => $platformData['']
                    ];
                    break;
            }
            $state = $platform->insertGetId($userData);
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            echo $throwable->getLine()."\n";
            echo $throwable->getFile()."\n";
            return error($throwable->getMessage());
        }
    }

}