
import 'package:be_happy/domain/entities/subscription_period.dart';

class Subscription {
  final int id;
  final String title;
  final String shortDescription;
  final String fullDescription;
  final String fullDescriptionJson;
  final int planId;
  final bool isActive;
  final bool isCurrent;
  final String currency;
  final DateTime? activeFrom;
  final DateTime? activeTo;
  final DateTime createdAt;
  final DateTime updatedAt;
  final List<SubscriptionPeriod> options;


  Subscription({
    required this.id,
    required this.title,
    required this.shortDescription,
    required this.fullDescription,
    required this.fullDescriptionJson,
    required this.planId,
    required this.isActive,
    required this.currency,
    this.activeFrom,
    this.activeTo,
    required this.createdAt,
    required this.updatedAt,
    required this.isCurrent,
    required this.options,
  });

  factory Subscription.fromJson(Map<String, dynamic> json) {
    final currencyData = json['currency'] as Map<String, dynamic>? ?? {};
    final optionsData = json['options'] as List<dynamic>? ?? [];


    return Subscription(
      id: json['id'] ?? 0,
      title: json['title'] ?? '',
      shortDescription: json['shortDescription'] ?? '',
      fullDescription: json['fullDescription'] ?? '',
      fullDescriptionJson: json['fullDescriptionJson'] ?? '',
      planId: json['planId'] ?? 0,
      isActive: json['isActive'] ?? false,
      currency: currencyData['currency'] ?? 'BYN',
      activeFrom: json['activeFrom'] != null ? DateTime.parse(json['activeFrom']) : null,
      activeTo: json['activeTo'] != null ? DateTime.parse(json['activeTo']) : null,
      createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt']) : DateTime.now(),
      updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : DateTime.now(),
      isCurrent: json['isCurrent'] ?? false,
      options: optionsData.map((e) => SubscriptionPeriod.fromJson(e as Map<String, dynamic>)).toList(),
    );
  }

  @override
  String toString() {
    return 'Subscription{id: $id, title: $title, isActive: $isActive}';
  }
}