import 'package:uuid/uuid.dart';

class GroupMember {
  final String id;
  final String groupId;
  final String userId;
  final String role; // 'admin', 'moderator', 'member'
  final DateTime joinedAt;
  final bool isActive;

  GroupMember({
    String? id,
    required this.groupId,
    required this.userId,
    this.role = 'member',
    DateTime? joinedAt,
    this.isActive = true,
  }) : this.id = id ?? const Uuid().v4(),
       this.joinedAt = joinedAt ?? DateTime.now();

  // Create a copy of this member
  GroupMember copyWith({
    String? id,
    String? groupId,
    String? userId,
    String? role,
    DateTime? joinedAt,
    bool? isActive,
  }) {
    return GroupMember(
      id: id ?? this.id,
      groupId: groupId ?? this.groupId,
      userId: userId ?? this.userId,
      role: role ?? this.role,
      joinedAt: joinedAt ?? this.joinedAt,
      isActive: isActive ?? this.isActive,
    );
  }

  // Create from database Map
  factory GroupMember.fromMap(Map<String, dynamic> map) {
    final joinedAtStr = map['joined_at'] as String?;
    final DateTime joinedAt =
        joinedAtStr != null ? DateTime.parse(joinedAtStr) : DateTime.now();

    return GroupMember(
      id: map['id'] as String,
      groupId: map['group_id'] as String,
      userId: map['user_id'] as String,
      role: map['role'] as String? ?? 'member',
      joinedAt: joinedAt,
      isActive: map['is_active'] as bool? ?? true,
    );
  }

  // Convert to database Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'group_id': groupId,
      'user_id': userId,
      'role': role,
      'joined_at': joinedAt.toIso8601String(),
      'is_active': isActive,
    };
  }

  // Check if member has admin privileges
  bool get isAdmin => role == 'admin';

  // Check if member has moderation privileges
  bool get isModerator => role == 'admin' || role == 'moderator';
}
