package com.hpay.hpay_mobile_api.config;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.InputStream;

@Configuration
public class firebaseConfig {

    @PostConstruct
    public void initialize() {
        try {

            InputStream serviceAccount = getClass().getClassLoader()
                    .getResourceAsStream("wechrist-firebase.json");

            if (serviceAccount == null) {
                throw new IllegalStateException("Fichier service-account.json introuvable !");
            }

            FirebaseOptions options = FirebaseOptions.builder()
                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .build();

            if (FirebaseApp.getApps().isEmpty()) {
                FirebaseApp.initializeApp(options);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
