const { DataTypes } = require("sequelize");
const sequelize = require("../db");

const ActivityLog = sequelize.define(
  "ActivityLog",
  {
    action: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: "Action performed: create, edit, delete, view, etc.",
    },
    entityType: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: "Type of entity: Deviations, Site Monitoring, Case Investigation, SDS, Safety Tool, etc.",
    },
    entityId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: "ID of the entity that was acted upon",
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: "Human-readable description of the action",
    },
    metadata: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: "Additional metadata about the action",
    },
    ipAddress: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    userAgent: {
      type: DataTypes.STRING,
      allowNull: true,
    },
  },
  {
    tableName: "activity_logs",
    timestamps: true,
    indexes: [
      {
        fields: ["userId"],
      },
      {
        fields: ["companyId"],
      },
      {
        fields: ["projectId"],
      },
      {
        fields: ["entityType"],
      },
      {
        fields: ["createdAt"],
      },
      {
        fields: ["action"],
      },
    ],
  }
);

module.exports = ActivityLog;
