"use client";

import {
  ChatHistoryResponse,
  ChatSendResponse,
  SendMessageRequest,
  UnlockHistoryResponse,
  UnlockPrivateRequest,
  UnlockPrivateResponse,
} from "@/data/dto/chat";
import type { ChatApi } from "@/data/services/api";
import { Result } from "@/utils";

export class ChatRemoteDataSource {
  constructor(private readonly api: ChatApi) {}

  async sendMessage(
    message: string,
    options?: { image?: string; useWebSocket?: boolean },
  ): Promise<Result<ChatSendResponse>> {
    return Result.wrap(async () => {
      const request = SendMessageRequest.from({
        message,
        image: options?.image ?? "",
        useWebSocket: options?.useWebSocket ?? false,
      });
      return await this.api.sendMessage(request);
    });
  }

  async getHistory(
    limit = 50,
    offset = 0,
  ): Promise<Result<ChatHistoryResponse>> {
    return Result.wrap(() => this.api.getHistory(limit, offset));
  }

  async unlockPrivateMessage(
    messageId: string,
  ): Promise<Result<UnlockPrivateResponse>> {
    return Result.wrap(() =>
      this.api.unlockPrivateMessage(UnlockPrivateRequest.from({ messageId })),
    );
  }

  async unlockHistory(): Promise<Result<UnlockHistoryResponse>> {
    return Result.wrap(() => this.api.unlockHistory());
  }
}
