"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var ConfigService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigService = void 0;
const common_1 = require("@nestjs/common");
const dotenv = require("dotenv");
const fs = require("fs");
const path = require("path");
let ConfigService = ConfigService_1 = class ConfigService {
    constructor() {
        this.logger = new common_1.Logger(ConfigService_1.name);
        const envFile = path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || 'development'}`);
        const defaultEnvFile = path.resolve(process.cwd(), '.env');
        if (fs.existsSync(envFile)) {
            this.envConfig = dotenv.parse(fs.readFileSync(envFile));
        }
        else if (fs.existsSync(defaultEnvFile)) {
            this.envConfig = dotenv.parse(fs.readFileSync(defaultEnvFile));
        }
        else {
            this.envConfig = {};
        }
        for (const key of Object.keys(process.env)) {
            if (process.env[key] !== undefined) {
                this.envConfig[key] = process.env[key];
            }
        }
    }
    get(key, defaultValue) {
        const value = this.envConfig[key];
        if (value === undefined && defaultValue === undefined) {
            this.logger.warn(`Config key "${key}" not found and no default provided`);
        }
        return value ?? defaultValue ?? '';
    }
    getNumber(key, defaultValue) {
        const val = this.get(key, defaultValue?.toString());
        return parseInt(val, 10);
    }
    getBoolean(key, defaultValue) {
        const val = this.get(key, defaultValue?.toString());
        return val === 'true' || val === '1';
    }
    getRedisConfig() {
        return {
            host: this.get('REDIS_HOST', 'localhost'),
            port: this.getNumber('REDIS_PORT', 6379),
            password: this.get('REDIS_PASSWORD', ''),
            db: this.getNumber('REDIS_DB', 0),
        };
    }
    getMpesaConfig() {
        return {
            consumerKey: this.get('MPESA_CONSUMER_KEY', ''),
            consumerSecret: this.get('MPESA_CONSUMER_SECRET', ''),
            passkey: this.get('MPESA_PASSKEY', ''),
            shortCode: this.get('MPESA_SHORTCODE', ''),
            tillNumber: this.get('MPESA_TILL_NUMBER', ''),
            transactionType: this.get('MPESA_TRANSACTION_TYPE', 'CustomerPayBillOnline'),
            callbackUrl: this.get('MPESA_CALLBACK_URL', ''),
            confirmationUrl: this.get('MPESA_CONFIRMATION_URL', ''),
            validationUrl: this.get('MPESA_VALIDATION_URL', ''),
            environment: this.get('MPESA_ENVIRONMENT', 'sandbox'),
            baseUrl: this.get('MPESA_ENVIRONMENT') === 'production'
                ? 'https://api.safaricom.co.ke'
                : 'https://sandbox.safaricom.co.ke',
            stkPushTimeout: this.getNumber('MPESA_STK_TIMEOUT', 30),
        };
    }
    getEncryptionKey() {
        return this.get('ENCRYPTION_KEY', 'change-me-in-production-32chars');
    }
};
exports.ConfigService = ConfigService;
exports.ConfigService = ConfigService = ConfigService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [])
], ConfigService);
