module.exports = (sequelize, DataTypes) => {
  const VendorCategory = sequelize.define('VendorCategory', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    category_code: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: 'Unique code for vendor category (e.g., REGULAR, SUB_CONTRACTOR, JOB_WORKER)'
    },
    category_name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: 'Name of vendor category (e.g., Regular Vendor, Sub Contractor, Job Worker)'
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    gst_reduction_percentage: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: true,
      defaultValue: 0.00,
      comment: 'GST reduction percentage for this category (e.g., 6% reduction means 18% becomes 12%)'
    },
    applies_to_raw_materials: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: 'Whether GST reduction applies to raw materials'
    },
    applies_to_semi_finished: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: 'Whether GST reduction applies to semi-finished materials'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'vendor_category',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'category_code']
      },
      {
        fields: ['company_id']
      },
      {
        fields: ['is_active']
      }
    ]
  });

  VendorCategory.associate = function(models) {
    VendorCategory.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    VendorCategory.hasMany(models.BusinessPartner, { foreignKey: 'vendor_category_id', as: 'businessPartners' });
    VendorCategory.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    VendorCategory.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
  };

  return VendorCategory;
};

