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

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

			// main
			ba_email: { type: DataTypes.STRING, allowNull: true },
			ba_designation_name: { type: DataTypes.STRING, allowNull: true },
			ba_territory_name: { type: DataTypes.STRING, allowNull: true },
			ba_username: { type: DataTypes.STRING, allowNull: true },
			ba_full_name: { type: DataTypes.STRING, allowNull: true },

			fin_email: { type: DataTypes.STRING, allowNull: true },
			fin_designation_name: { type: DataTypes.STRING, allowNull: true },
			fin_territory_name: { type: DataTypes.STRING, allowNull: true },
			fin_username: { type: DataTypes.STRING, allowNull: true },
			fin_full_name: { type: DataTypes.STRING, allowNull: true },

			// fk
			ba_company_id: { type: DataTypes.INTEGER, allowNull: true },
			ba_employee_id: { type: DataTypes.INTEGER, allowNull: false },
			ba_user_id: { type: DataTypes.INTEGER, allowNull: true },
			ba_designation_id: { type: DataTypes.INTEGER, allowNull: true },
			ba_territory_id: { type: DataTypes.INTEGER, allowNull: true },

			fin_company_id: { type: DataTypes.INTEGER, allowNull: true },
			fin_employee_id: { type: DataTypes.INTEGER, allowNull: false },
			fin_user_id: { type: DataTypes.INTEGER, allowNull: true },
			fin_designation_id: { type: DataTypes.INTEGER, allowNull: true },
			ba_territory_id: { type: DataTypes.INTEGER, allowNull: true },

			company_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetdepartment_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "budgetapprovers",
			freezeTableName: true,
		}
	);

	BudgetApprover.associate = (models) => {
		BudgetApprover.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		BudgetApprover.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		BudgetApprover.belongsTo(models.BudgetDepartment, { foreignKey: "budgetdepartment_id", as: "budgetdepartment" });
	};

	return BudgetApprover;
};
