module.exports = (sequelize, DataTypes) => {
  const Document = sequelize.define('Document', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      },
      comment: 'Company this document belongs to'
    },
    document_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'Document number/code (e.g., RI/MF/D/01)'
    },
    title: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: 'Document title/name'
    },
    category: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Document category (e.g., MF, QA, PUR, MKT, MN)'
    },
    document_type: {
      type: DataTypes.ENUM('D', 'F'),
      allowNull: true,
      comment: 'Document type: D (Directory/List) or F (Form)'
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Additional description or notes'
    },
    erp_module: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'ERP Module this document is linked to (e.g., BUSINESS_PARTNER, MATERIAL, PURCHASE_ORDER, PURCHASE_REQUISITION, STOCK_REGISTER)'
    },
    file_path: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: 'Path to the uploaded document file (PDF)'
    },
    file_name: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: 'Original filename of the uploaded document'
    },
    file_size: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Size of the document file in bytes'
    },
    file_type: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'MIME type of the document file (e.g., application/pdf)'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'DELETED'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    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: 'document',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'document_number']
      },
      {
        fields: ['company_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['category']
      }
    ]
  });

  Document.associate = function(models) {
    Document.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    Document.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    Document.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return Document;
};











