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

module.exports = (sequelize) => {
	const Ebudgeting = sequelize.define(
		"ebudgeting",
		{
			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
			name: { type: DataTypes.STRING(150), allowNull: false }, // e.g., "Budget Plan 2024 - Marketing Dept"
			display_name: { type: DataTypes.STRING(150), allowNull: false },
			description: { type: DataTypes.TEXT, allowNull: true },
			fiscal_year: { type: DataTypes.STRING(10), allowNull: true }, // TAMBAHAN: e.g., '2024' untuk periode tahun
			budget_period_start: { type: DataTypes.DATE, allowNull: true }, // TAMBAHAN: Mulai periode (opsional jika pakai fiscal_year)
			budget_period_end: { type: DataTypes.DATE, allowNull: true }, // TAMBAHAN: Akhir periode
			total_budget_amount: { type: DataTypes.FLOAT, allowNull: true }, // TAMBAHAN: Total sum dari items (computed)

			// approver 1 step
			follow_via_email: { type: DataTypes.BOOLEAN, allowNull: true },
			leave_approver: { type: DataTypes.INTEGER, allowNull: true },
			posting_date: { type: DataTypes.DATE, allowNull: true },
			submitted_date: { type: DataTypes.DATE, allowNull: true },
			approver_date: { type: DataTypes.DATE, allowNull: true },
			status: { type: DataTypes.STRING, allowNull: true },

			// additional
			employee_name: { type: DataTypes.STRING, allowNull: true },
			employee_email: { type: DataTypes.STRING, allowNull: true },
			leave_approver_email: { type: DataTypes.STRING, allowNull: true },
			leave_approver_name: { type: DataTypes.STRING, allowNull: true },
			leave_approver_designation_territory: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			employee_designation_territory: {
				type: DataTypes.STRING,
				allowNull: true,
			},

			// notes & reason
			cancel_reason: { type: DataTypes.TEXT, allowNull: true },
			revision_notes: { type: DataTypes.TEXT, allowNull: true },
			rejection_reason: { type: DataTypes.TEXT, allowNull: true },

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

	/**
	 * Associates the EmployeeGroup model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	Ebudgeting.associate = (models) => {
		Ebudgeting.belongsTo(models.Company, { as: "company", foreignKey: "company_id" });
		Ebudgeting.belongsTo(models.Department, { as: "department", foreignKey: "department_id" });
		Ebudgeting.hasMany(models.EbudgetingItem, { as: "ebudgetingitems", foreignKey: "ebudgeting_id" });

		Ebudgeting.hasMany(models.HistoryEbudgeting, { foreignKey: "ebudgeting_id", as: "historyebudgeting" });
		Ebudgeting.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		Ebudgeting.belongsTo(models.Employee, { foreignKey: "leave_approver", as: "leaveapprover_name" });
	};

	return Ebudgeting;
};
