import 'package:im_app/app/di/user_provider.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:im_app/data/local/drift/app_database.dart';
import 'package:im_app/domain/entities/user.dart';
import 'package:im_app/domain/repositories/user_repository.dart';

part 'user_notifier.g.dart';

/// 单个用户状态管理 (family — 每个 uid 独立 notifier)
///
/// ## 用法
/// ```dart
/// // 监听 — 自动重建
/// final userAsync = ref.watch(userNotifierProvider(123));
///
/// // 即时读取，无需 await
/// final user = ref.read(userNotifierProvider(123).notifier).current;
///
/// // 部分更新
/// ref.read(userNotifierProvider(123).notifier).updateFields(
///   UsersCompanion(nickname: Value('New Name')),
/// );
/// ```
@riverpod
class UserNotifier extends _$UserNotifier {
  User? _cached;

  UserRepository get _repo => ref.watch(userRepositoryProvider);

  @override
  Future<User?> build(int uid) async {
    ref.onDispose(() => _cached = null);

    // Stream starts automatically — uid is the family arg
    _repo.watchUser(uid).listen((user) {
      _cached = user;
      state = AsyncData(user);
    });

    // Return initial DB value
    return _repo.getUser(uid);
  }

  // ── 即时访问，无需 await ──────────────────────────────────────────────────

  User? get current => _cached;

  // ── 写入 ─────────────────────────────────────────────────────────────────

  Future<void> saveUser(User user) async {
    await _repo.saveUser(user);
  }

  Future<void> updateFields(UsersCompanion companion) async {
    await _repo.updateFields(uid, companion); // uid from build arg
  }

  Future<void> updateUser(User user) async {
    await _repo.saveUser(user);
  }

  // ── 删除 ─────────────────────────────────────────────────────────────────

  Future<void> deleteUser() async {
    await _repo.deleteUser(uid);
    _cached = null;
    state = const AsyncData(null);
  }
}
