import {
  TagCategoryEntity,
  UserTagEntity,
  TagType,
  TagValueType,
  UserTagLogEntity,
  TagAction,
} from '../../../domain/entities/user-tag.entity';

/**
 * 标签分类响应
 */
export class TagCategoryResponseDto {
  id: string;
  code: string;
  name: string;
  description: string | null;
  sortOrder: number;
  isEnabled: boolean;
  createdAt: string;
  updatedAt: string;

  static fromEntity(entity: TagCategoryEntity): TagCategoryResponseDto {
    return {
      id: entity.id,
      code: entity.code,
      name: entity.name,
      description: entity.description,
      sortOrder: entity.sortOrder,
      isEnabled: entity.isEnabled,
      createdAt: entity.createdAt.toISOString(),
      updatedAt: entity.updatedAt.toISOString(),
    };
  }
}

/**
 * 标签响应
 */
export class TagResponseDto {
  id: string;
  categoryId: string | null;
  code: string;
  name: string;
  description: string | null;
  color: string | null;
  type: TagType;
  valueType: TagValueType;
  enumValues: string[] | null;
  ruleId: string | null;
  isAdvertisable: boolean;
  estimatedUsers: number | null;
  isEnabled: boolean;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;

  static fromEntity(entity: UserTagEntity): TagResponseDto {
    return {
      id: entity.id,
      categoryId: entity.categoryId,
      code: entity.code,
      name: entity.name,
      description: entity.description,
      color: entity.color,
      type: entity.type,
      valueType: entity.valueType,
      enumValues: entity.enumValues,
      ruleId: entity.ruleId,
      isAdvertisable: entity.isAdvertisable,
      estimatedUsers: entity.estimatedUsers,
      isEnabled: entity.isEnabled,
      sortOrder: entity.sortOrder,
      createdAt: entity.createdAt.toISOString(),
      updatedAt: entity.updatedAt.toISOString(),
    };
  }
}

/**
 * 标签列表响应
 */
export class TagListResponseDto {
  items: TagResponseDto[];
  total: number;
}

/**
 * 用户标签响应
 */
export class UserTagResponseDto {
  tagId: string;
  tagCode: string;
  tagName: string;
  value: string | null;
  isAuto: boolean;
  assignedAt: string;
}

/**
 * 用户标签列表响应
 */
export class UserTagListResponseDto {
  accountSequence: string;
  tags: UserTagResponseDto[];
}

/**
 * 标签用户响应
 */
export class TagUserResponseDto {
  accountSequence: string;
  value: string | null;
  assignedAt: string;
  assignedBy: string | null;
  source: string | null;
}

/**
 * 标签用户列表响应
 */
export class TagUserListResponseDto {
  tagId: string;
  users: TagUserResponseDto[];
  total: number;
}

/**
 * 标签变更日志响应
 */
export class TagLogResponseDto {
  id: string;
  accountSequence: string;
  tagCode: string;
  action: TagAction;
  oldValue: string | null;
  newValue: string | null;
  reason: string | null;
  operatorId: string | null;
  createdAt: string;

  static fromEntity(entity: UserTagLogEntity): TagLogResponseDto {
    return {
      id: entity.id,
      accountSequence: entity.accountSequence,
      tagCode: entity.tagCode,
      action: entity.action,
      oldValue: entity.oldValue,
      newValue: entity.newValue,
      reason: entity.reason,
      operatorId: entity.operatorId,
      createdAt: entity.createdAt.toISOString(),
    };
  }
}

/**
 * 标签同步结果响应
 */
export class TagSyncResultResponseDto {
  accountSequence: string;
  added: string[];
  removed: string[];
  unchanged: string[];
}

/**
 * 批量同步结果响应
 */
export class BatchSyncResultResponseDto {
  total: number;
  processed: number;
  results: TagSyncResultResponseDto[];
  errors: Array<{ accountSequence: string; error: string }>;
}

/**
 * 批量操作结果响应
 */
export class BatchOperationResultDto {
  success: number;
  failed: number;
}
