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

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

			date: { type: DataTypes.DATEONLY, allowNull: false },
			in_time: { type: DataTypes.TIME, allowNull: true },
			out_time: { type: DataTypes.TIME, allowNull: true },
			in_status: { type: DataTypes.STRING, allowNull: true },
			out_status: { type: DataTypes.STRING, allowNull: true },
			in_distance: { type: DataTypes.STRING, allowNull: true },
			out_distance: { type: DataTypes.STRING, allowNull: true },
			in_address: { type: DataTypes.TEXT, allowNull: true },
			out_address: { type: DataTypes.TEXT, allowNull: true },
			total_hour: { type: DataTypes.FLOAT, allowNull: true },
			in_latitude: { type: DataTypes.STRING, allowNull: true },
			in_longitude: { type: DataTypes.STRING, allowNull: true },
			out_latitude: { type: DataTypes.STRING, allowNull: true },
			out_longitude: { type: DataTypes.STRING, allowNull: true },
			reason: { type: DataTypes.TEXT, allowNull: true },
			notes: { type: DataTypes.TEXT, allowNull: true },
			status_absenceclarification: { type: DataTypes.BOOLEAN, allowNull: true },
			status_working_in_dayoff: { type: DataTypes.BOOLEAN, allowNull: true },
			status_permitease: { type: DataTypes.BOOLEAN, allowNull: true },
			filename: { type: DataTypes.STRING, allowNull: true },
			half_day: { type: DataTypes.BOOLEAN, allowNull: true },

			need_clarification: { type: DataTypes.BOOLEAN, allowNull: true },

			company_id: { type: DataTypes.INTEGER, allowNull: false },
			employee_id: { type: DataTypes.INTEGER, allowNull: false },
			shifttype_id: { type: DataTypes.INTEGER, allowNull: true },
			leavetype_id: { type: DataTypes.INTEGER, allowNull: true },
			leaveapplication_id: { type: DataTypes.INTEGER, allowNull: true },
			attendancerequest_id: { type: DataTypes.INTEGER, allowNull: true },
			permitease_id: { type: DataTypes.INTEGER, allowNull: true },
			absenceclarificationchild_id: { type: DataTypes.INTEGER, allowNull: true },
			absenceclarification_id: { type: DataTypes.INTEGER, allowNull: true },
			holidaylistitem_id: { type: DataTypes.INTEGER, allowNull: true },

			// New fields for robust synchronization (added via migration)
			is_attendance_request: { type: DataTypes.BOOLEAN, allowNull: true, defaultValue: null },
			is_leave: { type: DataTypes.BOOLEAN, allowNull: true, defaultValue: null },
			is_absence_clarification: { type: DataTypes.BOOLEAN, allowNull: true, defaultValue: null },

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

	/**
	 * Associates the Attendance model with other models.
	 * @param {object} models - The models to associate with.
	 */
	Attendance.associate = (models) => {
		Attendance.belongsTo(models.Employee, { foreignKey: "employee_id", as: "employee" });

		Attendance.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });

		Attendance.belongsTo(models.ShiftType, { foreignKey: "shifttype_id", as: "shifttype" });

		Attendance.belongsTo(models.LeaveType, { foreignKey: "leavetype_id", as: "leavetype" });

		Attendance.belongsTo(models.LeaveApplication, { foreignKey: "leaveapplication_id", as: "leaveapplication" });

		Attendance.belongsTo(models.AttendanceRequest, { foreignKey: "attendancerequest_id", as: "attendancerequest" });

		Attendance.belongsTo(models.PermitEase, { foreignKey: "permitease_id", as: "permitease" });

		Attendance.belongsTo(models.AbsenceClarification, { foreignKey: "absenceclarification_id", as: "absenceclarification" });

		Attendance.belongsTo(models.HolidayListItem, { foreignKey: "holidaylistitem_id", as: "holidaylistitem" });

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

	return Attendance;
};
