import { Injectable, Logger } from '@nestjs/common';
import { PushChannel } from '../../../domain/entities/device-token.entity';
import {
  IPushChannelProvider,
  PushPayload,
  PushResult,
} from '../../../domain/ports/push-channel-provider.interface';

/**
 * Apple Push Notification service (APNs) provider.
 *
 * Production integration:
 *   npm install @parse/node-apn   (or use HTTP/2 client directly)
 *   Environment: APNS_KEY_ID, APNS_TEAM_ID, APNS_KEY_PATH, APNS_BUNDLE_ID, APNS_PRODUCTION (bool)
 *
 * Primary channel for iOS devices in China where FCM is unreliable.
 */
@Injectable()
export class ApnsPushProvider implements IPushChannelProvider {
  private readonly logger = new Logger(ApnsPushProvider.name);
  readonly channel = PushChannel.APNS;

  async send(payload: PushPayload): Promise<PushResult> {
    // TODO: Replace with APNs HTTP/2 push
    this.logger.log(
      `[MOCK APNs] → token=${payload.token.slice(0, 12)}… title="${payload.title}"`,
    );
    return {
      success: true,
      messageId: `apns-mock-${Date.now()}`,
    };
  }

  async sendBatch(payloads: PushPayload[]): Promise<PushResult[]> {
    // TODO: Replace with parallel APNs sends
    this.logger.log(`[MOCK APNs] Batch send: ${payloads.length} messages`);
    return payloads.map((p) => ({
      success: true,
      messageId: `apns-mock-${Date.now()}-${p.token.slice(0, 8)}`,
    }));
  }
}
