import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:my_attendance/models/settings.dart';

class SettingsService with ChangeNotifier {
  static const String _workDurationKey = 'dailyWorkDuration';

  AppSettings _settings = const AppSettings();
  AppSettings get settings => _settings;

  bool _isLoading = true;
  bool get isLoading => _isLoading;

  SettingsService() {
    loadSettings();
  }

  Future<void> loadSettings() async {
    _isLoading = true;
    notifyListeners();

    final prefs = await SharedPreferences.getInstance();
    final durationMinutes = prefs.getInt(_workDurationKey);

    if (durationMinutes != null) {
      _settings = AppSettings(
        dailyWorkDuration: Duration(minutes: durationMinutes),
      );
    }

    _isLoading = false;
    notifyListeners();
  }

  Future<void> saveWorkDuration(Duration newDuration) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt(_workDurationKey, newDuration.inMinutes);
    _settings = _settings.copyWith(dailyWorkDuration: newDuration);
    notifyListeners();
  }
}
