import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../core/network/dio_client.dart';
import '../data/models/asset/withdraw_record.dart';
import '../data/services/withdraw_service.dart';

// ══════════════════════════════════════════════════════════════
// Withdraw Detail State
// ═════════════════���════════════════════════════════════════════

class WithdrawDetailState {
  final WithdrawRecord? record;
  final bool isLoading;
  final bool isCancelling;
  final String? errorMessage;

  const WithdrawDetailState({
    this.record,
    this.isLoading = false,
    this.isCancelling = false,
    this.errorMessage,
  });

  WithdrawDetailState copyWith({
    WithdrawRecord? record,
    bool? isLoading,
    bool? isCancelling,
    String? errorMessage,
  }) =>
      WithdrawDetailState(
        record: record ?? this.record,
        isLoading: isLoading ?? this.isLoading,
        isCancelling: isCancelling ?? this.isCancelling,
        errorMessage: errorMessage,
      );
}

// ══════════════════════════════════════════════════════════════
// Withdraw Detail Notifier
// ══════════════════════════════════════════════════════════════

class WithdrawDetailNotifier extends Notifier<WithdrawDetailState> {
  @override
  WithdrawDetailState build() {
    return const WithdrawDetailState();
  }

  /// 加载单条提现/转账记录
  Future<void> loadRecord(WithdrawRecord initialRecord, {required bool isTransfer}) async {
    state = state.copyWith(isLoading: true, errorMessage: null);
    try {
      final dio = ref.read(dioClientProvider);
      final service = WithdrawService(dio);

      final record = isTransfer
          ? await service.getTransferRecord(initialRecord.id)
          : await service.getWithdrawRecord(initialRecord.id);

      state = state.copyWith(record: record, isLoading: false);
    } catch (e) {
      state = state.copyWith(isLoading: false, errorMessage: e.toString());
    }
  }

  /// 取消提现/转账
  Future<bool> cancel({required bool isTransfer}) async {
    final record = state.record;
    if (record == null) return false;

    state = state.copyWith(isCancelling: true, errorMessage: null);
    try {
      final dio = ref.read(dioClientProvider);
      final service = WithdrawService(dio);

      if (isTransfer) {
        await service.cancelTransfer(record.id);
      } else {
        await service.cancelWithdraw(record.id);
      }

      // 取消成功后刷新数据
      await loadRecord(record, isTransfer: isTransfer);
      state = state.copyWith(isCancelling: false);
      return true;
    } catch (e) {
      state = state.copyWith(isCancelling: false, errorMessage: e.toString());
      return false;
    }
  }

  /// 刷新记录
  Future<void> refresh({required bool isTransfer}) async {
    final record = state.record;
    if (record != null) {
      await loadRecord(record, isTransfer: isTransfer);
    }
  }
}

// ══════════════════════════════════════════════════════════════
// Provider
// ══════════════════════════════════════════════════════════════

final withdrawDetailProvider =
    NotifierProvider<WithdrawDetailNotifier, WithdrawDetailState>(
      WithdrawDetailNotifier.new,
    );
