import { Injectable } from "@nestjs/common";
import { SimOrchestratorService } from "./sim-management/services/sim-orchestrator.service";
import { SimNotificationService } from "./sim-management/services/sim-notification.service";
import type {
  SimDetails,
  SimUsage,
  SimTopUpHistory,
} from "@bff/integrations/freebit/interfaces/freebit.types";
import type {
  SimTopUpRequest,
  SimPlanChangeRequest,
  SimCancelRequest,
  SimTopUpHistoryRequest,
  SimFeaturesUpdateRequest,
} from "./sim-management/types/sim-requests.types";
import type { SimNotificationContext } from "./sim-management/interfaces/sim-base.interface";

@Injectable()
export class SimManagementService {
  constructor(
    private readonly simOrchestrator: SimOrchestratorService,
    private readonly simNotification: SimNotificationService
  ) {}

  // Delegate to notification service for backward compatibility
  private async notifySimAction(
    action: string,
    status: "SUCCESS" | "ERROR",
    context: SimNotificationContext
  ): Promise<void> {
    return this.simNotification.notifySimAction(action, status, context);
  }

  /**
   * Debug method to check subscription data for SIM services
   */
  async debugSimSubscription(
    userId: string,
    subscriptionId: number
  ): Promise<Record<string, unknown>> {
    return this.simOrchestrator.debugSimSubscription(userId, subscriptionId);
  }

  /**
   * Debug method to query Freebit directly for any account's details
   */
  async getSimDetailsDebug(account: string): Promise<SimDetails> {
    return this.simOrchestrator.getSimDetailsDirectly(account);
  }

  // This method is now handled by SimValidationService internally

  /**
   * Get SIM details for a subscription
   */
  async getSimDetails(userId: string, subscriptionId: number): Promise<SimDetails> {
    return this.simOrchestrator.getSimDetails(userId, subscriptionId);
  }

  /**
   * Get SIM data usage for a subscription
   */
  async getSimUsage(userId: string, subscriptionId: number): Promise<SimUsage> {
    return this.simOrchestrator.getSimUsage(userId, subscriptionId);
  }

  /**
   * Top up SIM data quota with payment processing
   * Pricing: 1GB = 500 JPY
   */
  async topUpSim(userId: string, subscriptionId: number, request: SimTopUpRequest): Promise<void> {
    return this.simOrchestrator.topUpSim(userId, subscriptionId, request);
  }

  /**
   * Get SIM top-up history
   */
  async getSimTopUpHistory(
    userId: string,
    subscriptionId: number,
    request: SimTopUpHistoryRequest
  ): Promise<SimTopUpHistory> {
    return this.simOrchestrator.getSimTopUpHistory(userId, subscriptionId, request);
  }

  /**
   * Change SIM plan
   */
  async changeSimPlan(
    userId: string,
    subscriptionId: number,
    request: SimPlanChangeRequest
  ): Promise<{ ipv4?: string; ipv6?: string }> {
    return this.simOrchestrator.changeSimPlan(userId, subscriptionId, request);
  }

  /**
   * Update SIM features (voicemail, call waiting, roaming, network type)
   */
  async updateSimFeatures(
    userId: string,
    subscriptionId: number,
    request: SimFeaturesUpdateRequest
  ): Promise<void> {
    return this.simOrchestrator.updateSimFeatures(userId, subscriptionId, request);
  }

  /**
   * Cancel SIM service
   */
  async cancelSim(
    userId: string,
    subscriptionId: number,
    request: SimCancelRequest = {}
  ): Promise<void> {
    return this.simOrchestrator.cancelSim(userId, subscriptionId, request);
  }

  /**
   * Reissue eSIM profile
   */
  async reissueEsimProfile(userId: string, subscriptionId: number, newEid?: string): Promise<void> {
    return this.simOrchestrator.reissueEsimProfile(userId, subscriptionId, newEid);
  }

  /**
   * Get comprehensive SIM information (details + usage combined)
   */
  async getSimInfo(
    userId: string,
    subscriptionId: number
  ): Promise<{
    details: SimDetails;
    usage: SimUsage;
  }> {
    return this.simOrchestrator.getSimInfo(userId, subscriptionId);
  }

  /**
   * Convert technical errors to user-friendly messages for SIM operations
   */
  private getUserFriendlySimError(technicalError: string): string {
    return this.simNotification.getUserFriendlySimError(technicalError);
  }
}
