package ru.nbch.credit_tracker.config.settings.version;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Slf4j
@Configuration
public class VersionInfoLogger {

    @EventListener(ApplicationReadyEvent.class)
    public void logGitInfo() {
        try {
            Properties properties = new Properties();
            ClassPathResource resource = new ClassPathResource("git.properties");
            try (InputStream inputStream = resource.getInputStream()) {
                properties.load(inputStream);
                String version = VersionInfoProvider.obtainVersionInfo(properties, "git_info");
                log.info("Application version: {}", version);
            }
        } catch (IOException e) {
            log.warn("Failed to load git.properties", e);
        }
    }

}
