import 'package:flutter_riverpod/flutter_riverpod.dart';

class UserState {
  final String? accountSequence;
  final String? nickname;
  final String? phone;
  final bool isLoggedIn;

  UserState({
    this.accountSequence,
    this.nickname,
    this.phone,
    this.isLoggedIn = false,
  });

  UserState copyWith({
    String? accountSequence,
    String? nickname,
    String? phone,
    bool? isLoggedIn,
  }) {
    return UserState(
      accountSequence: accountSequence ?? this.accountSequence,
      nickname: nickname ?? this.nickname,
      phone: phone ?? this.phone,
      isLoggedIn: isLoggedIn ?? this.isLoggedIn,
    );
  }
}

class UserNotifier extends StateNotifier<UserState> {
  UserNotifier() : super(UserState());

  void login({
    required String accountSequence,
    String? nickname,
    String? phone,
  }) {
    state = state.copyWith(
      accountSequence: accountSequence,
      nickname: nickname,
      phone: phone,
      isLoggedIn: true,
    );
  }

  void logout() {
    state = UserState();
  }

  void setMockUser() {
    state = state.copyWith(
      accountSequence: '1001',
      nickname: '测试用户',
      phone: '138****8888',
      isLoggedIn: true,
    );
  }
}

final userNotifierProvider = StateNotifierProvider<UserNotifier, UserState>(
  (ref) => UserNotifier(),
);

final currentAccountSequenceProvider = Provider<String?>((ref) {
  return ref.watch(userNotifierProvider).accountSequence;
});
