/**
 * AssignTagAPIService
 *
 * Service for assigning tags to entities (collections or files).
 * Calls the backend API: POST /api/v1/tags/{id}/assign
 */
class AssignTagAPIService {
  constructor(authManager) {
    this.authManager = authManager;
    this._apiClient = null;
  }

  async getApiClient() {
    if (!this._apiClient) {
      const { default: ApiClient } = await import("../ApiClient.js");
      this._apiClient = ApiClient;
    }
    return this._apiClient;
  }

  /**
   * Assign a tag to an entity (collection or file)
   *
   * @param {string} tagId - UUID of the tag
   * @param {Object} payload - Assignment payload
   * @param {string} payload.entity_id - UUID of the entity (collection or file)
   * @param {string} payload.entity_type - Type of entity: "collection" or "file"
   * @returns {Promise<void>} - Resolves when assignment is successful (204 No Content)
   */
  async assignTag(tagId, payload) {
    try {
      console.log("[AssignTagAPIService] Assigning tag to entity", {
        tagId,
        entityId: payload.entity_id,
        entityType: payload.entity_type
      });

      const apiClient = await this.getApiClient();
      await apiClient.postMapleFile(
        `/tags/${tagId}/assign`,
        payload
      );

      console.log("[AssignTagAPIService] Tag assigned successfully");
    } catch (error) {
      console.error("[AssignTagAPIService] Failed to assign tag:", error);
      throw error;
    }
  }
}

export default AssignTagAPIService;
