import '../../domain/entities/profile_entity.dart';

/// نگاشتِ پاسخ‌های /me، /wallet و /stats به ProfileEntity.
class ProfileModel {
  static ProfileEntity fromJson(
    Map<String, dynamic> user,
    Map<String, dynamic> wallet,
    Map<String, dynamic> stats,
  ) {
    final name = (user['first_name'] as String?)?.trim();
    final avatar = (user['avatar'] as String?)?.trim();
    final s = stats['stats'] as Map?;
    return ProfileEntity(
      name: (name == null || name.isEmpty) ? 'بازیکن' : name,
      avatar: (avatar == null || avatar.isEmpty) ? 'hakem-1' : avatar,
      avatarImg: (wallet['selected_avatar_img'] ?? '') as String,
      mobile: (user['mobile'] as String?) ?? '',
      level: (wallet['level'] ?? 1) as int,
      trophies: (wallet['trophies'] ?? 0) as int,
      xpInto: (wallet['xp_into_level'] ?? 0) as int,
      xpNext: (wallet['xp_for_next'] ?? 1) as int,
      vip: (stats['vip'] ?? false) as bool,
      rankPoints: (wallet['rank_points'] ?? 0) as int,
      rankTier: (wallet['rank_tier'] ?? 'bronze') as String,
      stats: s == null
          ? null
          : ProfileStats(
              games: (s['games'] ?? 0) as int,
              wins: (s['wins'] ?? 0) as int,
              losses: (s['losses'] ?? 0) as int,
              kotMade: (s['kot_made'] ?? 0) as int,
              kotReceived: (s['kot_received'] ?? 0) as int,
              cuts: (s['cuts'] ?? 0) as int,
              hakemCount: (s['hakem_count'] ?? 0) as int,
            ),
    );
  }
}
