import 'package:flutter/widgets.dart';

import '../services/auto_backup_service.dart';

/// Drives the automatic backup off the app lifecycle: once on startup and again
/// each time the app becomes hidden (backgrounded on mobile, window hidden on
/// desktop). The [AutoBackupService] decides whether a copy is actually due, so
/// these are cheap no-ops most of the time.
///
/// When [service] is null (widget tests, or platforms without file storage such
/// as web) it wraps [child] untouched and does nothing.
class AutoBackupGate extends StatefulWidget {
  const AutoBackupGate({required this.child, this.service, super.key});

  final Widget child;
  final AutoBackupService? service;

  @override
  State<AutoBackupGate> createState() => _AutoBackupGateState();
}

class _AutoBackupGateState extends State<AutoBackupGate> {
  AppLifecycleListener? _listener;

  @override
  void initState() {
    super.initState();
    final service = widget.service;
    if (service == null) return;
    _listener = AppLifecycleListener(onHide: () => service.runIfDue());
    WidgetsBinding.instance.addPostFrameCallback((_) => service.runIfDue());
  }

  @override
  void dispose() {
    _listener?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => widget.child;
}
