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'],
    );
  }
}

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

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

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

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

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

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

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']),
    );
  }
}

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(),
    );
  }
  
  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(),
    );
  }

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