import {Mongo} from "meteor/mongo";
import {Meteor} from "meteor/meteor";
import {check} from "meteor/check";
import {Assets} from "/imports/api/assets";
import {Students} from "/imports/api/students";
import {Staff} from "/imports/api/staff";
import {AssetTypes} from "/imports/api/asset-types";

export const AssetAssignmentHistory = new Mongo.Collection('assetAssignmentHistory');

/*
Maintains a historical record of asset assignments.
assetKey: The MongoID of the asset.  AssetID's could be reused, so this prevents that eventuality from messing up historical records.
assetId: The asset's assigned ID (not a MongoID).
serial: The asset's serial number (part of the device, or defined by the manufacturer).  In some cases this may be a partial serial.  It is not a unique identifier, but should be mostly unique.  This might be undefined if a serial was never provided for the original asset.
assetTypeName: The name of the asset type, or "UNK" if one could not be found (shouldn't happen).  This is stored because these records could be kept longer than the assets in the system.
assigneeType: One of 'Student' or 'Staff'.
assigneeId: The MongoID of the student or staff the asset was assigned to.
startDate: The date/time of the assignment.
endDate: The date/time of the unassignment.
comment: A text block detailing the reason for the unassignment of the device.  Eg: 'Broke Screen' or 'End of year' or 'Not charging' or 'Needs replacement'.
startCondition: One of the condition options: [New, Like New, Good, Okay, Damaged] (see assets.unassign for details).
endCondition: One of the condition options: [New, Like New, Good, Okay, Damaged] (see assets.unassign for details).
startConditionDetails: An optional text block for details on the condition.
endConditionDetails: An optional text block for details on the condition.
 */


if (Meteor.isServer) {
	Meteor.methods({
		/**
		 * Collects historical data on asset assignments.
		 * @param params An object with a single attribute.  The attribute must be one of: assetId, serial, staffId, or studentId.  It will find all Assignment data for the given attribute value.
		 * @returns {any} Array of Asset Assignment History objects.
		 */
		'AssetAssignmentHistory.get'(params) {
			if(Roles.userIsInRole(Meteor.userId(), "laptop-management", {anyScope:true})) {
				let query = {};

				if(params.studentId) check(params.studentId, String)
				if(params.staffId) check(params.staffId, String)
				if(params.assetId) check(params.assetId, String)
				if(params.serial) check(params.serial, String)

				if(params.serial) query.serial = params.serial;
				else if(params.assetId) query.assetId = params.assetId;
				else if(params.studentId) {
					query.assigneeId = params.studentId
					query.assigneeType = "Student"
				}
				else if(params.staffId) {
					query.assigneeId = params.staffId
					query.assigneeType = "Staff"
				}
				else {
					query = undefined;
				}
				
				if(query) {
					//Sort by the last time the record was updated from most to least recent.
					let result = AssetAssignmentHistory.find(query, {sort: {endDate: -1}}).fetch();

					//Add some additional data to the records.
					for(let next of result) {
						if(next.serial) {
							next.asset = Assets.findOne({serial: next.serial});
						}

						if(next.asset) {
							next.assetType = AssetTypes.findOne({_id: next.asset.assetType})
						}

						if(next.assigneeId) {
							next.assignee = next.asset.assigneeType === "Student" ? Students.findOne({_id: next.assigneeId}) : Staff.findOne({_id: next.assigneeId})
						}
					}

					return result;
				} else return null;
			}
			else return null;
		}
	});
}