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

/// Runs the Backup command for the Scheduler Backup package.
library;

import 'dart:io';

import 'package:scheduler_backup/backup.dart';

/// Entry point for this executable Dart file.
/// It wires the local command, test, or application behavior needed by this repository without exposing additional public API.
Future<void> main(List<String> arguments) async {
  final passphrase = _option(arguments, '--passphrase') ??
      Platform.environment['SCHEDULER_BACKUP_PASSPHRASE'];
  if (passphrase == null || passphrase.isEmpty) {
    stderr.writeln(
      'Usage: dart run scheduler_backup:backup --passphrase <passphrase>',
    );
    exitCode = 64;
    return;
  }

  try {
    final backup = await createEncryptedBackup(passphrase: passphrase);
    stdout.writeln(backup.path);
  } on Object catch (error) {
    stderr.writeln(error);
    exitCode = 1;
  }
}

/// Top-level helper that performs the `_option` 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? _option(List<String> arguments, String name) {
  final index = arguments.indexOf(name);
  if (index == -1 || index + 1 >= arguments.length) {
    return null;
  }
  return arguments[index + 1];
}
