const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
	const TrackingLocation = sequelize.define(
		"trackinglocation",
		{
			id: {
				type: DataTypes.INTEGER,
				autoIncrement: true,
				primaryKey: true,
			},
			creation: { type: DataTypes.DATE, allowNull: false },
			modified: { type: DataTypes.DATE, allowNull: false },
			modified_by: { type: DataTypes.INTEGER, allowNull: false },
			owner: { type: DataTypes.INTEGER, allowNull: false },

			// main
			browser_name: { type: DataTypes.STRING(50), allowNull: true },
			browser_version: { type: DataTypes.STRING(50), allowNull: true },
			platform: { type: DataTypes.STRING(50), allowNull: true },
			user_agent: { type: DataTypes.STRING(50), allowNull: true },
			device_type: { type: DataTypes.STRING(50), allowNull: true },
			ip_address: { type: DataTypes.STRING(50), allowNull: true },
			battery_level: { type: DataTypes.STRING(50), allowNull: true },
			battery_charging: { type: DataTypes.BOOLEAN, allowNull: true },
			device_id: { type: DataTypes.STRING(50), allowNull: true },

			latitude: { type: DataTypes.DECIMAL(10, 8), allowNull: true },
			longitude: { type: DataTypes.DECIMAL(11, 8), allowNull: true },

			date: { type: DataTypes.DATEONLY, allowNull: true },
			timestamp: { type: DataTypes.DATE, allowNull: true },

			// fk
			user_id: { type: DataTypes.INTEGER, allowNull: true },
			employee_id: { type: DataTypes.INTEGER, allowNull: true },
			company_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "trackinglocations",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the TrackingLocation model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	TrackingLocation.associate = (models) => {
		TrackingLocation.belongsTo(models.Company, {
			foreignKey: "company_id",
			as: "company",
		});

		TrackingLocation.belongsTo(models.Employee, {
			foreignKey: "employee_id",
			as: "employee",
		});

		TrackingLocation.belongsTo(models.User, {
			foreignKey: "user_id",
			as: "user",
		});
	};

	return TrackingLocation;
};
