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

/// 消息 DTO
class MessageDto {
  final int id;
  final int? messageId;
  final int? chatId;
  final int? chatIdx;
  final int? sendId;
  final String? content;
  final int? typ;
  final int? sendTime;
  final int? expireTime;
  final int? createTime;
  final String? atUsers;
  final String emojis;
  final int editTime;
  final int refTyp;
  final int flag;
  final String cmid;

  const MessageDto({
    required this.id,
    this.messageId,
    this.chatId,
    this.chatIdx,
    this.sendId,
    this.content,
    this.typ,
    this.sendTime,
    this.expireTime,
    this.createTime,
    this.atUsers,
    this.emojis = '[]',
    this.editTime = 0,
    this.refTyp = 0,
    this.flag = 0,
    this.cmid = '',
  });

  factory MessageDto.fromJson(Map<String, dynamic> json) => MessageDto(
    id: json['id'] as int,
    messageId: json['message_id'],
    chatId: json['chat_id'],
    chatIdx: json['chat_idx'],
    sendId: json['send_id'],
    content: json['content'],
    typ: json['typ'],
    sendTime: json['send_time'],
    expireTime: json['expire_time'],
    createTime: json['create_time'],
    atUsers: json['at_users'],
    emojis: json['emojis'] ?? '[]',
    editTime: json['edit_time'] ?? 0,
    refTyp: json['ref_typ'] ?? 0,
    flag: json['flag'] ?? 0,
    cmid: json['cmid'] ?? '',
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'message_id': messageId,
    'chat_id': chatId,
    'chat_idx': chatIdx,
    'send_id': sendId,
    'content': content,
    'typ': typ,
    'send_time': sendTime,
    'expire_time': expireTime,
    'create_time': createTime,
    'at_users': atUsers,
    'emojis': emojis,
    'edit_time': editTime,
    'ref_typ': refTyp,
    'flag': flag,
    'cmid': cmid,
  };

  Message toEntity() => Message(
    id: id,
    messageId: messageId,
    chatId: chatId,
    chatIdx: chatIdx,
    sendId: sendId,
    content: content,
    typ: typ,
    sendTime: sendTime,
    expireTime: expireTime,
    createTime: createTime,
    atUsers: atUsers,
    emojis: emojis,
    editTime: editTime,
    refTyp: refTyp,
    flag: flag,
    cmid: cmid,
  );

  factory MessageDto.fromEntity(Message message) => MessageDto(
    id: message.id,
    messageId: message.messageId,
    chatId: message.chatId,
    chatIdx: message.chatIdx,
    sendId: message.sendId,
    content: message.content,
    typ: message.typ,
    sendTime: message.sendTime,
    expireTime: message.expireTime,
    createTime: message.createTime,
    atUsers: message.atUsers,
    emojis: message.emojis,
    editTime: message.editTime,
    refTyp: message.refTyp,
    flag: message.flag,
    cmid: message.cmid,
  );

  MessagesCompanion toCompanion() => MessagesCompanion(
    id: Value(id),
    messageId: Value(messageId),
    chatId: Value(chatId),
    chatIdx: Value(chatIdx),
    sendId: Value(sendId),
    content: Value(content),
    typ: Value(typ),
    sendTime: Value(sendTime),
    expireTime: Value(expireTime),
    createTime: Value(createTime),
    atUsers: Value(atUsers),
    emojis: Value(emojis),
    editTime: Value(editTime),
    refTyp: Value(refTyp),
    flag: Value(flag),
    cmid: Value(cmid),
  );
}