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

/// SQLite repository implementations backed by [SchedulerDb].
library;

import 'dart:convert';
import 'dart:io';

import 'package:drift/drift.dart' as drift;
import 'package:drift/native.dart';
import 'package:scheduler_core/scheduler_core.dart' as core;
import 'package:scheduler_persistence/persistence.dart';
import 'package:sqlite3/sqlite3.dart' as sqlite;

import 'scheduler_db.dart';
part 'sqlite_repositories/sqlite_task_repository.dart';
part 'sqlite_repositories/sqlite_project_repository.dart';
part 'sqlite_repositories/sqlite_locked_block_repository.dart';
part 'sqlite_repositories/sqlite_settings_repository.dart';
part 'sqlite_repositories/sqlite_schedule_snapshot_repository.dart';
part 'sqlite_application/sqlite_application_runtime.dart';
part 'sqlite_application/sqlite_application_unit_of_work.dart';
part 'sqlite_application/sqlite_application_repositories.dart';
part 'sqlite_application/sqlite_application_task_repository.dart';
part 'sqlite_application/sqlite_application_project_repository.dart';
part 'sqlite_application/sqlite_application_locked_block_repository.dart';
part 'sqlite_application/sqlite_application_snapshot_repository.dart';
part 'sqlite_application/sqlite_application_activity_repository.dart';
part 'sqlite_application/sqlite_application_project_statistics_repository.dart';
part 'sqlite_application/sqlite_application_owner_settings_repository.dart';
part 'sqlite_application/sqlite_application_notice_repository.dart';
part 'sqlite_application/sqlite_application_operation_repository.dart';
part 'sqlite_application/sqlite_application_mapping.dart';

/// Private file-level value for `_snapshotRetention`.
/// Keeping this value near the helpers that use it avoids hidden global configuration while still preventing duplicate literals.
const _snapshotRetention = Duration(days: 30);

/// Private file-level value for `_epoch`.
/// Keeping this value near the helpers that use it avoids hidden global configuration while still preventing duplicate literals.
final _epoch = DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);

/// Adds focused helper behavior through `on`.
/// The extension keeps conversion or convenience logic near the type it supports while avoiding changes to the original model surface.
extension on int {
  /// Performs the `next` behavior for this scheduler component.
  /// The method provides a named entry point for this operation so callers do not need to know the lower-level state or persistence steps involved.
  core.Revision next() => core.Revision(this).next();
}

/// Top-level helper that performs the `_windowsOverlap` operation for this file.
/// Keeping the helper private makes the surrounding flow easier to read while preventing unrelated libraries from depending on implementation detail.
bool _windowsOverlap(core.SchedulingWindow left, core.SchedulingWindow right) {
  return left.start.isBefore(right.end) && right.start.isBefore(left.end);
}
