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

/// 重试 DTO
class RetryDto {
  final int? id;
  final int? uid;
  final String apiType;
  final String endPoint;
  final String requestData;
  final int? synced;
  final String callbackFun;
  final int? expired;
  final int? replace;
  final int? expireTime;
  final int? createTime;
  final int? addIndex;

  const RetryDto({
    this.id,
    this.uid,
    this.apiType = '',
    this.endPoint = '',
    this.requestData = '',
    this.synced,
    this.callbackFun = '',
    this.expired,
    this.replace,
    this.expireTime,
    this.createTime,
    this.addIndex,
  });

  factory RetryDto.fromJson(Map<String, dynamic> json) => RetryDto(
    id: json['id'],
    uid: json['uid'],
    apiType: json['api_type'] ?? '',
    endPoint: json['end_point'] ?? '',
    requestData: json['request_data'] ?? '',
    synced: json['synced'],
    callbackFun: json['callback_fun'] ?? '',
    expired: json['expired'],
    replace: json['replace'],
    expireTime: json['expire_time'],
    createTime: json['create_time'],
    addIndex: json['__add_index'],
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'uid': uid,
    'api_type': apiType,
    'end_point': endPoint,
    'request_data': requestData,
    'synced': synced,
    'callback_fun': callbackFun,
    'expired': expired,
    'replace': replace,
    'expire_time': expireTime,
    'create_time': createTime,
    '__add_index': addIndex,
  };

  Retry toEntity() => Retry(
    id: id,
    uid: uid,
    apiType: apiType,
    endPoint: endPoint,
    requestData: requestData,
    synced: synced,
    callbackFun: callbackFun,
    expired: expired,
    replace: replace,
    expireTime: expireTime,
    createTime: createTime,
    addIndex: addIndex,
  );

  factory RetryDto.fromEntity(Retry retry) => RetryDto(
    id: retry.id,
    uid: retry.uid,
    apiType: retry.apiType,
    endPoint: retry.endPoint,
    requestData: retry.requestData,
    synced: retry.synced,
    callbackFun: retry.callbackFun,
    expired: retry.expired,
    replace: retry.replace,
    expireTime: retry.expireTime,
    createTime: retry.createTime,
    addIndex: retry.addIndex,
  );

  RetriesCompanion toCompanion() => RetriesCompanion(
    id: id != null ? Value(id!) : const Value.absent(),
    uid: Value(uid),
    apiType: Value(apiType),
    endPoint: Value(endPoint),
    requestData: Value(requestData),
    synced: Value(synced),
    callbackFun: Value(callbackFun),
    expired: Value(expired),
    replace: Value(replace),
    expireTime: Value(expireTime),
    createTime: Value(createTime),
    addIndex: Value(addIndex),
  );
}
