/*
 * Copyright 2025 Hirokazu Kobayashi
 *
 * Licensed 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.idp.server.control_plane.management.identity.user;

import org.idp.server.core.openid.identity.User;
import org.idp.server.core.openid.token.OAuthToken;
import org.idp.server.platform.multi_tenancy.tenant.Tenant;
import org.idp.server.platform.security.SecurityEvent;
import org.idp.server.platform.security.SecurityEventPublisher;
import org.idp.server.platform.security.event.SecurityEventType;
import org.idp.server.platform.type.RequestAttributes;

/**
 * Publisher for SecurityEvents generated by Management API operations.
 *
 * <p>This publisher creates SecurityEvents for administrative operations performed through the
 * Management API, including user creation, updates, password changes, and deletions. Each event
 * includes information about the operator (admin user) and the target user.
 */
public class ManagementEventPublisher {

  SecurityEventPublisher securityEventPublisher;

  public ManagementEventPublisher(SecurityEventPublisher securityEventPublisher) {
    this.securityEventPublisher = securityEventPublisher;
  }

  /**
   * Publishes a SecurityEvent for a Management API operation.
   *
   * @param tenant the tenant where the operation occurred
   * @param operator the admin user performing the operation
   * @param targetUser the user being operated on (must not be null for management operations)
   * @param oAuthToken the OAuth token used for the operation
   * @param securityEventType the type of security event
   * @param requestAttributes HTTP request attributes (IP, User-Agent, etc.)
   */
  public void publish(
      Tenant tenant,
      User operator,
      User targetUser,
      OAuthToken oAuthToken,
      SecurityEventType securityEventType,
      RequestAttributes requestAttributes) {
    ManagementEventCreator eventCreator =
        new ManagementEventCreator(
            tenant, operator, targetUser, oAuthToken, securityEventType, requestAttributes);
    SecurityEvent securityEvent = eventCreator.create();
    securityEventPublisher.publish(securityEvent);
  }

  public void publishSync(
      Tenant tenant,
      User operator,
      User targetUser,
      OAuthToken oAuthToken,
      SecurityEventType securityEventType,
      RequestAttributes requestAttributes) {
    ManagementEventCreator eventCreator =
        new ManagementEventCreator(
            tenant, operator, targetUser, oAuthToken, securityEventType, requestAttributes);
    SecurityEvent securityEvent = eventCreator.create();
    securityEventPublisher.publishSync(securityEvent);
  }
}
