// SPDX-FileCopyrightText: 2026 FocusFlow contributors
// SPDX-License-Identifier: AGPL-3.0-only

part of '../../encrypted_backup.dart';

/// Default scheduler SQLite database path.
File defaultSchedulerSqliteFile() {
  return File(_joinPath(
    _homeDirectory().path,
    defaultSchedulerDirectoryName,
    defaultSchedulerDatabaseFileName,
  ));
}

/// Default encrypted backup output directory.
Directory defaultSchedulerBackupDirectory() {
  return Directory(_joinPath(
    _homeDirectory().path,
    defaultSchedulerDirectoryName,
    defaultSchedulerBackupDirectoryName,
  ));
}

/// Top-level helper that performs the `_homeDirectory` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
Directory _homeDirectory() {
  final home = Platform.environment['HOME'] ??
      Platform.environment['USERPROFILE'] ??
      Platform.environment['HOMEDRIVE'];
  if (home == null || home.trim().isEmpty) {
    logger.warn(
        () => 'Backup home directory could not be resolved from environment.');
    throw const BackupException('Home directory could not be resolved.');
  }
  return Directory(home);
}

/// Top-level helper that performs the `_joinPath` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
String _joinPath(String first, String second, [String? third]) {
  final separator = Platform.pathSeparator;
  final left = first.endsWith(separator)
      ? first.substring(0, first.length - separator.length)
      : first;
  final middle = second.startsWith(separator) ? second.substring(1) : second;
  final joined = '$left$separator$middle';
  if (third == null) {
    return joined;
  }
  final right = third.startsWith(separator) ? third.substring(1) : third;
  return '$joined$separator$right';
}
