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

module.exports = (sequelize) => {
	const BudgetPlanItem = sequelize.define(
		"budgetplanitem",
		{
			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
			description: { type: DataTypes.TEXT, allowNull: true },
			specifications: { type: DataTypes.TEXT, allowNull: true },
			notes: { type: DataTypes.TEXT, allowNull: true },

			allocation_type: { type: DataTypes.STRING, allowNull: true },
			allocated_month: { type: DataTypes.JSON, allowNull: true },

			// detail item
			quantity: { type: DataTypes.FLOAT, allowNull: true },
			unit_price: { type: DataTypes.FLOAT, allowNull: true },
			total_price: { type: DataTypes.FLOAT, allowNull: true },

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

			follow_via_email: { type: DataTypes.BOOLEAN, allowNull: true },
			budget_approver: { type: DataTypes.INTEGER, allowNull: true },
			budget_approver_email: { type: DataTypes.INTEGER, allowNull: true },
			finance_approver: { type: DataTypes.INTEGER, allowNull: true },
			finance_approver_email: { type: DataTypes.INTEGER, allowNull: true },

			posting_date: { type: DataTypes.DATE, allowNull: true },
			submitted_date: { type: DataTypes.DATE, allowNull: true },

			budget_approver_date: { type: DataTypes.DATE, allowNull: true },
			finance_approver_date: { type: DataTypes.DATE, allowNull: true },

			status: { type: DataTypes.STRING, allowNull: true },

			cancel_budget_reason: { type: DataTypes.TEXT, allowNull: true },
			revision_budget_notes: { type: DataTypes.TEXT, allowNull: true },
			rejection_budget_reason: { type: DataTypes.TEXT, allowNull: true },
			approval_budget_notes: { type: DataTypes.TEXT, allowNull: true },

			cancel_finance_reason: { type: DataTypes.TEXT, allowNull: true },
			revision_finance_notes: { type: DataTypes.TEXT, allowNull: true },
			rejection_finance_reason: { type: DataTypes.TEXT, allowNull: true },
			approval_finance_notes: { type: DataTypes.TEXT, allowNull: true },

			item_code: { type: DataTypes.STRING(50), allowNull: true },
			item_name: { type: DataTypes.STRING(255), allowNull: true },
			specifications: { type: DataTypes.TEXT, allowNull: true },
			description: { type: DataTypes.TEXT, allowNull: true },
			default_price: { type: DataTypes.FLOAT, allowNull: true },

			// fk
			company_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetdepartment_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetplan_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetitem_id: { type: DataTypes.INTEGER, allowNull: true },

			budgetclassification_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetcategory_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetaccount_id: { type: DataTypes.INTEGER, allowNull: true },
			budgetunitofmeasurement_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "budgetplanitems",
			freezeTableName: true,
		}
	);

	BudgetPlanItem.associate = (models) => {
		BudgetPlanItem.belongsTo(models.User, { foreignKey: "modified_by", as: "modifier" });
		BudgetPlanItem.belongsTo(models.Employee, { foreignKey: "budget_approver", as: "budgetapprover" });
		BudgetPlanItem.belongsTo(models.Employee, { foreignKey: "finance_approver", as: "financeapprover" });
		BudgetPlanItem.belongsTo(models.Company, { foreignKey: "company_id", as: "company" });
		BudgetPlanItem.belongsTo(models.BudgetItem, { foreignKey: "budgetitem_id", as: "budgetitem" });
		BudgetPlanItem.belongsTo(models.BudgetPlan, { foreignKey: "budgetplan_id", as: "budgetplan" });
	};

	return BudgetPlanItem;
};
