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

/// 收藏 DTO
class FavouriteDto {
  final int id;
  final String parentId;
  final String data;
  final int createdAt;
  final int updatedAt;
  final int deletedAt;
  final int? source;
  final int? userId;
  final int? authorId;
  final String typ;
  final String tag;
  final int isPin;
  final int chatTyp;
  final int isUploaded;
  final String urls;

  const FavouriteDto({
    required this.id,
    this.parentId = '',
    this.data = '',
    this.createdAt = 0,
    this.updatedAt = 0,
    this.deletedAt = 0,
    this.source,
    this.userId,
    this.authorId,
    this.typ = '[]',
    this.tag = '[]',
    this.isPin = 0,
    this.chatTyp = 0,
    this.isUploaded = 1,
    this.urls = '[]',
  });

  factory FavouriteDto.fromJson(Map<String, dynamic> json) => FavouriteDto(
    id: json['id'] as int,
    parentId: json['parent_id'] ?? '',
    data: json['data'] ?? '',
    createdAt: json['created_at'] ?? 0,
    updatedAt: json['updated_at'] ?? 0,
    deletedAt: json['deleted_at'] ?? 0,
    source: json['source'],
    userId: json['user_id'],
    authorId: json['author_id'],
    typ: json['typ'] ?? '[]',
    tag: json['tag'] ?? '[]',
    isPin: json['is_pin'] ?? 0,
    chatTyp: json['chat_typ'] ?? 0,
    isUploaded: json['is_uploaded'] ?? 1,
    urls: json['urls'] ?? '[]',
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'parent_id': parentId,
    'data': data,
    'created_at': createdAt,
    'updated_at': updatedAt,
    'deleted_at': deletedAt,
    'source': source,
    'user_id': userId,
    'author_id': authorId,
    'typ': typ,
    'tag': tag,
    'is_pin': isPin,
    'chat_typ': chatTyp,
    'is_uploaded': isUploaded,
    'urls': urls,
  };

  Favourite toEntity() => Favourite(
    id: id,
    parentId: parentId,
    data: data,
    createdAt: createdAt,
    updatedAt: updatedAt,
    deletedAt: deletedAt,
    source: source,
    userId: userId,
    authorId: authorId,
    typ: typ,
    tag: tag,
    isPin: isPin,
    chatTyp: chatTyp,
    isUploaded: isUploaded,
    urls: urls,
  );

  factory FavouriteDto.fromEntity(Favourite favourite) => FavouriteDto(
    id: favourite.id,
    parentId: favourite.parentId,
    data: favourite.data,
    createdAt: favourite.createdAt,
    updatedAt: favourite.updatedAt,
    deletedAt: favourite.deletedAt,
    source: favourite.source,
    userId: favourite.userId,
    authorId: favourite.authorId,
    typ: favourite.typ,
    tag: favourite.tag,
    isPin: favourite.isPin,
    chatTyp: favourite.chatTyp,
    isUploaded: favourite.isUploaded,
    urls: favourite.urls,
  );

  FavouritesCompanion toCompanion() => FavouritesCompanion(
    id: Value(id),
    parentId: Value(parentId),
    data: Value(data),
    createdAt: Value(createdAt),
    updatedAt: Value(updatedAt),
    deletedAt: Value(deletedAt),
    source: Value(source),
    userId: Value(userId),
    authorId: Value(authorId),
    typ: Value(typ),
    tag: Value(tag),
    isPin: Value(isPin),
    chatTyp: Value(chatTyp),
    isUploaded: Value(isUploaded),
    urls: Value(urls),
  );
}
