import { PostDto, VisitType } from "@nice/common";
import { useVisitor } from "@nice/client";
import { Button, Tooltip } from "antd";
import { LikeFilled, LikeOutlined } from "@ant-design/icons";
import { useAuth } from "@web/src/providers/auth-provider";

export default function PostLikeButton({ post }: { post: PostDto }) {
	const { user } = useAuth();
	const { like, unLike } = useVisitor();
	function likeThisPost() {
		if (!post?.liked) {
			post.likes += 1;
			post.liked = true;
			like.mutateAsync({
				data: {
					visitorId: user?.id || null,
					postId: post.id,
					type: VisitType.LIKE,
				},
			});
		} else {
			post.likes -= 1;
			post.liked = false;
			unLike.mutateAsync({
				where: {
					visitorId: user?.id || null,
					postId: post.id,
					type: VisitType.LIKE,
				},
			});
		}
	}
	return (
		<Button
			title={post?.liked ? "取消点赞" : "点赞"}
			type={post?.liked ? "primary" : "default"}
			shape="round"
			style={{
				boxShadow: "none", // 去除阴影
			}}
			icon={post?.liked ? <LikeFilled /> : <LikeOutlined />}
			onClick={(e) => {
				e.stopPropagation();
				likeThisPost();
			}}>
			<span className="mr-1">有帮助</span>
			{post?.likes}
		</Button>
	);
}
