// مدل‌های کاتالوگ فروشگاه (پاسخ GET /api/shop).

class CoinPackage {
  final String id;
  final String title;
  final int coins;
  final int vipDays;
  final int priceToman;
  final int bonusPct;

  CoinPackage.fromJson(Map<String, dynamic> j)
      : 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;
}

class TicketPackage {
  final String id;
  final String title;
  final int tickets;
  final int priceToman;

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

class CardSkin {
  final String id;
  final String title;
  final int priceCoins;

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

class Booster {
  final String id;
  final String title;
  final int multiplier;
  final int hours;
  final int priceToman;

  Booster.fromJson(Map<String, dynamic> j)
      : 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;
}

/// داده‌ی کامل فروشگاه: کاتالوگ + کارت‌های متعلق به کاربر + کارت انتخابی.
class ShopData {
  final List<CoinPackage> coinPackages;
  final List<TicketPackage> ticketPackages;
  final List<CardSkin> cardSkins;
  final List<Booster> boosters;
  final List<String> ownedCards;
  final String selectedCard;

  ShopData({
    required this.coinPackages,
    required this.ticketPackages,
    required this.cardSkins,
    required this.boosters,
    required this.ownedCards,
    required this.selectedCard,
  });

  factory ShopData.fromJson(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', CoinPackage.fromJson),
      ticketPackages: parse('ticket_packages', TicketPackage.fromJson),
      cardSkins: parse('card_skins', CardSkin.fromJson),
      boosters: parse('boosters', Booster.fromJson),
      ownedCards: ((j['owned_cards'] as List?) ?? []).map((e) => e as String).toList(),
      selectedCard: (j['selected_card'] ?? 'simple') as String,
    );
  }

  bool owns(String cardId) => cardId == 'simple' || ownedCards.contains(cardId);
}
