import { Injectable, OnModuleInit } from '@nestjs/common';
import { Counter, Gauge } from 'prom-client';
import { InjectMetric } from '@willsoto/nestjs-prometheus';

@Injectable()
export class MetricsBootstrap implements OnModuleInit {
    constructor(
        @InjectMetric('app_info')
        private readonly appInfoGauge: Gauge<string>,

        @InjectMetric('service_restart_total')
        private readonly restartCounter: Counter<string>,
    ) { }

    onModuleInit() {
        const job = process.env.SERVICE_NAME || 'HUB-backend';
        const version = process.env.APP_VERSION || '1.0.0';
        const buildTime = process.env.BUILD_TIME || new Date().toISOString();

        // Set build info
        this.appInfoGauge.set(
            {
                job,
                version,
                build_time: buildTime,
            },
            1,
        );

        // Increment restart counter ONCE per startup
        this.restartCounter.inc({ job }, 1);
    }
}
