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';

/**
 * Google Firebase Cloud Messaging (FCM) push provider.
 *
 * Production integration:
 *   npm install firebase-admin
 *   Environment: FIREBASE_SERVICE_ACCOUNT_JSON (path or JSON string)
 *
 * Supports both Android and iOS devices globally.
 */
@Injectable()
export class FcmPushProvider implements IPushChannelProvider {
  private readonly logger = new Logger(FcmPushProvider.name);
  readonly channel = PushChannel.FCM;

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

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