import {
  Sequelize,
  DataTypes,
  Model,
  InferAttributes,
  InferCreationAttributes,
  CreationOptional,
} from "sequelize";

export class AuditLog extends Model<
  InferAttributes<AuditLog>,
  InferCreationAttributes<AuditLog>
> {
  declare id: CreationOptional<string>;
  declare actorUserId: string | null;
  declare workspaceId: string | null;
  declare action: string;
  declare metadata: Record<string, unknown> | null;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
}

export function initAuditLogModel(sequelize: Sequelize) {
  AuditLog.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      actorUserId: { type: DataTypes.UUID, allowNull: true },
      workspaceId: { type: DataTypes.UUID, allowNull: true },
      action: { type: DataTypes.STRING(255), allowNull: false },
      metadata: { type: DataTypes.JSON, allowNull: true },
      createdAt: DataTypes.DATE,
      updatedAt: DataTypes.DATE,
    },
    { sequelize, modelName: "AuditLog", tableName: "AuditLogs" }
  );
  return AuditLog;
}
