import { FcmService } from '../services/fcm.service';

vi.mock('firebase-admin', () => ({
  apps: [],
  initializeApp: vi.fn(),
  credential: { cert: vi.fn() },
  messaging: vi.fn(),
}));

describe('FcmService', () => {
  let service: FcmService;
  let mockLogger: { log: ReturnType<typeof vi.fn>; warn: ReturnType<typeof vi.fn>; error: ReturnType<typeof vi.fn> };

  beforeEach(() => {
    mockLogger = { log: vi.fn(), warn: vi.fn(), error: vi.fn() };
    delete process.env['FIREBASE_SERVICE_ACCOUNT'];
    service = new FcmService(mockLogger as any);
  });

  it('isAvailable returns false when FIREBASE_SERVICE_ACCOUNT not set', () => {
    service.onModuleInit();

    expect(service.isAvailable).toBe(false);
  });

  it('send throws when not initialized', async () => {
    service.onModuleInit();

    await expect(
      service.send({ token: 'device-token', title: 'Test', body: 'Test body' }),
    ).rejects.toThrow('FCM not initialized — FIREBASE_SERVICE_ACCOUNT not configured');
  });

  it('onModuleInit logs warning when env not set', () => {
    service.onModuleInit();

    expect(mockLogger.warn).toHaveBeenCalledWith(
      'FIREBASE_SERVICE_ACCOUNT not set — push notifications disabled',
      'FcmService',
    );
  });
});
