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

module.exports = (sequelize) => {
	const Budgeting = sequelize.define(
		"budgeting",
		{
			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
			// erp form
			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 },
			budget_against: { type: DataTypes.STRING(50), allowNull: true },
			company_erp: { type: DataTypes.STRING(50), allowNull: true },
			department_erp: { type: DataTypes.STRING(50), allowNull: true },
			fiscal_year: { type: DataTypes.STRING(10), allowNull: true }, // TAMBAHAN: e.g., '2024' untuk periode tahun
			total_budget_amount: { type: DataTypes.FLOAT, allowNull: true }, // TAMBAHAN: Total sum dari items (computed)
			budget_name: { type: DataTypes.STRING(150), allowNull: true },

			// 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: "budgetings",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the EmployeeGroup model with other models.
	 * @param {object} models - The models to associate with.
	 * @description
	 */
	Budgeting.associate = (models) => {
		Budgeting.belongsTo(models.Company, { as: "company", foreignKey: "company_id" });
		Budgeting.belongsTo(models.Department, { as: "department", foreignKey: "department_id" });
		Budgeting.belongsTo(models.Employee, { as: "employee", foreignKey: "employee_id" });
		Budgeting.hasMany(models.BudgetingItem, { as: "budgetingitems", foreignKey: "budgeting_id" });
		Budgeting.hasMany(models.BudgetingAccount, { as: "budgetingaccounts", foreignKey: "budgeting_id" });

		Budgeting.hasMany(models.HistoryBudgeting, { foreignKey: "budgeting_id", as: "historybudgeting" });
		Budgeting.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		Budgeting.belongsTo(models.Employee, { foreignKey: "leave_approver", as: "leaveapprover_name" });
	};

	return Budgeting;
};
