import { Injectable } from "@nestjs/common";
import { FreebitOperationsService } from "./freebit-operations.service";
import { FreebitMapperService } from "./freebit-mapper.service";
import type { SimDetails, SimUsage, SimTopUpHistory } from "../interfaces/freebit.types";

@Injectable()
export class FreebitOrchestratorService {
  constructor(
    private readonly operations: FreebitOperationsService,
    private readonly mapper: FreebitMapperService
  ) {}

  /**
   * Get SIM account details
   */
  async getSimDetails(account: string): Promise<SimDetails> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.getSimDetails(normalizedAccount);
  }

  /**
   * Get SIM usage information
   */
  async getSimUsage(account: string): Promise<SimUsage> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.getSimUsage(normalizedAccount);
  }

  /**
   * Top up SIM data quota
   */
  async topUpSim(
    account: string,
    quotaMb: number,
    options: { campaignCode?: string; expiryDate?: string; scheduledAt?: string } = {}
  ): Promise<void> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.topUpSim(normalizedAccount, quotaMb, options);
  }

  /**
   * Get SIM top-up history
   */
  async getSimTopUpHistory(
    account: string,
    fromDate: string,
    toDate: string
  ): Promise<SimTopUpHistory> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.getSimTopUpHistory(normalizedAccount, fromDate, toDate);
  }

  /**
   * Change SIM plan
   */
  async changeSimPlan(
    account: string,
    newPlanCode: string,
    options: { assignGlobalIp?: boolean; scheduledAt?: string } = {}
  ): Promise<{ ipv4?: string; ipv6?: string }> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.changeSimPlan(normalizedAccount, newPlanCode, options);
  }

  /**
   * Update SIM features
   */
  async updateSimFeatures(
    account: string,
    features: {
      voiceMailEnabled?: boolean;
      callWaitingEnabled?: boolean;
      internationalRoamingEnabled?: boolean;
      networkType?: "4G" | "5G";
    }
  ): Promise<void> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.updateSimFeatures(normalizedAccount, features);
  }

  /**
   * Cancel SIM service
   */
  async cancelSim(account: string, scheduledAt?: string): Promise<void> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.cancelSim(normalizedAccount, scheduledAt);
  }

  /**
   * Reissue eSIM profile (simple)
   */
  async reissueEsimProfile(account: string): Promise<void> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.reissueEsimProfile(normalizedAccount);
  }

  /**
   * Reissue eSIM profile with enhanced options
   */
  async reissueEsimProfileEnhanced(
    account: string,
    newEid: string,
    options: { oldEid?: string; planCode?: string } = {}
  ): Promise<void> {
    const normalizedAccount = this.mapper.normalizeAccount(account);
    return this.operations.reissueEsimProfileEnhanced(normalizedAccount, newEid, options);
  }

  /**
   * Activate new eSIM account
   */
  async activateEsimAccountNew(params: {
    account: string;
    eid: string;
    planCode?: string;
    contractLine?: "4G" | "5G";
    aladinOperated?: "10" | "20";
    shipDate?: string;
    addKind?: "N" | "M" | "R";
    simKind?: "E0" | "E2" | "E3";  // E0:音声あり, E2:SMSなし, E3:SMSあり
    repAccount?: string;
    deliveryCode?: string;
    globalIp?: "10" | "20";
    mnp?: { reserveNumber: string; reserveExpireDate?: string };
    identity?: {
      firstnameKanji?: string;
      lastnameKanji?: string;
      firstnameZenKana?: string;
      lastnameZenKana?: string;
      gender?: string;
      birthday?: string;
    };
  }): Promise<void> {
    const normalizedAccount = this.mapper.normalizeAccount(params.account);
    return this.operations.activateEsimAccountNew({
      account: normalizedAccount,
      eid: params.eid,
      planCode: params.planCode,
      contractLine: params.contractLine,
      aladinOperated: params.aladinOperated,
      shipDate: params.shipDate,
      addKind: params.addKind,
      simKind: params.simKind,
      repAccount: params.repAccount,
      deliveryCode: params.deliveryCode,
      globalIp: params.globalIp,
      mnp: params.mnp,
      identity: params.identity,
    });
  }

  /**
   * Health check
   */
  async healthCheck(): Promise<boolean> {
    return this.operations.healthCheck();
  }
}
