import 'package:krezus/repositories/metadata/models/budget.dart';
import 'package:krezus/repositories/metadata/models/category.dart';
import 'package:krezus/repositories/json/models/json.dart';
import 'package:krezus/repositories/metadata/models/account.dart';
import 'package:krezus/repositories/metadata/models/settings.dart';

class Metadata implements Json {
  List<Budget> budgets;
  List<Category> categories;
  List<Account> accounts;
  Settings settings;

  Metadata({
    this.budgets = const [],
    this.categories = const [],
    this.accounts = const [],
    this.settings = const Settings(),
  });

  @override
  Map<String, dynamic> toJson() => {
    'budgets': budgets.map((budget) => budget.toJson()).toList(),
    'categories': categories.map((category) => category.toJson()).toList(),
    'accounts': accounts.map((account) => account.toJson()).toList(),
  };
  
  @override
  String getJsonFileName() {
    return 'metadata.json';
  }
}

class MetadataFactory implements JsonFactory<Metadata> {
  @override
  Metadata fromJson(Map<String, dynamic> json) {
    return Metadata(
      budgets: List<Budget>.from(json['budgets']?.map((budget) => Budget.fromJson(budget))),
      categories: List<Category>.from(json['categories']?.map((category) => Category.fromJson(category))),
      accounts: List<Account>.from(json['accounts']?.map((account) => Account.fromJson(account))),
    );
  }
}