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

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

			nc_no: { type: DataTypes.STRING, allowNull: false },
			nc_date: { type: DataTypes.DATE, allowNull: false },
			supplier_name: { type: DataTypes.STRING, allowNull: false },
			invoice_no: { type: DataTypes.STRING, allowNull: true },
			invoice_date: { type: DataTypes.DATE, allowNull: false },
			suratjalan_no: { type: DataTypes.STRING, allowNull: true },
			purchase_order_name: { type: DataTypes.STRING, allowNull: true },
			prc_no: { type: DataTypes.STRING, allowNull: true },
			prc_date: { type: DataTypes.DATE, allowNull: false },

			nc_description: { type: DataTypes.TEXT, allowNull: true },
			nc_cause: { type: DataTypes.TEXT, allowNull: true },

			follow_up: { type: DataTypes.STRING(50), allowNull: true },
			return_date: { type: DataTypes.DATE, allowNull: true },
			bast_file: { type: DataTypes.TEXT, allowNull: true },

			ncreceipt_date: { type: DataTypes.DATE, allowNull: true },
			progress: { type: DataTypes.STRING, allowNull: true },
			creditnote_file: { type: DataTypes.TEXT, allowNull: true },
			creditnote_date: { type: DataTypes.DATE, allowNull: true },

			productcondition_file: { type: DataTypes.TEXT, allowNull: true },
			//   fk
			company_id: {
				type: DataTypes.INTEGER,
				allowNull: false,
				references: {
					model: "companies",
					key: "id",
				},
			},
		},
		{
			tableName: "ncproducts",
			freezeTableName: true,
		}
	);

	/**
	 * Associates the Applicant model with other models.
	 * @param {object} models - The models to associate with.
	 */
	NcProducts.associate = (models) => {
		NcProducts.hasMany(models.NcProductChild, {
			foreignKey: "ncproduct_id",
			as: "ncproductchilds",
			onDelete: "CASCADE",
		});

		NcProducts.hasMany(models.NcProductFU, {
			foreignKey: "ncproduct_id",
			as: "ncproductfus",
			onDelete: "CASCADE",
		});
		/**
		 * Associates the Applicants model with the Company model.
		 * A Model belongs to a Company.
		 * @param {object} models - The models to associate with.
		 */
		NcProducts.belongsTo(models.Company, {
			foreignKey: "company_id",
			as: "company",
		});

		NcProducts.belongsTo(models.User, { foreignKey: "owner", as: "user" });
	};

	return NcProducts;
};
