import 'dart:convert'; /// Represents a budget envelope per category with currency awareness. class BudgetCategory { /// Unique identifier of the category entry. final String id; /// Category name (hébergement, transport, food, activités...). final String name; /// Planned amount. final double planned; /// Currency code (ISO 4217) used for the amount. final String currency; /// Amount actually spent (to be filled by expenses sync later). final double spent; /// Creation timestamp for ordering. final DateTime createdAt; /// Creates a budget category entry. const BudgetCategory({ required this.id, required this.name, required this.planned, required this.currency, required this.spent, required this.createdAt, }); /// Convenience constructor for new envelope. factory BudgetCategory.newCategory({ required String id, required String name, required double planned, required String currency, }) { return BudgetCategory( id: id, name: name, planned: planned, currency: currency, spent: 0, createdAt: DateTime.now().toUtc(), ); } /// Returns a copy with updated fields. BudgetCategory copyWith({ String? id, String? name, double? planned, String? currency, double? spent, DateTime? createdAt, }) { return BudgetCategory( id: id ?? this.id, name: name ?? this.name, planned: planned ?? this.planned, currency: currency ?? this.currency, spent: spent ?? this.spent, createdAt: createdAt ?? this.createdAt, ); } /// JSON serialization. Map toJson() { return { 'id': id, 'name': name, 'planned': planned, 'currency': currency, 'spent': spent, 'createdAt': createdAt.toIso8601String(), }; } /// JSON deserialization. factory BudgetCategory.fromJson(Map json) { return BudgetCategory( id: json['id'] as String, name: json['name'] as String, planned: (json['planned'] as num).toDouble(), currency: json['currency'] as String, spent: (json['spent'] as num).toDouble(), createdAt: DateTime.parse(json['createdAt'] as String), ); } /// Encodes a list to JSON string. static String encodeList(List categories) { return json.encode(categories.map((c) => c.toJson()).toList()); } /// Decodes a list from JSON string. static List decodeList(String raw) { final decoded = json.decode(raw) as List; return decoded .cast>() .map(BudgetCategory.fromJson) .toList(); } }