'use strict';
const { Model } = require('sequelize');
const { defaultKeys, modelDefaults, actionUsers } = require('../sequelize/defaults');
module.exports = (sequelize, DataTypes) => {
  class Product extends Model {
    static associate(models) {
      actionUsers(Product, models);
      Product.hasMany(models.OrderDetails, {
        sourceKey: "product_id",
        foreignKey: "product_id",
        as: "orderDetails",
      });
      Product.belongsTo(models.Branch, {
        targetKey: "branch_id",
        foreignKey: "branch_id",
        as: "branches",
      }); 
    }
  }

  Product.init({
    ...defaultKeys("product_id"),
    tbl_ref_id: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    product_name: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
    },
    product_descp: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    active_status: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: 1,
    },
  }, modelDefaults(sequelize, 'products'));
  return Product;
};