import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import 'package:im_app/app/di/app_providers.dart';
import 'package:im_app/features/settings/di/settings_providers.dart';

part 'theme_view_model.g.dart';

/// 主题 ViewModel
///
/// View 层只感知此 ViewModel，不直接依赖 app 级 Provider。
///
/// ## 数据流
///
/// ```
/// ThemeView
///   → ref.watch(themeViewModelProvider)              ← 当前 ThemeMode
///   → ref.read(themeViewModelProvider.notifier).setMode(mode)
///     → ★ ThemeViewModel.setMode() ★               ← 你在这里
///       → SetThemeUseCase.execute()
///         → 幂等校验（相同模式直接返回）
///         → onApply → ThemeModeNotifier.setMode()   ← 更新内存状态
///         → TODO: 持久化（storage_sdk）
/// ```
@riverpod
class ThemeViewModel extends _$ThemeViewModel {
  @override
  ThemeMode build() => ref.watch(themeModeProvider);

  void setMode(ThemeMode mode) {
    ref.read(setThemeUseCaseProvider).execute(
          current: state,
          requested: mode,
          onApply: (m) => ref.read(themeModeProvider.notifier).setMode(m),
        );
  }
}
