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

/// 通话记录 DTO（Data Transfer Object）
///
/// local / remote 共用的数据传输对象。
/// 提供与 Domain Entity [CallLog] 之间的双向转换。
class CallLogDto {
  final String id;
  final int? callerId;
  final int? receiverId;
  final int? chatId;
  final int? duration;
  final int? videoCall;
  final int? createdAt;
  final int? updatedAt;
  final int? endedAt;
  final int? status;
  final int? isDeleted;
  final int? deletedAt;
  final int? isRead;

  const CallLogDto({
    required this.id,
    this.callerId,
    this.receiverId,
    this.chatId,
    this.duration,
    this.videoCall,
    this.createdAt,
    this.updatedAt,
    this.endedAt,
    this.status,
    this.isDeleted,
    this.deletedAt,
    this.isRead,
  });

  factory CallLogDto.fromJson(Map<String, dynamic> json) => CallLogDto(
    id: json['id'] as String,
    callerId: json['caller_id'],
    receiverId: json['receiver_id'],
    chatId: json['chat_id'],
    duration: json['duration'],
    videoCall: json['video_call'],
    createdAt: json['created_at'],
    updatedAt: json['updated_at'],
    endedAt: json['ended_at'],
    status: json['status'],
    isDeleted: json['is_deleted'],
    deletedAt: json['deleted_at'],
    isRead: json['is_read'],
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'caller_id': callerId,
    'receiver_id': receiverId,
    'chat_id': chatId,
    'duration': duration,
    'video_call': videoCall,
    'created_at': createdAt,
    'updated_at': updatedAt,
    'ended_at': endedAt,
    'status': status,
    'is_deleted': isDeleted,
    'deleted_at': deletedAt,
    'is_read': isRead,
  };

  /// DTO → Domain Entity
  CallLog toEntity() => CallLog(
    id: id,
    callerId: callerId,
    receiverId: receiverId,
    chatId: chatId,
    duration: duration,
    videoCall: videoCall,
    createdAt: createdAt,
    updatedAt: updatedAt,
    endedAt: endedAt,
    status: status,
    isDeleted: isDeleted,
    deletedAt: deletedAt,
    isRead: isRead,
  );

  /// Domain Entity → DTO
  factory CallLogDto.fromEntity(CallLog callLog) => CallLogDto(
    id: callLog.id,
    callerId: callLog.callerId,
    receiverId: callLog.receiverId,
    chatId: callLog.chatId,
    duration: callLog.duration,
    videoCall: callLog.videoCall,
    createdAt: callLog.createdAt,
    updatedAt: callLog.updatedAt,
    endedAt: callLog.endedAt,
    status: callLog.status,
    isDeleted: callLog.isDeleted,
    deletedAt: callLog.deletedAt,
    isRead: callLog.isRead,
  );

  /// DTO → Drift Companion (for DB insert/update)
  CallLogsCompanion toCompanion() => CallLogsCompanion(
    id: Value(id),
    callerId: Value(callerId),
    receiverId: Value(receiverId),
    chatId: Value(chatId),
    duration: Value(duration),
    videoCall: Value(videoCall),
    createdAt: Value(createdAt),
    updatedAt: Value(updatedAt),
    endedAt: Value(endedAt),
    status: Value(status),
    isDeleted: Value(isDeleted),
    deletedAt: Value(deletedAt),
    isRead: Value(isRead),
  );
}