// File: monorepo/web/maplefile-frontend/src/services/API/BlockedEmailAPIService.js
// Blocked Email API Service - Handles all API calls for blocked email management

class BlockedEmailAPIService {
  constructor(authManager) {
    // BlockedEmailAPIService depends on AuthManager for authentication
    this.authManager = authManager;
    this._apiClient = null;
    console.log(
      "[BlockedEmailAPIService] API service initialized with AuthManager dependency",
    );
  }

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

  // List all blocked emails for the current user
  async listBlockedEmails() {
    if (!this.authManager.isAuthenticated()) {
      throw new Error("User not authenticated via AuthManager");
    }

    try {
      console.log(
        "[BlockedEmailAPIService] Fetching blocked emails list from API",
      );

      const apiClient = await this.getApiClient();
      const response = await apiClient.getMapleFile("/me/blocked-emails");

      console.log(
        "[BlockedEmailAPIService] Blocked emails list retrieved:",
        response,
      );
      return response;
    } catch (error) {
      console.error(
        "[BlockedEmailAPIService] Failed to get blocked emails from API:",
        error,
      );
      throw error;
    }
  }

  // Create a new blocked email entry
  async createBlockedEmail(email) {
    if (!this.authManager.isAuthenticated()) {
      throw new Error("User not authenticated via AuthManager");
    }

    if (!email) {
      throw new Error("Email address is required");
    }

    try {
      console.log(
        "[BlockedEmailAPIService] Creating blocked email entry via API:",
        email,
      );

      const apiClient = await this.getApiClient();
      const response = await apiClient.postMapleFile("/me/blocked-emails", {
        email: email.toLowerCase().trim(),
      });

      console.log(
        "[BlockedEmailAPIService] Blocked email created successfully:",
        response,
      );
      return response;
    } catch (error) {
      console.error(
        "[BlockedEmailAPIService] Failed to create blocked email via API:",
        error,
      );
      throw error;
    }
  }

  // Delete a blocked email entry
  async deleteBlockedEmail(email) {
    if (!this.authManager.isAuthenticated()) {
      throw new Error("User not authenticated via AuthManager");
    }

    if (!email) {
      throw new Error("Email address is required");
    }

    try {
      console.log(
        "[BlockedEmailAPIService] Deleting blocked email entry via API:",
        email,
      );

      const apiClient = await this.getApiClient();
      const encodedEmail = encodeURIComponent(email.toLowerCase().trim());
      const response = await apiClient.deleteMapleFile(
        `/me/blocked-emails/${encodedEmail}`,
      );

      console.log(
        "[BlockedEmailAPIService] Blocked email deleted successfully:",
        response,
      );
      return response;
    } catch (error) {
      console.error(
        "[BlockedEmailAPIService] Failed to delete blocked email via API:",
        error,
      );
      throw error;
    }
  }

  // Get debug information
  getDebugInfo() {
    return {
      serviceName: "BlockedEmailAPIService",
      managedBy: "AuthManager",
      isAuthenticated: this.authManager.isAuthenticated(),
      canMakeRequests: this.authManager.canMakeAuthenticatedRequests(),
      authManagerStatus: {
        userEmail: this.authManager.getCurrentUserEmail(),
        sessionKeyStatus: this.authManager.getSessionKeyStatus(),
      },
    };
  }
}

export default BlockedEmailAPIService;
