import 'package:drift/drift.dart';
import 'package:im_app/data/local/drift/app_database.dart';
import 'package:im_app/domain/entities/group.dart';

/// 群组 DTO
class GroupDto {
  final int id;
  final int? userJoinDate;
  final String? name;
  final String? profile;
  final String? icon;
  final String iconGaussian;
  final int? permission;
  final int? admin;
  final String? members;
  final int? owner;
  final String? admins;
  final int? visible;
  final int? speakInterval;
  final int? groupType;
  final int? roomType;
  final int? maxNumber;
  final int? channelId;
  final int? channelGroupId;
  final int? createTime;
  final int? updateTime;
  final int? addIndex;
  final int? maxMember;
  final int? expireTime;
  final int workspaceId;
  final int mode;
  final int redpacketPlay;
  final String? topic;
  final String? rp;

  const GroupDto({
    required this.id,
    this.userJoinDate,
    this.name,
    this.profile,
    this.icon,
    this.iconGaussian = '',
    this.permission,
    this.admin,
    this.members,
    this.owner,
    this.admins,
    this.visible,
    this.speakInterval,
    this.groupType,
    this.roomType,
    this.maxNumber,
    this.channelId,
    this.channelGroupId,
    this.createTime,
    this.updateTime,
    this.addIndex,
    this.maxMember,
    this.expireTime,
    this.workspaceId = 0,
    this.mode = 0,
    this.redpacketPlay = 0,
    this.topic,
    this.rp,
  });

  factory GroupDto.fromJson(Map<String, dynamic> json) => GroupDto(
    id: json['id'] as int,
    userJoinDate: json['user_join_date'],
    name: json['name'],
    profile: json['profile'],
    icon: json['icon'],
    iconGaussian: json['icon_gaussian'] ?? '',
    permission: json['permission'],
    admin: json['admin'],
    members: json['members'],
    owner: json['owner'],
    admins: json['admins'],
    visible: json['visible'],
    speakInterval: json['speak_interval'],
    groupType: json['group_type'],
    roomType: json['room_type'],
    maxNumber: json['max_number'],
    channelId: json['channel_id'],
    channelGroupId: json['channel_group_id'],
    createTime: json['create_time'],
    updateTime: json['update_time'],
    addIndex: json['__add_index'],
    maxMember: json['max_member'],
    expireTime: json['expire_time'],
    workspaceId: json['workspace_id'] ?? 0,
    mode: json['mode'] ?? 0,
    redpacketPlay: json['redpacket_play'] ?? 0,
    topic: json['topic'],
    rp: json['rp'],
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'user_join_date': userJoinDate,
    'name': name,
    'profile': profile,
    'icon': icon,
    'icon_gaussian': iconGaussian,
    'permission': permission,
    'admin': admin,
    'members': members,
    'owner': owner,
    'admins': admins,
    'visible': visible,
    'speak_interval': speakInterval,
    'group_type': groupType,
    'room_type': roomType,
    'max_number': maxNumber,
    'channel_id': channelId,
    'channel_group_id': channelGroupId,
    'create_time': createTime,
    'update_time': updateTime,
    '__add_index': addIndex,
    'max_member': maxMember,
    'expire_time': expireTime,
    'workspace_id': workspaceId,
    'mode': mode,
    'redpacket_play': redpacketPlay,
    'topic': topic,
    'rp': rp,
  };

  Group toEntity() => Group(
    id: id,
    userJoinDate: userJoinDate,
    name: name,
    profile: profile,
    icon: icon,
    iconGaussian: iconGaussian,
    permission: permission,
    admin: admin,
    members: members,
    owner: owner,
    admins: admins,
    visible: visible,
    speakInterval: speakInterval,
    groupType: groupType,
    roomType: roomType,
    maxNumber: maxNumber,
    channelId: channelId,
    channelGroupId: channelGroupId,
    createTime: createTime,
    updateTime: updateTime,
    addIndex: addIndex,
    maxMember: maxMember,
    expireTime: expireTime,
    workspaceId: workspaceId,
    mode: mode,
    redpacketPlay: redpacketPlay,
    topic: topic,
    rp: rp,
  );

  factory GroupDto.fromEntity(Group group) => GroupDto(
    id: group.id,
    userJoinDate: group.userJoinDate,
    name: group.name,
    profile: group.profile,
    icon: group.icon,
    iconGaussian: group.iconGaussian,
    permission: group.permission,
    admin: group.admin,
    members: group.members,
    owner: group.owner,
    admins: group.admins,
    visible: group.visible,
    speakInterval: group.speakInterval,
    groupType: group.groupType,
    roomType: group.roomType,
    maxNumber: group.maxNumber,
    channelId: group.channelId,
    channelGroupId: group.channelGroupId,
    createTime: group.createTime,
    updateTime: group.updateTime,
    addIndex: group.addIndex,
    maxMember: group.maxMember,
    expireTime: group.expireTime,
    workspaceId: group.workspaceId,
    mode: group.mode,
    redpacketPlay: group.redpacketPlay,
    topic: group.topic,
    rp: group.rp,
  );

  GroupsCompanion toCompanion() => GroupsCompanion(
    id: Value(id),
    userJoinDate: Value(userJoinDate),
    name: Value(name),
    profile: Value(profile),
    icon: Value(icon),
    iconGaussian: Value(iconGaussian),
    permission: Value(permission),
    admin: Value(admin),
    members: Value(members),
    owner: Value(owner),
    admins: Value(admins),
    visible: Value(visible),
    speakInterval: Value(speakInterval),
    groupType: Value(groupType),
    roomType: Value(roomType),
    maxNumber: Value(maxNumber),
    channelId: Value(channelId),
    channelGroupId: Value(channelGroupId),
    createTime: Value(createTime),
    updateTime: Value(updateTime),
    addIndex: Value(addIndex),
    maxMember: Value(maxMember),
    expireTime: Value(expireTime),
    workspaceId: Value(workspaceId),
    mode: Value(mode),
    redpacketPlay: Value(redpacketPlay),
    topic: Value(topic),
    rp: Value(rp),
  );
}
