import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AppConfigService {
  constructor(private readonly configService: ConfigService) {}

  get nodeEnv(): string {
    return this.required('NODE_ENV');
  }

  get port(): number {
    return Number(this.required('PORT'));
  }

  get databaseUrl(): string {
    return this.required('DATABASE_URL');
  }

  get storageRoot(): string {
    return this.required('STORAGE_ROOT');
  }

  get publicBaseUrl(): string {
    return this.required('PUBLIC_BASE_URL');
  }

  get deviceBootstrapSecret(): string {
    return this.required('DEVICE_BOOTSTRAP_SECRET');
  }

  get maxUploadSizeBytes(): number {
    return Number(this.required('MAX_UPLOAD_SIZE_BYTES'));
  }

  get appVersion(): string {
    return process.env.npm_package_version ?? '0.1.0';
  }

  private required(key: string): string {
    const value = this.configService.get<string>(key);
    if (!value) {
      throw new Error(`Missing required configuration value for ${key}`);
    }
    return value;
  }
}
