/**
 * Application configuration singleton
 */

class Config {
	private basePath: string;
	public readonly timezone: string;
	public readonly parserUrl: string;
	public readonly port: number;
	public readonly host: string;

	constructor() {
		// Default base path logic:
		// 1. Check environment variable
		// 2. Fall back to directory containing the executable
		const envPath = Deno.env.get('APP_BASE_PATH');
		if (envPath) {
			this.basePath = envPath;
		} else {
			// Use the directory where the executable is located
			const execPath = Deno.execPath();
			const lastSlash = Math.max(execPath.lastIndexOf('/'), execPath.lastIndexOf('\\'));
			this.basePath = lastSlash > 0 ? execPath.substring(0, lastSlash) : '.';
		}

		// Timezone configuration:
		// 1. Check TZ environment variable
		// 2. Fall back to system timezone
		this.timezone = Deno.env.get('TZ') || Intl.DateTimeFormat().resolvedOptions().timeZone;

		// Parser service configuration
		const parserHost = Deno.env.get('PARSER_HOST') || 'localhost';
		const parserPort = Deno.env.get('PARSER_PORT') || '5000';
		this.parserUrl = `http://${parserHost}:${parserPort}`;

		// Server bind configuration
		this.port = parseInt(Deno.env.get('PORT') || '6868', 10);
		this.host = Deno.env.get('HOST') || '0.0.0.0';
	}

	/**
	 * Get the server URL for display
	 */
	get serverUrl(): string {
		const displayHost = this.host === '0.0.0.0' ? 'localhost' : this.host;
		return `http://${displayHost}:${this.port}`;
	}

	/**
	 * Initialize the configuration (create directories)
	 * Must be called before using the config
	 */
	async init(): Promise<void> {
		await Deno.mkdir(this.paths.logs, { recursive: true });
		await Deno.mkdir(this.paths.data, { recursive: true });
		await Deno.mkdir(this.paths.backups, { recursive: true });
		await Deno.mkdir(this.paths.databases, { recursive: true });
	}

	/**
	 * Set the base path for the application
	 */
	setBasePath(path: string): void {
		this.basePath = path;
	}

	/**
	 * Application paths (relative to base)
	 */
	readonly paths = {
		get base(): string {
			return config.basePath;
		},
		get logs(): string {
			return `${config.basePath}/logs`;
		},
		get logFile(): string {
			return `${config.basePath}/logs/app.log`;
		},
		get data(): string {
			return `${config.basePath}/data`;
		},
		get database(): string {
			return `${config.basePath}/data/profilarr.db`;
		},
		get databases(): string {
			return `${config.basePath}/data/databases`;
		},
		get backups(): string {
			return `${config.basePath}/backups`;
		}
	};
}

export const config = new Config();
