import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/network/dio_client.dart';
import '../domain/models/referral_info.dart';

class ReferralRepository {
  final Dio _dio;

  ReferralRepository(this._dio);

  Future<ReferralInfo> getMyReferralInfo() async {
    final res = await _dio.get('/api/v1/referral/me');
    return ReferralInfo.fromJson(res.data as Map<String, dynamic>);
  }

  Future<({List<ReferralItem> items, int total})> getMyReferrals({
    int limit = 20,
    int offset = 0,
  }) async {
    final res = await _dio.get(
      '/api/v1/referral/me/referrals',
      queryParameters: {'limit': limit, 'offset': offset},
    );
    final data = res.data as Map<String, dynamic>;
    final items = (data['items'] as List)
        .map((e) => ReferralItem.fromJson(e as Map<String, dynamic>))
        .toList();
    return (items: items, total: data['total'] as int? ?? 0);
  }

  Future<({List<RewardItem> items, int total})> getMyRewards({
    String? status,
    int limit = 20,
    int offset = 0,
  }) async {
    final res = await _dio.get(
      '/api/v1/referral/me/rewards',
      queryParameters: {
        if (status != null) 'status': status,
        'limit': limit,
        'offset': offset,
      },
    );
    final data = res.data as Map<String, dynamic>;
    final items = (data['items'] as List)
        .map((e) => RewardItem.fromJson(e as Map<String, dynamic>))
        .toList();
    return (items: items, total: data['total'] as int? ?? 0);
  }

  // ── User-level / personal circle ─────────────────────────────────────────

  Future<UserReferralInfo> getMyUserReferralInfo() async {
    final res = await _dio.get('/api/v1/referral/me/user');
    return UserReferralInfo.fromJson(res.data as Map<String, dynamic>);
  }

  Future<({List<CircleMember> items, int total})> getMyCircle({
    int limit = 20,
    int offset = 0,
  }) async {
    final res = await _dio.get(
      '/api/v1/referral/me/circle',
      queryParameters: {'limit': limit, 'offset': offset},
    );
    final data = res.data as Map<String, dynamic>;
    final items = (data['items'] as List)
        .map((e) => CircleMember.fromJson(e as Map<String, dynamic>))
        .toList();
    return (items: items, total: data['total'] as int? ?? 0);
  }

  Future<({int balance, int totalEarned, int totalSpent, List<PointTransaction> transactions, int total})>
      getMyPoints({int limit = 20, int offset = 0}) async {
    final res = await _dio.get(
      '/api/v1/referral/me/points',
      queryParameters: {'limit': limit, 'offset': offset},
    );
    final data = res.data as Map<String, dynamic>;
    final bal = data['balance'] as Map<String, dynamic>? ?? {};
    final txData = data['transactions'] as Map<String, dynamic>? ?? {};
    final txList = (txData['items'] as List? ?? [])
        .map((e) => PointTransaction.fromJson(e as Map<String, dynamic>))
        .toList();
    return (
      balance: bal['balance'] as int? ?? 0,
      totalEarned: bal['totalEarned'] as int? ?? 0,
      totalSpent: (bal['totalSpent'] as int? ?? 0).abs(),
      transactions: txList,
      total: txData['total'] as int? ?? 0,
    );
  }
}

// ── Riverpod provider ────────────────────────────────────────────────────────

final referralRepositoryProvider = Provider<ReferralRepository>((ref) {
  final dio = ref.watch(dioClientProvider);
  return ReferralRepository(dio);
});
