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

module.exports = (sequelize) => {
	const ShiftType = sequelize.define(
		"shifttype",
		{
			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 },
			enabled: { type: DataTypes.BOOLEAN, allowNull: false },

			name: { type: DataTypes.STRING(100), allowNull: false },
			display_name: { type: DataTypes.STRING(200), allowNull: false },
			description: { type: DataTypes.TEXT, allowNull: true },
			code: { type: DataTypes.STRING(10), allowNull: true },

			start_time: { type: DataTypes.TIME, allowNull: false },
			end_time: { type: DataTypes.TIME, allowNull: false },
			late_time: { type: DataTypes.TIME, allowNull: false },
			early_time: { type: DataTypes.TIME, allowNull: false },
			duration_break: { type: DataTypes.FLOAT, allowNull: true },

			start_date_ramadhan: { type: DataTypes.DATEONLY, allowNull: true },
			end_date_ramadhan: { type: DataTypes.DATEONLY, allowNull: true },
			start_time_ramadhan: { type: DataTypes.TIME, allowNull: true },
			end_time_ramadhan: { type: DataTypes.TIME, allowNull: true },
			late_time_ramadhan: { type: DataTypes.TIME, allowNull: true },
			early_time_ramadhan: { type: DataTypes.TIME, allowNull: true },
			duration_break_ramadhan: { type: DataTypes.FLOAT, allowNull: true },

			enable_auto_attendance: { type: DataTypes.BOOLEAN, allowNull: true },
			location: { type: DataTypes.JSON, allowNull: true },

			//fk
			holidaygroup_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
				references: {
					model: "holidaygroups",
					key: "id",
				},
			},
			holidaylist_id: {
				type: DataTypes.INTEGER,
				allowNull: true,
				references: {
					model: "holidaylists",
					key: "id",
				},
			},
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
				references: {
					model: "companies",
					key: "id",
				},
			},
		},
		{
			tableName: "shifttypes",
			freezeTableName: true,
		}
	);

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

		/**
		 * Associates the ShiftType model with the HolidayList model.
		 */
		ShiftType.belongsTo(models.HolidayList, {
			foreignKey: "holidaylist_id",
			as: "holidaylist",
		});

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

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

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

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

	return ShiftType;
};
