import 'package:domain/repository/main_repository.dart';
import 'package:plugin_platform/engine/toast/toast_engine.dart';
import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared/utils/log_utils.dart';

import 'feedback_detail_state.dart';

part 'feedback_detail_view_model.g.dart';

@riverpod
class FeedbackDetailViewModel extends _$FeedbackDetailViewModel with DioCancelableMixin {
  late final MainRepository _mainRepository;

  @override
  FeedbackDetailState build() {
    _mainRepository = ref.read(mainRepositoryProvider);
    final state = FeedbackDetailState();
    registerCancellation();
    return state;
  }

  /// 获取详情数据
  void fetchFeedbackDetail(String? id) async {
    final result = await _mainRepository.fetchFeedbackDetail(id: id, cancelToken: cancelToken);

    if (result.isSuccess) {
      state = state.copyWith(detail: result.data, isReplyState: result.data?.replies?.isNotEmpty);
    } else {
      ToastEngine.show(result.errorMsg ?? "UnKnow Error");
    }
  }
}
