import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '@config/database';

interface ActivityAttributes {
  activityId: string;
  requestId: string;
  userId?: string | null;
  userName?: string | null;
  activityType: string; // activity_type
  activityDescription: string; // activity_description
  activityCategory?: string | null;
  severity?: string | null;
  metadata?: object | null;
  isSystemEvent?: boolean | null;
  ipAddress?: string | null;
  userAgent?: string | null;
  createdAt: Date;
}

interface ActivityCreationAttributes extends Optional<ActivityAttributes, 'activityId' | 'createdAt'> {}

class Activity extends Model<ActivityAttributes, ActivityCreationAttributes> implements ActivityAttributes {
  public activityId!: string;
  public requestId!: string;
  public userId!: string | null;
  public userName!: string | null;
  public activityType!: string;
  public activityDescription!: string;
  public activityCategory!: string | null;
  public severity!: string | null;
  public metadata!: object | null;
  public isSystemEvent!: boolean | null;
  public ipAddress!: string | null;
  public userAgent!: string | null;
  public createdAt!: Date;
}

Activity.init(
  {
    activityId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'activity_id'
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'request_id'
    },
    userId: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'user_id'
    },
    userName: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'user_name'
    },
    activityType: {
      type: DataTypes.STRING(100),
      allowNull: false,
      field: 'activity_type'
    },
    activityDescription: {
      type: DataTypes.TEXT,
      allowNull: false,
      field: 'activity_description'
    },
    activityCategory: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'activity_category'
    },
    severity: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    metadata: {
      type: DataTypes.JSONB,
      allowNull: true
    },
    isSystemEvent: {
      type: DataTypes.BOOLEAN,
      allowNull: true,
      field: 'is_system_event'
    },
    ipAddress: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'ip_address'
    },
    userAgent: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'user_agent'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    }
  },
  {
    sequelize,
    modelName: 'Activity',
    tableName: 'activities',
    timestamps: false,
    indexes: [
      { fields: ['request_id'] },
      { fields: ['created_at'] }
    ]
  }
);

export { Activity };


