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

part of '../../application_layer.dart';

/// Owner-level settings needed by application use cases.
class OwnerSettings {
  /// Creates a `OwnerSettings` instance with the values required by its invariants.
  /// Callers should pass already-normalized scheduler data because validation and defaulting belong at the model or service boundary, not in arbitrary UI code.
  OwnerSettings({
    required String ownerId,
    required String timeZoneId,
    WallTime? dayStart,
    WallTime? dayEnd,
    this.compactModeEnabled = false,
    this.backlogStalenessSettings = const BacklogStalenessSettings(),
  })  : ownerId = _requiredTrimmed(ownerId, 'ownerId'),
        timeZoneId = _requiredTrimmed(timeZoneId, 'timeZoneId'),
        dayStart = dayStart ?? WallTime(hour: 0, minute: 0),
        dayEnd = dayEnd ?? WallTime(hour: 23, minute: 59) {
    if (this.dayStart.compareTo(this.dayEnd) >= 0) {
      throw ArgumentError.value(
        {'dayStart': this.dayStart, 'dayEnd': this.dayEnd},
        'dayEnd',
        'Day end must be after day start.',
      );
    }
  }

  /// Authorization-neutral owner scope.
  final String ownerId;

  /// Owner time-zone id used by date-sensitive application use cases.
  final String timeZoneId;

  /// Default local day start.
  final WallTime dayStart;

  /// Default local day end.
  final WallTime dayEnd;

  /// Persisted compact-mode preference.
  final bool compactModeEnabled;

  /// Configurable backlog age thresholds.
  final BacklogStalenessSettings backlogStalenessSettings;

  /// Return a copy with selected settings changed.
  OwnerSettings copyWith({
    String? ownerId,
    String? timeZoneId,
    WallTime? dayStart,
    WallTime? dayEnd,
    bool? compactModeEnabled,
    BacklogStalenessSettings? backlogStalenessSettings,
  }) {
    return OwnerSettings(
      ownerId: ownerId ?? this.ownerId,
      timeZoneId: timeZoneId ?? this.timeZoneId,
      dayStart: dayStart ?? this.dayStart,
      dayEnd: dayEnd ?? this.dayEnd,
      compactModeEnabled: compactModeEnabled ?? this.compactModeEnabled,
      backlogStalenessSettings:
          backlogStalenessSettings ?? this.backlogStalenessSettings,
    );
  }
}
