const { setSurvey } = require('../../connection');
const { project } = require('../../utils');

async function get (projectId, sequence, point, opts = {}) {

	const client = await setSurvey(projectId);

	async function query (table='raw_shots') {
		const text = `
			SELECT
				sequence, line, point, objref, tstamp, hash, ST_Transform(geometry, 4326)::json as geometry, meta
			FROM ${table}
			WHERE sequence = $1 and point = $2;
		`;

		const values = [sequence, point];
		const res = await client.query(text, values);
		return res.rows;
	}

	let res;

	if (opts.kind=="final") {
		res = project(await query("final_shots"), opts.project);
	} else {
		res = project(await query("raw_shots"), opts.project);
	}

	client.release();

	return res[0];
}

module.exports = get;
