/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.gravitino.listener.api.event;

import com.google.common.collect.ImmutableList;
import java.util.List;
import org.apache.gravitino.annotation.DeveloperApi;
import org.apache.gravitino.authorization.SecurableObject;
import org.apache.gravitino.utils.NameIdentifierUtil;

/**
 * Represents an event triggered when an attempt to override privileges in a role fails due to an
 * exception.
 */
@DeveloperApi
public class OverridePrivilegesFailureEvent extends RoleFailureEvent {

  private final String roleName;
  private final List<SecurableObject> securableObjectsToOverride;

  /**
   * Constructs a new {@code OverridePrivilegesFailureEvent} instance.
   *
   * @param user the name of the user who triggered the event
   * @param metalake the name of the metalake
   * @param exception the exception that occurred while overriding privileges
   * @param roleName the name of the role whose privileges were being overridden
   * @param securableObjectsToOverride the list of securable objects intended to be overridden; if
   *     {@code null}, an empty list is used
   */
  public OverridePrivilegesFailureEvent(
      String user,
      String metalake,
      Exception exception,
      String roleName,
      List<SecurableObject> securableObjectsToOverride) {
    super(user, NameIdentifierUtil.ofRole(metalake, roleName), exception);
    this.roleName = roleName;
    this.securableObjectsToOverride =
        securableObjectsToOverride == null
            ? ImmutableList.of()
            : ImmutableList.copyOf(securableObjectsToOverride);
  }

  /**
   * Returns the name of the role.
   *
   * @return the name of the role whose privileges were intended to be overridden
   */
  public String roleName() {
    return roleName;
  }

  /**
   * Returns the list of securable objects intended to be overridden.
   *
   * @return an immutable list of securable objects
   */
  public List<SecurableObject> securableObjectsToOverride() {
    return securableObjectsToOverride;
  }

  /**
   * Returns the operation type of this event.
   *
   * @return the operation type.
   */
  @Override
  public OperationType operationType() {
    return OperationType.OVERRIDE_PRIVILEGES;
  }
}
