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

module.exports = (sequelize) => {
	const EmployeeCheckin = sequelize.define(
		"employeecheckin",
		{
			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 },

			log_type: { type: DataTypes.STRING, allowNull: true },
			date: { type: DataTypes.DATEONLY, allowNull: true },
			time: { type: DataTypes.TIME, allowNull: true },
			location: { type: DataTypes.JSON, allowNull: true },
			latitude: { type: DataTypes.STRING, allowNull: true },
			longitude: { type: DataTypes.STRING, allowNull: true },
			distance: { type: DataTypes.BIGINT, allowNull: true },
			accuracy: { type: DataTypes.BIGINT, allowNull: true },
			address: { type: DataTypes.TEXT, allowNull: true },
			status: { type: DataTypes.STRING, allowNull: true },

			// fk
			shifttype_id: { type: DataTypes.INTEGER, allowNull: true },
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			employee_id: { type: DataTypes.INTEGER, allowNull: true },
			holidaylistitem_id: { type: DataTypes.INTEGER, allowNull: true },

			// sappsv1 or erpnext
			ids: { type: DataTypes.INTEGER, allowNull: true },
			import_from: { type: DataTypes.STRING(10), allowNull: true },
		},
		{
			tableName: "employeecheckins",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the EmployeeCheckin model with other models.
	 * @param {object} models - The models to associate with.
	 */
	EmployeeCheckin.associate = (models) => {
		/**
		 * Associates the EmployeeCheckin model with the Employee model.
		 * Defines a many-to-one relationship between EmployeeCheckin and Employee.
		 * @param {object} models - The models to associate with.
		 */
		EmployeeCheckin.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });

		/**
		 * Associates the EmployeeCheckin model with the Company model.
		 * Defines a many-to-one relationship between EmployeeCheckin and Company.
		 * @param {object} models - The models to associate with.
		 */
		EmployeeCheckin.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

		/**
		 * Associates the EmployeeCheckin model with the ShiftType model.
		 * Defines a many-to-one relationship between EmployeeCheckin and ShiftType.
		 * @param {object} models - The models to associate with.
		 */
		EmployeeCheckin.belongsTo(models.ShiftType, { foreignKey: "shifttype_id", as: "shifttype" });

		/**
		 * Associates the EmployeeCheckin model with the HolidayListItem model.
		 * Defines a many-to-one relationship between EmployeeCheckin and HolidayListItem.
		 * @param {object} models - The models to associate with.
		 */
		EmployeeCheckin.belongsTo(models.HolidayListItem, { foreignKey: "holidaylistitem_id", as: "holidaylistitem" });

		EmployeeCheckin.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
	};

	return EmployeeCheckin;
};
