import {Mongo} from "meteor/mongo";
import {Meteor} from "meteor/meteor";
import { check } from 'meteor/check';
import { Roles } from 'meteor/alanning:roles';
import SimpleSchema from "simpl-schema";

//
// An asset type is a specific type of equipment.  Example: Lenovo 100e Chromebook.
//
export const AssetTypes = new Mongo.Collection('assetType');
/*

const AssetTypesSchema = new SimpleSchema({
	name: {
		type: String,
		label: "Model Name",
		optional: false,
		trim: true,
	},
	description: {
		type: String,
		label: "Description",
		optional: true,
		trim: true,
		defaultValue: ""
	},
	hasSerial: {
		type: Boolean,
		label: "Is a serial number available for all instances?",
		optional: false,
		defaultValue: false
	},
});

AssetTypes.attachSchema(AssetTypesSchema);
 */

if (Meteor.isServer) {
	// Drop any old indexes we no longer will use.  Create indexes we need.
	try {AssetTypes._dropIndex("name")} catch(e) {}
	//AssetTypes.createIndex({name: "text"}, {name: "name", unique: false});
	AssetTypes.createIndex({id: 1}, {name: "External ID", unique: true});

	//Debug: Show all indexes.
	// AssetTypes.rawCollection().indexes((err, indexes) => {
	// 	console.log(indexes);
	// });

	// This code only runs on the server
	Meteor.publish('assetTypes', function() {
		return AssetTypes.find({}, {sort: {name: 1}});
	});
}
Meteor.methods({
	'assetTypes.add'(name, description, hasSerial) {
		check(name, String);
		check(description, String);
		check(hasSerial, Boolean);

		if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
			AssetTypes.insert({name, description, hasSerial});
		}
	},
	'assetTypes.remove'(_id) {
		check(_id, String);

		if(Roles.userIsInRole(Meteor.userId(), "admin", {anyScope:true})) {
			//TODO: Need to first verify there are no checked out assets to the staff member.
		}
	},
});

