// lib/models/profile.dart
class Profile {
  final String profilID;
  final String name;
  final String? gruppename;
  final String? gruppeID;
  final DateTime? createdAt;
  final List<int> seItems;
  final List<int> feItems;

  Profile({
    required this.profilID,
    required this.name,
    this.gruppename,
    this.gruppeID,
    this.createdAt,
    required this.seItems,
    required this.feItems,
  });

  factory Profile.fromJson(Map<String, dynamic> json) {
    List<int> se = [];
    List<int> fe = [];
    
    for (int i = 1; i <= 36; i++) {
      se.add(json['item$i'] ?? 2);
      fe.add(json['feitem$i'] ?? 2);
    }
    
    return Profile(
      profilID: json['profilID'].toString(),
      name: json['name'],
      gruppename: json['gruppename'],
      gruppeID: json['gruppeID']?.toString(),
      createdAt: json['created_at'] != null 
          ? DateTime.tryParse(json['created_at']) 
          : null,
      seItems: se,
      feItems: fe,
    );
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{
      'name': name,
      'gruppename': gruppename,
      'gruppeID': gruppeID,
    };
    
    for (int i = 0; i < 36; i++) {
      map['item${i+1}'] = seItems[i];
      map['feitem${i+1}'] = feItems[i];
    }
    
    return map;
  }

  Profile copyWith({
    String? profilID,
    String? name,
    String? gruppename,
    String? gruppeID,
    List<int>? seItems,
    List<int>? feItems,
  }) {
    return Profile(
      profilID: profilID ?? this.profilID,
      name: name ?? this.name,
      gruppename: gruppename ?? this.gruppename,
      gruppeID: gruppeID ?? this.gruppeID,
      createdAt: createdAt,
      seItems: seItems ?? this.seItems,
      feItems: feItems ?? this.feItems,
    );
  }
}