import { pushUserToUserList } from "@/services/user-list/push.js";
import { UserLists, UserListJoinings, Blockings, Followings } from "@/models/index.js";
import define from "../../../define.js";
import { ApiError } from "../../../error.js";
import { getUser } from "../../../common/getters.js";

export const meta = {
	tags: ["lists", "users"],

	requireCredential: true,

	kind: "write:account",

	description: "Add a user to an existing list.",

	errors: {
		noSuchList: {
			message: "No such list.",
			code: "NO_SUCH_LIST",
			id: "2214501d-ac96-4049-b717-91e42272a711",
		},

		noSuchUser: {
			message: "No such user.",
			code: "NO_SUCH_USER",
			id: "a89abd3d-f0bc-4cce-beb1-2f446f4f1e6a",
		},

		alreadyAdded: {
			message: "That user has already been added to that list.",
			code: "ALREADY_ADDED",
			id: "1de7c884-1595-49e9-857e-61f12f4d4fc5",
		},

		youHaveBeenBlocked: {
			message:
				"You cannot push this user because you have been blocked by this user.",
			code: "YOU_HAVE_BEEN_BLOCKED",
			id: "990232c5-3f9d-4d83-9f3f-ef27b6332a4b",
		},

		notFollowing: {
			message:
				"You cannot push this user because you are not following this user.",
			code: "NOT_FOLLOWING",
			id: "0a2e4d73-fe61-41fb-822c-d365ec81ba2a",
		},
	},
} as const;

export const paramDef = {
	type: "object",
	properties: {
		listId: { type: "string", format: "misskey:id" },
		userId: { type: "string", format: "misskey:id" },
	},
	required: ["listId", "userId"],
} as const;

export default define(meta, paramDef, async (ps, me) => {
	// Fetch the list
	const userList = await UserLists.findOneBy({
		id: ps.listId,
		userId: me.id,
	});

	if (!userList) {
		throw new ApiError(meta.errors.noSuchList);
	}

	// Fetch the user
	const user = await getUser(ps.userId).catch((e) => {
		if (e.id === "15348ddd-432d-49c2-8a5a-8069753becff")
			throw new ApiError(meta.errors.noSuchUser);
		throw e;
	});

	// Check blocking and following status
	if (user.id !== me.id) {
		const isBlocked = await Blockings.exist({
			where: {
				blockerId: user.id,
				blockeeId: me.id,
			},
		});
		const isFollowed = await Followings.exist({
			where: {
				followerId: me.id,
				followeeId: user.id,
			},
		});
		if (isBlocked) {
			throw new ApiError(meta.errors.youHaveBeenBlocked);
		}
		if (!isFollowed) {
			throw new ApiError(meta.errors.notFollowing);
		}
	}

	const exist = await UserListJoinings.exist({
		where: {
			userListId: ps.listId,
			userId: user.id,
		},
	});

	if (exist) {
		throw new ApiError(meta.errors.alreadyAdded);
	}

	// Push the user
	await pushUserToUserList(user, userList);
});
