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

/// 聊天机器人 DTO
class ChatBotDto {
  final int id;
  final String? name;
  final String? username;
  final int? botUserId;
  final String? icon;
  final String? iconGaussian;
  final String? description;
  final String? token;
  final int? flag;
  final int? status;
  final String? webhook;
  final String? commands;
  final String? banner;
  final int? channelId;
  final int? channelGroupId;
  final int? deletedAt;
  final String? internalWebhook;
  final int? mode;
  final String? redirectUrl;
  final int? isInvitable;
  final int? isAllowForward;
  final String? tips;

  const ChatBotDto({
    required this.id,
    this.name,
    this.username,
    this.botUserId,
    this.icon,
    this.iconGaussian,
    this.description,
    this.token,
    this.flag,
    this.status,
    this.webhook,
    this.commands,
    this.banner,
    this.channelId,
    this.channelGroupId,
    this.deletedAt,
    this.internalWebhook,
    this.mode,
    this.redirectUrl,
    this.isInvitable,
    this.isAllowForward,
    this.tips,
  });

  factory ChatBotDto.fromJson(Map<String, dynamic> json) => ChatBotDto(
    id: json['id'] as int,
    name: json['name'],
    username: json['username'],
    botUserId: json['bot_user_id'],
    icon: json['icon'],
    iconGaussian: json['icon_gaussian'],
    description: json['description'],
    token: json['token'],
    flag: json['flag'],
    status: json['status'],
    webhook: json['webhook'],
    commands: json['commands'],
    banner: json['banner'],
    channelId: json['channel_id'],
    channelGroupId: json['channel_group_id'],
    deletedAt: json['deleted_at'],
    internalWebhook: json['internal_webhook'],
    mode: json['mode'],
    redirectUrl: json['redirect_url'],
    isInvitable: json['is_invitable'],
    isAllowForward: json['is_allow_forward'],
    tips: json['tips'],
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'name': name,
    'username': username,
    'bot_user_id': botUserId,
    'icon': icon,
    'icon_gaussian': iconGaussian,
    'description': description,
    'token': token,
    'flag': flag,
    'status': status,
    'webhook': webhook,
    'commands': commands,
    'banner': banner,
    'channel_id': channelId,
    'channel_group_id': channelGroupId,
    'deleted_at': deletedAt,
    'internal_webhook': internalWebhook,
    'mode': mode,
    'redirect_url': redirectUrl,
    'is_invitable': isInvitable,
    'is_allow_forward': isAllowForward,
    'tips': tips,
  };

  ChatBot toEntity() => ChatBot(
    id: id,
    name: name,
    username: username,
    botUserId: botUserId,
    icon: icon,
    iconGaussian: iconGaussian,
    description: description,
    token: token,
    flag: flag,
    status: status,
    webhook: webhook,
    commands: commands,
    banner: banner,
    channelId: channelId,
    channelGroupId: channelGroupId,
    deletedAt: deletedAt,
    internalWebhook: internalWebhook,
    mode: mode,
    redirectUrl: redirectUrl,
    isInvitable: isInvitable,
    isAllowForward: isAllowForward,
    tips: tips,
  );

  factory ChatBotDto.fromEntity(ChatBot chatBot) => ChatBotDto(
    id: chatBot.id,
    name: chatBot.name,
    username: chatBot.username,
    botUserId: chatBot.botUserId,
    icon: chatBot.icon,
    iconGaussian: chatBot.iconGaussian,
    description: chatBot.description,
    token: chatBot.token,
    flag: chatBot.flag,
    status: chatBot.status,
    webhook: chatBot.webhook,
    commands: chatBot.commands,
    banner: chatBot.banner,
    channelId: chatBot.channelId,
    channelGroupId: chatBot.channelGroupId,
    deletedAt: chatBot.deletedAt,
    internalWebhook: chatBot.internalWebhook,
    mode: chatBot.mode,
    redirectUrl: chatBot.redirectUrl,
    isInvitable: chatBot.isInvitable,
    isAllowForward: chatBot.isAllowForward,
    tips: chatBot.tips,
  );

  ChatBotsCompanion toCompanion() => ChatBotsCompanion(
    id: Value(id),
    name: Value(name),
    username: Value(username),
    botUserId: Value(botUserId),
    icon: Value(icon),
    iconGaussian: Value(iconGaussian),
    description: Value(description),
    token: Value(token),
    flag: Value(flag),
    status: Value(status),
    webhook: Value(webhook ?? ''),
    commands: Value(commands ?? '[]'),
    banner: Value(banner),
    channelId: Value(channelId),
    channelGroupId: Value(channelGroupId),
    deletedAt: Value(deletedAt),
    internalWebhook: Value(internalWebhook),
    mode: Value(mode),
    redirectUrl: Value(redirectUrl),
    isInvitable: Value(isInvitable),
    isAllowForward: Value(isAllowForward),
    tips: Value(tips),
  );
}