// lib/models/team.dart

import 'category.dart';
import 'user.dart';

class Team {
  final int id;
//  final String zekkenNumber;
  final String teamName;
  final NewCategory category;
  final User owner;
  List<User> members;  // membersフィールドを追加

  Team({
    required this.id,
//    required this.zekkenNumber,
    required this.teamName,
    required this.category,
    required this.owner,
    this.members = const [],  // デフォルト値を空のリストに設定
  });

  factory Team.fromJson(Map<String, dynamic> json) {
    return Team(
      id: json['id'] ?? 0,
      //zekkenNumber: json['zekken_number'] ?? 'Unknown',
      teamName: json['team_name'] ?? 'Unknown Team',
      category: json['category'] != null
          ? NewCategory.fromJson(json['category'])
          : NewCategory(id: 0, categoryName: 'Unknown', categoryNumber: 0, duration: Duration.zero, numOfMember: 1, family: false, female: false),
      owner: json['owner'] != null
          ? User.fromJson(json['owner'])
          : User(id: 0, email: 'unknown@example.com', firstname: 'Unknown', lastname: 'User', dateOfBirth: null, female: false, isActive: false),
      members: json['members'] != null  // membersフィールドを解析
          ? List<User>.from(json['members'].map((x) => User.fromJson(x)))
          : [],
    );
  }


  Map<String, dynamic> toJson() {
    return {
      'id': id,
      //'zekken_number': zekkenNumber,
      'team_name': teamName,
      'category': category.toJson(),
      'owner': owner.toJson(),
      'members': members.map((member) => member.toJson()).toList(),  // membersフィールドをJSONに変換
    };
  }
}