/**
 * UnassignTagAPIService
 *
 * Service for unassigning tags from entities (collections or files).
 * Calls the backend API: DELETE /api/v1/tags/{tagId}/entities/{entityId}
 */
class UnassignTagAPIService {
  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;
  }

  /**
   * Unassign a tag from an entity (collection or file)
   *
   * @param {string} tagId - UUID of the tag
   * @param {string} entityId - UUID of the entity (collection or file)
   * @param {string} entityType - Type of entity: "collection" or "file"
   * @returns {Promise<void>} - Resolves when unassignment is successful (204 No Content)
   */
  async unassignTag(tagId, entityId, entityType) {
    try {
      console.log("[UnassignTagAPIService] Unassigning tag from entity", {
        tagId,
        entityId,
        entityType
      });

      const apiClient = await this.getApiClient();
      await apiClient.deleteMapleFile(
        `/tags/${tagId}/entities/${entityId}?entity_type=${entityType}`
      );

      console.log("[UnassignTagAPIService] Tag unassigned successfully");
    } catch (error) {
      console.error("[UnassignTagAPIService] Failed to unassign tag:", error);
      throw error;
    }
  }
}

export default UnassignTagAPIService;
