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

/// نگاشتِ JSON کاتالوگ فروشگاه به موجودیت‌ها.
class ShopMapper {
  static CoinPackage coin(Map<String, dynamic> j) => CoinPackage(
        id: j['id'] as String,
        title: j['title'] as String,
        coins: (j['coins'] ?? 0) as int,
        vipDays: (j['vip_days'] ?? 0) as int,
        priceToman: (j['price_toman'] ?? 0) as int,
        bonusPct: (j['bonus_pct'] ?? 0) as int,
        sku: (j['sku'] ?? '') as String,
      );

  static TicketPackage ticket(Map<String, dynamic> j) => TicketPackage(
        id: j['id'] as String,
        title: j['title'] as String,
        tickets: (j['tickets'] ?? 0) as int,
        priceToman: (j['price_toman'] ?? 0) as int,
        sku: (j['sku'] ?? '') as String,
      );

  static CardSkin card(Map<String, dynamic> j) => CardSkin(
        id: j['id'] as String,
        title: j['title'] as String,
        priceCoins: (j['price_coins'] ?? 0) as int,
      );

  static Booster booster(Map<String, dynamic> j) => Booster(
        id: j['id'] as String,
        title: j['title'] as String,
        multiplier: (j['multiplier'] ?? 1) as int,
        hours: (j['hours'] ?? 0) as int,
        priceToman: (j['price_toman'] ?? 0) as int,
        sku: (j['sku'] ?? '') as String,
      );

  static VipPackage vip(Map<String, dynamic> j) => VipPackage(
        id: j['id'] as String,
        title: j['title'] as String,
        months: (j['months'] ?? 1) as int,
        priceToman: (j['price_toman'] ?? 0) as int,
        sku: (j['sku'] ?? '') as String,
      );

  static ShopData shopData(Map<String, dynamic> j) {
    final cat = Map<String, dynamic>.from(j['catalog'] as Map);
    List<T> parse<T>(String key, T Function(Map<String, dynamic>) f) =>
        ((cat[key] as List?) ?? [])
            .map((e) => f(Map<String, dynamic>.from(e as Map)))
            .toList();
    return ShopData(
      coinPackages: parse('coin_packages', coin),
      ticketPackages: parse('ticket_packages', ticket),
      cardSkins: parse('card_skins', card),
      boosters: parse('boosters', booster),
      vipPackages: parse('vip_packages', vip),
      ownedCards:
          ((j['owned_cards'] as List?) ?? []).map((e) => e as String).toList(),
      selectedCard: (j['selected_card'] ?? 'simple') as String,
    );
  }
}
