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

/// 标签 DTO
class TagDto {
  final int? id;
  final int? uid;
  final String name;
  final int? type;
  final int? createdAt;
  final int? updatedAt;
  final int? addIndex;

  const TagDto({
    this.id,
    this.uid,
    this.name = '',
    this.type,
    this.createdAt,
    this.updatedAt,
    this.addIndex,
  });

  factory TagDto.fromJson(Map<String, dynamic> json) => TagDto(
    id: json['id'],
    uid: json['uid'],
    name: json['name'] ?? '',
    type: json['type'],
    createdAt: json['created_at'],
    updatedAt: json['updated_at'],
    addIndex: json['__add_index'],
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'uid': uid,
    'name': name,
    'type': type,
    'created_at': createdAt,
    'updated_at': updatedAt,
    '__add_index': addIndex,
  };

  Tag toEntity() => Tag(
    id: id,
    uid: uid,
    name: name,
    type: type,
    createdAt: createdAt,
    updatedAt: updatedAt,
    addIndex: addIndex,
  );

  factory TagDto.fromEntity(Tag tag) => TagDto(
    id: tag.id,
    uid: tag.uid,
    name: tag.name,
    type: tag.type,
    createdAt: tag.createdAt,
    updatedAt: tag.updatedAt,
    addIndex: tag.addIndex,
  );

  TagsCompanion toCompanion() => TagsCompanion(
    id: id != null ? Value(id!) : const Value.absent(),
    uid: Value(uid),
    name: Value(name),
    type: Value(type),
    createdAt: Value(createdAt),
    updatedAt: Value(updatedAt),
    addIndex: Value(addIndex),
  );
}
