// base class for many tests of the "PUT /codemarks/unfollow/:id" requests

'use strict';

const BoundAsync = require(process.env.CSSVC_BACKEND_ROOT + '/shared/server_utils/bound_async');
const CodeStreamAPITest = require(process.env.CSSVC_BACKEND_ROOT + '/api_server/lib/test_base/codestream_api_test');
const DeepClone = require(process.env.CSSVC_BACKEND_ROOT + '/shared/server_utils/deep_clone');

class CommonInit {

	init (callback) {
		this.teamOptions.creatorIndex = 1;
		/*
		this.streamOptions.creatorIndex = 1;
		this.streamOptions.type = this.streamType || 'channel';
		this.streamOptions.isTeamStream = this.isTeamStream || false;
		*/
		Object.assign(this.postOptions, {
			numPosts: 1,
			creatorIndex: 1,
			wantCodemark: true,
			wantMarker: true
		});

		BoundAsync.series(this, [
			CodeStreamAPITest.prototype.before.bind(this),
			this.followCodemark,	// first follow the codemark
			this.makeTestData		// make the data to use when issuing the test request
		], callback);
	}

	// follow the codemark so we can unfollow in the test
	followCodemark (callback) {
		this.codemark = this.postData[0].codemark;
		if (this.skipFollow) { return callback(); }
		this.doApiRequest(
			{
				method: 'put',
				path: `/codemarks/follow/${this.codemark.id}`,
				token: this.currentUser.accessToken
			},
			callback
		);
	}

	// make the data to use when issuing the test request
	makeTestData (callback) {
		this.expectedResponse = {
			codemark: {
				_id: this.codemark.id,	// DEPRECATE ME
				id: this.codemark.id,
				$set: {
					version: this.expectedVersion,
					modifiedAt: Date.now() // placeholder
				},
				$pull: {
					followerIds: this.currentUser.user.id
				},
				$version: {
					before: this.expectedVersion - 1,
					after: this.expectedVersion
				}
			}
		};
		this.modifiedAfter = Date.now();
		this.path = `/codemarks/unfollow/${this.codemark.id}`;
		this.expectedCodemark = DeepClone(this.codemark);
		Object.assign(this.expectedCodemark, this.expectedResponse.codemark.$set);
		this.expectedCodemark.followerIds = [this.users[1].user.id];
		callback();
	}

	// perform the actual unfollow
	unfollowCodemark (callback) {
		this.doApiRequest(
			{
				method: 'put',
				path: `/codemarks/unfollow/${this.codemark.id}`,
				token: this.currentUser.accessToken
			},
			(error, response) => {
				if (error) { return callback(error); }
				this.message = response;
				callback();
			}
		);
	}
}

module.exports = CommonInit;
