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

/// Coordinates selected planning date state for FocusFlow.
library;

import 'package:flutter/foundation.dart';
import 'package:scheduler_core/scheduler_core.dart';

/// Reads the currently selected owner-local planning date.
typedef SelectedDateReader = CivilDate Function();

/// Owns the visible owner-local planning day.
class SelectedDateController extends ChangeNotifier {
  /// Creates a selected-date controller initialized to [initialDate].
  SelectedDateController({required CivilDate initialDate})
    : _date = initialDate;

  /// Private state storing the currently selected date.
  CivilDate _date;

  /// Current visible owner-local planning date.
  CivilDate get date => _date;

  /// Selects [date] as the visible planning day.
  bool selectDate(CivilDate date) {
    if (date == _date) {
      logger.finer(() => 'Selected date unchanged. date=${date.toIsoString()}');
      return false;
    }
    logger.debug(
      () =>
          'Selected date changed. previous=${_date.toIsoString()} next=${date.toIsoString()}',
    );
    _date = date;
    notifyListeners();
    return true;
  }

  /// Moves the selected planning day backward by one civil day.
  bool selectPreviousDay() {
    return selectDate(_date.addDays(-1));
  }

  /// Moves the selected planning day forward by one civil day.
  bool selectNextDay() {
    return selectDate(_date.addDays(1));
  }
}
