import { resolve } from 'path';
import { existsSync } from 'fs';

export class OidcConfigService {
	private static readonly KEY_FILE = resolve(process.cwd(), 'config', 'keys', 'keystore.json');

	get issuer(): string {
		return process.env.OIDC_ISSUER ?? 'http://localhost:3001';
	}

	get accessTokenTtl(): number {
		return Number(process.env.OIDC_ACCESS_TOKEN_TTL ?? 900);
	}

	get refreshTokenTtl(): number {
		return Number(process.env.OIDC_REFRESH_TOKEN_TTL ?? 2592000);
	}

	get cookieKeys(): string[] {
		const keys = process.env.OIDC_COOKIE_KEYS ?? 'dev-cookie-key-minimum-32-chars';
		return keys.split(',').filter(Boolean);
	}

	/** Path to the JWK keystore file for signing key rotation persistence */
	get jwkFile(): string {
		return process.env.OIDC_JWK_FILE ?? OidcConfigService.KEY_FILE;
	}

	get hasJwkFile(): boolean {
		return existsSync(this.jwkFile);
	}
}
