import {
  JWTTokensPair,
  RhoV2Config
} from '../typings'
import axios, { Axios } from 'axios'

export interface VerifySignatureRequest {
  address: string
  signature: string
  chainId: number
  type: SubAccountType
}

export enum SubAccountType {
  blockchain = 'blockchain',
}

export class RhoV2AuthAPI {
  private client: Axios

  constructor(config: RhoV2Config) {
    this.client = axios.create({
      baseURL: `${config.authServiceUrl}`
    })
  }

  async getPublicKey() {
    const { data } = await this.client.get<string>('/auth/publicKey')
    return data
  }

  async requestSignIn(address: string) {
    const { data } = await this.client.post<{
      nonce: number
    }>('/auth/nonce', {
      address
    })
    return data.nonce
  }

  async verify(params: VerifySignatureRequest) {
    const { data } = await this.client.post<JWTTokensPair>('/auth/verify', params)
    return data
  }
}
