class TranslationModel {
  final int id;
  final String locale;

  TranslationModel({required this.id, required this.locale});

  Map<String, dynamic> toJson() {
    return {'id': id, 'language': locale};
  }

  factory TranslationModel.fromJson(Map<String, dynamic> json) {
    return TranslationModel(id: json['id'] ?? 0, locale: json['locale'] ?? '');
  }
}

class Files {
  final String? youtube;
  final String? video;
  final String? audio;
  final String? booklet;
  final String? simple;

  Files({this.youtube, this.video, this.audio, this.booklet, this.simple});

  Map<String, dynamic> toJson() {
    return {
      'youtube': youtube,
      'video': video,
      'audio': audio,
      'booklet': booklet,
      'simple': simple,
    };
  }

  factory Files.fromJson(Map<String, dynamic> json) {
    return Files(
      youtube: json['youtube'] as String?,
      video: json['video'] as String?,
      audio: json['audio'] as String?,
      booklet: json['booklet'] as String?,
      simple: json['simple'] as String?,
    );
  }
}

class DurationField {
  final int duration;

  DurationField({required this.duration});

  factory DurationField.fromJson(Map<String, dynamic> json) {
    // convert duration from string to int
    final dynamic rawDuration = json['duration'];
    final int parsedDuration = rawDuration is String
        ? int.tryParse(rawDuration) ?? 0
        : rawDuration is int
            ? rawDuration
            : 0;
    return DurationField(duration: parsedDuration);
  }
}

class ItemModel {
  final String id;
  final String title;
  final DateTime date;
  final String type;
  final String? slug;
  bool rm;
  String biblestudy;
  bool draft;
  int timestamp;
  String activity;
  DurationField duration;
  String place;
  String city;
  String state;
  String country;
  // List<String> type;
  String thumbnail;
  List<TranslationModel> translations;
  Files files;

  ItemModel({
    required this.id,
    required this.title,
    required this.date,
    this.type = '',
    this.slug,
    this.rm = false,
    this.biblestudy = '',
    this.draft = false,
    this.timestamp = 0,
    this.activity = '',
    required this.duration,
    this.place = '',
    this.city = '',
    this.state = '',
    this.country = '',
    // this.type = const [],
    this.thumbnail = '',
    this.translations = const [],
    Files? files,
  }) : files = files ?? Files();

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'date': date.toIso8601String(),
      'type': type,
      'slug': slug,
      'rm': rm,
      'biblestudy': biblestudy,
      'draft': draft,
      'timestamp': timestamp,
      'activity': activity,
      'duration': duration,
      'place': place,
      'city': city,
      'state': state,
      'country': country,
      'thumbnail': thumbnail.toString(),
      'translations': translations.toString(),
      'files': files.toString(),
    };
  }

  factory ItemModel.fromJson(Map<String, dynamic> json) {
    return ItemModel(
      id: json['id'] ?? '',
      title: json['title'] ?? '',
      date:
          json['date'] != null
              ? DateTime.parse(json['date'].toString())
              : DateTime.now(),
      type: json['type'] ?? '',
      slug: json['slug'],
      rm: json['rm'] ?? false,
      biblestudy: json['biblestudy'] ?? '',
      draft: json['draft'] ?? false,
      timestamp: json['timestamp'] ?? 0,
      activity: json['activity'] ?? '',
      duration: DurationField.fromJson({'duration': json['duration']}),
      place: json['place'] ?? '',
      city: json['city'] ?? '',
      state: json['state'] ?? '',
      country: json['country'] ?? '',
      // type: List<String>.from(json['type'] ?? []),
      thumbnail: json['thumbnail'] ?? '',
      translations:
          (json['translations'] as List<dynamic>?)
              ?.map(
                (item) =>
                    TranslationModel.fromJson(item as Map<String, dynamic>),
              )
              .toList() ??
          [],
      files:
          json['files'] != null
              ? (json['files'] is Map<String, dynamic>
                  ? Files.fromJson(json['files'] as Map<String, dynamic>)
                  : Files())
              : Files(),
    );
  }
}

class ResultModel {
  final List<YearModel> years;

  ResultModel({required this.years});

  factory ResultModel.fromJson(Map<String, dynamic> json) {
    return ResultModel(
      years: (json['years'] as List)
          .map((e) => YearModel.fromJson(e))
          .toList(),
    );
  }
}

class YearModel {
  final int year;
  final int count;
  final List<MonthModel> months;

  YearModel({required this.year, required this.count, required this.months});

  factory YearModel.fromJson(Map<String, dynamic> json) {
    return YearModel(
      year: json['year'],
      count: json['count'],
      months: (json['months'] as List)
          .map((e) => MonthModel.fromJson(e))
          .toList(),
    );
  }
}

class MonthModel {
  final String name;
  final String month;
  final int count;

  MonthModel({required this.name, required this.month, required this.count});

  factory MonthModel.fromJson(Map<String, dynamic> json) {
    return MonthModel(
      name: json['name'],
      month: json['month'],
      count: int.parse(json['count']),
    );
  }
}

