<?php
namespace app\api\controller;
use Shopwwi\WebmanFilesystem\FilesystemFactory;
use Shopwwi\WebmanFilesystem\Facade\Storage;
use app\model\User as UserModel;
use app\model\Realname as RealnameModel;
use app\model\FriendCircle as FriendCircleModel;
use app\model\FriendCircleLike as FriendCircleLikeModel;
use app\model\FriendCircleComment as FriendCircleCommentModel;
use support\Request;
use support\Response;
use taoser\facade\Validate;
use support\think\Db;
use hg\apidoc\annotation as Apidoc;

/**
 * 朋友圈
 */
class FriendCircleController extends BaseController{
    /**
     * 不需要鉴权的方法
     * @var array
     */
    public $noNeedAuth = ['*'];

    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    public $noNeedLogin = [];
    /**
     * 朋友圈设置
     * @return void
     */
    function info(Request $request): Response{
        $res = $this->newcount($request);
        $response = $res->rawBody();
        $json = json_decode($response,true);
        $json['data']['settings'] = [
            'bg' => '',
        ];
        $json['data']['last_unread_item'] = [
            'user_id' => '1',
            'avatar' => '',
            'nickname' => '',
        ];
        $res->withBody(json_encode($json));
        return $res;
    }
    /**
     * @Apidoc\Title("列表")
     * @Apidoc\Method("GET")
     * @Apidoc\Query("page", type="int", require=true, desc="页码",default=1)
     * @Apidoc\Query("limit", type="int", require=true, desc="分页大小",default=10)
     * @Apidoc\Query("user_id", type="int", require=false, desc="用户ID，不传则获取所有")
     */
    function list(Request $request): Response
    {
        $page = (int)$request->get('page', 1);
        $limit = (int)$request->get('limit', 10);
        $user_id = $request->get('user_id', 0);
        
        $query = FriendCircleModel::where('status', 1)
            ->with(['user' => function($query) {
                $query->field('id,nickname,avatar');
            }])
            ->order('created_at', 'desc');
        
        // 如果指定了用户ID，只获取该用户的朋友圈
        if ($user_id > 0) {
            $query->where('user_id', $user_id);
        }
        
        $list = $query->paginate([
            'list_rows' => $limit,
            'page' => $page,
        ]);
        
        $current_user = \support\Jwt::getUser();
        $current_user_id = $current_user ? $current_user->id : 0;
        
        // 处理每条朋友圈数据
        $items = $list->items();
        $list->each(function($item) use ($current_user_id){
            // 获取点赞列表
            $likes = FriendCircleLikeModel::where('circle_id', $item->id)
                ->with(['user' => function($query) {
                    $query->field('id,nickname,avatar');
                }])
                ->order('created_at', 'desc')
                ->limit(20)
                ->select();
            
            // 检查当前用户是否已点赞
            $is_liked = false;
            if ($current_user_id > 0) {
                $is_liked = FriendCircleLikeModel::where('circle_id', $item->id)
                    ->where('user_id', $current_user_id)
                    ->count() > 0;
            }
            
            // 获取评论列表（最新10条）
            $comments = FriendCircleCommentModel::where('circle_id', $item->id)
                ->where('status', 1)
                ->with(['user' => function($query) {
                    $query->field('id,nickname,avatar');
                }, 'replyUser' => function($query) {
                    $query->field('id,nickname,avatar');
                }])
                ->order('created_at', 'asc')
                ->limit(10)
                ->select();
            
            // 格式化数据
            $item->is_liked = $is_liked;
            $item->likes = $likes;
            $item->comments = $comments;
            
            // 处理图片URL
            if (!empty($item->files)) {
                $files = is_array($item->files) ? $item->files : json_decode($item->files, true);
                if (is_array($files)) {
                    $item->files = array_map(function($file) {
                        return cdnurl($file);
                    }, $files);
                } else {
                    $item->files = [];
                }
            } else {
                $item->files = [];
            }
            
            // 处理用户头像
            if ($item->user && $item->user->avatar) {
                $item->user->avatar = cdnurl($item->user->avatar);
            }
            
            // 处理点赞用户头像
            foreach ($item->likes as $like) {
                if ($like->user && $like->user->avatar) {
                    $like->user->avatar = cdnurl($like->user->avatar);
                }
            }
            
            // 处理评论用户头像
            foreach ($item->comments as $comment) {
                if ($comment->user && $comment->user->avatar) {
                    $comment->user->avatar = cdnurl($comment->user->avatar);
                }
                if ($comment->replyUser && $comment->replyUser->avatar) {
                    $comment->replyUser->avatar = cdnurl($comment->replyUser->avatar);
                }
            }
            return $item;
        });
        
        return $this->success('ok', $list);
    }
    
    /**
     * @Apidoc\Title("最近更新的数量")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("last_see", type="string",require=false, desc="最近查看的时间戳")
     */
    function newcount(Request $request): Response
    {
        $user = \support\Jwt::getUser();
        if (!$user) {
            return $this->fail('请先登录');
        }
        
        $last_see = $request->post('last_see');
        if (empty($last_see)) {
            $last_see = cache('last_see_' . $user->id);
        }
        
        if (empty($last_see)) {
            $last_see = time() - 86400; // 默认24小时前
        } else {
            // 保存最后查看时间
            cache('last_see_' . $user->id, $last_see);
        }
        
        // 统计从上次查看时间到现在新增的朋友圈数量
        $count = FriendCircleModel::where('status', 1)
            ->where('created_at', '>', $last_see)
            ->count();
        
        return $this->success('ok', ['count' => $count]);
    }
    
    /**
     * @Apidoc\Title("发布朋友圈")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("body", type="string",require=false, desc="内容")
     * @Apidoc\Param("files", type="string",require=false, desc="图片列表(JSON数组)")
     */
    function create(Request $request): Response
    {
        $user = \support\Jwt::getUser();
        if (!$user) {
            return $this->fail('请先登录');
        }
        
        $body = $request->post('content', '');
        $files = $request->post('files', '');
        $address = $request->post('address', '');
        $releaseType = $request->post('releaseType', '');
        // 验证内容
        if (empty($body)) {
            return $this->fail('什么内容都木有啊');
        }
        
        
        // 处理图片列表
        $files_array = [];
        if (!empty($files)) {
            if (is_string($files)) {
                $files_array = json_decode($files, true);
            } elseif (is_array($files)) {
                $files_array = $files;
            }
            
            if (!is_array($files_array)) {
                return $this->fail('图片列表格式错误');
            }
            
            // 限制图片数量
            if (count($files_array) > 9) {
                return $this->fail('最多只能上传9张图片');
            }
        }
        
        // 创建朋友圈动态
        $circle = FriendCircleModel::create([
            'user_id' => $user->id,
            'releaseType' => $releaseType,
            'body' => $body,
            'files' => $files_array,
            'address' => $address,
            'status' => 1,
        ]);
        
        return $this->success('发布成功', ['id' => $circle->id]);
    }
    
    /**
     * @Apidoc\Title("发表评论")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("body", type="string",require=true, desc="内容")
     * @Apidoc\Param("id", type="int",require=true, desc="朋友圈动态ID")
     * @Apidoc\Param("reply_user_id", type="int",require=false, desc="回复的用户ID(回复评论时使用)")
     */
    function comment(Request $request): Response
    {
        $user = \support\Jwt::getUser();
        if (!$user) {
            return $this->fail('请先登录');
        }
        
        $body = $request->post('body', '');
        $circle_id = (int)$request->post('id', 0);
        $reply_user_id = (int)$request->post('reply_user_id', 0);
        
        if (empty($body)) {
            return $this->fail('评论内容不能为空');
        }
        
        if ($circle_id <= 0) {
            return $this->fail('朋友圈动态ID错误');
        }
        
        // 检查朋友圈动态是否存在
        $circle = FriendCircleModel::where('id', $circle_id)
            ->where('status', 1)
            ->find();
        
        if (!$circle) {
            return $this->fail('朋友圈动态不存在');
        }
        
        // 如果回复评论，检查被回复的用户是否存在
        if ($reply_user_id > 0) {
            $reply_user = UserModel::where('id', $reply_user_id)->find();
            if (!$reply_user) {
                return $this->fail('被回复的用户不存在');
            }
        }
        
        // 创建评论
        $comment = FriendCircleCommentModel::create([
            'circle_id' => $circle_id,
            'user_id' => $user->id,
            'reply_user_id' => $reply_user_id,
            'body' => $body,
            'status' => 1,
        ]);
        
        // 更新朋友圈评论数
        $circle->comment_count = FriendCircleCommentModel::where('circle_id', $circle_id)
            ->where('status', 1)
            ->count();
        $circle->save();
        
        return $this->success('评论成功', ['id' => $comment->id]);
    }
    
    /**
     * @Apidoc\Title("点赞")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int",require=true, desc="朋友圈动态ID")
     */
    function like(Request $request): Response
    {
        $user = \support\Jwt::getUser();
        if (!$user) {
            return $this->fail('请先登录');
        }
        
        $circle_id = (int)$request->post('id', 0);
        
        if ($circle_id <= 0) {
            return $this->fail('朋友圈动态ID错误');
        }
        
        // 检查朋友圈动态是否存在
        $circle = FriendCircleModel::where('id', $circle_id)
            ->where('status', 1)
            ->find();
        
        if (!$circle) {
            return $this->fail('朋友圈动态不存在');
        }
        
        // 检查是否已点赞
        $like = FriendCircleLikeModel::where('circle_id', $circle_id)
            ->where('user_id', $user->id)
            ->find();
        
        if ($like) {
            // 取消点赞
            $like->delete();
            $circle->like_count = max(0, $circle->like_count - 1);
            $circle->save();
            return $this->success('取消点赞成功', ['is_liked' => false]);
        } else {
            // 添加点赞
            FriendCircleLikeModel::create([
                'circle_id' => $circle_id,
                'user_id' => $user->id,
            ]);
            $circle->like_count = $circle->like_count + 1;
            $circle->save();
            return $this->success('点赞成功', ['is_liked' => true]);
        }
    }
}