import { isValidObjectId, Types } from 'mongoose';
import User from '@/modules/user/user.model';
import { ApiError } from '@/shared/utils/errors';
import { defaultStatus } from '@/shared/utils/responseCode/httpStatusAlias';
import responseCodes from '@/shared/utils/responseCode/responseCode';

const { PartnerNetworkResponseCodes } = responseCodes;

export const validatePartnerRequestIds = async (
  senderId: Types.ObjectId | string,
  receiverId: Types.ObjectId | string,
): Promise<void> => {
  if (!isValidObjectId(senderId) || !isValidObjectId(receiverId))
    throw new ApiError(
      defaultStatus.OK,
      'Invalid user ID',
      true,
      '',
      PartnerNetworkResponseCodes.PARTNER_NETWORK_ERROR,
    );

  if (senderId.toString() === receiverId.toString())
    throw new ApiError(
      defaultStatus.OK,
      'Sender and receiver cannot be the same',
      true,
      '',
      PartnerNetworkResponseCodes.PARTNER_NETWORK_ERROR,
    );

  const userCount = await User.countDocuments({
    _id: { $in: [senderId, receiverId] },
  });

  if (userCount !== 2)
    throw new ApiError(
      defaultStatus.OK,
      'User not found',
      true,
      '',
      PartnerNetworkResponseCodes.PARTNER_NETWORK_ERROR,
    );
};
