class InvitationTag {
  final int id;
  final String name;
  final String colorHex;
  final String iconName;

  InvitationTag({
    required this.id,
    required this.name,
    required this.colorHex,
    required this.iconName,
  });

  factory InvitationTag.fromJson(Map<String, dynamic> json) {
    return InvitationTag(
      id: json['id'],
      name: json['name'],
      colorHex: json['colorHex'],
      iconName: json['iconName'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'colorHex': colorHex,
      'iconName': iconName,
    };
  }
}

class InvitationCreator {
  final int id;
  final String displayName;
  final String? avatar;
  final String? phoneNumber;

  InvitationCreator({
    required this.id,
    required this.displayName,
    this.avatar,
    this.phoneNumber,
  });

  factory InvitationCreator.fromJson(Map<String, dynamic> json) {
    return InvitationCreator(
      id: json['id'],
      displayName: json['displayName'],
      avatar: json['avatar'],
      phoneNumber: json['phoneNumber'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'displayName': displayName,
      'avatar': avatar,
      'phoneNumber': phoneNumber,
    };
  }
}

class InvitationAttendee {
  final int id;
  final String displayName;
  final String? avatar;
  final DateTime joinedAt;
  final String? phoneNumber;

  InvitationAttendee({
    required this.id,
    required this.displayName,
    this.avatar,
    required this.joinedAt,
    this.phoneNumber,
  });

  factory InvitationAttendee.fromJson(Map<String, dynamic> json) {
    return InvitationAttendee(
      id: json['id'],
      displayName: json['displayName'],
      avatar: json['avatar'],
      joinedAt: DateTime.parse(json['joinedAt']),
      phoneNumber: json['phoneNumber'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'displayName': displayName,
      'avatar': avatar,
      'joinedAt': joinedAt.toIso8601String(),
      'phoneNumber': phoneNumber,
    };
  }
}

class Invitation {
  final int id;
  final String title;
  final String? description;
  final DateTime? dateTime;
  final String? location;
  final int maxParticipants;
  final int currentAttendees;
  final InvitationTag tag;
  final InvitationCreator creator;
  final DateTime createdAt;

  Invitation({
    required this.id,
    required this.title,
    this.description,
    this.dateTime,
    this.location,
    required this.maxParticipants,
    required this.currentAttendees,
    required this.tag,
    required this.creator,
    required this.createdAt,
  });

  factory Invitation.fromJson(Map<String, dynamic> json) {
    return Invitation(
      id: json['id'],
      title: json['title'],
      description: json['description'],
      dateTime: json['dateTime'] != null ? DateTime.parse(json['dateTime']) : null,
      location: json['location'],
      maxParticipants: json['maxParticipants'],
      currentAttendees: json['currentAttendees'],
      tag: InvitationTag.fromJson(json['tag']),
      creator: InvitationCreator.fromJson(json['creator']),
      createdAt: DateTime.parse(json['createdAt']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'dateTime': dateTime?.toIso8601String(),
      'location': location,
      'maxParticipants': maxParticipants,
      'currentAttendees': currentAttendees,
      'tag': tag.toJson(),
      'creator': creator.toJson(),
      'createdAt': createdAt.toIso8601String(),
    };
  }
}

class InvitationsResponse {
  final bool status;
  final String? message;
  final InvitationsData? data;

  InvitationsResponse({
    required this.status,
    this.message,
    this.data,
  });

  factory InvitationsResponse.fromJson(Map<String, dynamic> json) {
    return InvitationsResponse(
      status: json['status'],
      message: json['message'],
      data: json['data'] != null ? InvitationsData.fromJson(json['data']) : null,
    );
  }
}

class InvitationsData {
  final List<Invitation> created;
  final List<Invitation> accepted;
  final List<Invitation> available;

  InvitationsData({
    required this.created,
    required this.accepted,
    required this.available,
  });

  factory InvitationsData.fromJson(Map<String, dynamic> json) {
    return InvitationsData(
      created: (json['created'] as List)
          .map((item) => Invitation.fromJson(item))
          .toList(),
      accepted: (json['accepted'] as List)
          .map((item) => Invitation.fromJson(item))
          .toList(),
      available: (json['available'] as List)
          .map((item) => Invitation.fromJson(item))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'created': created.map((item) => item.toJson()).toList(),
      'accepted': accepted.map((item) => item.toJson()).toList(),
      'available': available.map((item) => item.toJson()).toList(),
    };
  }
  
  bool get isEmpty => created.isEmpty && accepted.isEmpty && available.isEmpty;
}

class InvitationDetails {
  final int id;
  final String title;
  final String? description;
  final DateTime? dateTime;
  final String? location;
  final int maxParticipants;
  final int currentAttendees;
  final InvitationTag tag;
  final InvitationCreator creator;
  final DateTime createdAt;
  final List<InvitationAttendee> attendees;

  InvitationDetails({
    required this.id,
    required this.title,
    this.description,
    this.dateTime,
    this.location,
    required this.maxParticipants,
    required this.currentAttendees,
    required this.tag,
    required this.creator,
    required this.createdAt,
    required this.attendees,
  });

  factory InvitationDetails.fromJson(Map<String, dynamic> json) {
    return InvitationDetails(
      id: json['id'],
      title: json['title'],
      description: json['description'],
      dateTime: json['dateTime'] != null ? DateTime.parse(json['dateTime']) : null,
      location: json['location'],
      maxParticipants: json['maxParticipants'],
      currentAttendees: json['currentAttendees'],
      tag: InvitationTag.fromJson(json['tag']),
      creator: InvitationCreator.fromJson(json['creator']),
      createdAt: DateTime.parse(json['createdAt']),
      attendees: (json['attendees'] as List)
          .map((item) => InvitationAttendee.fromJson(item))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'dateTime': dateTime?.toIso8601String(),
      'location': location,
      'maxParticipants': maxParticipants,
      'currentAttendees': currentAttendees,
      'tag': tag.toJson(),
      'creator': creator.toJson(),
      'createdAt': createdAt.toIso8601String(),
      'attendees': attendees.map((item) => item.toJson()).toList(),
    };
  }

  String get status {
    if (currentAttendees >= maxParticipants) {
      return 'Full';
    }
    return 'Available';
  }
}