import type { RouteLocation } from 'vue-router'
import { ApiClient } from './base'
import { getSelectedProfile } from './profile'

export interface Attachment {
  id: number
  uploadedAt: string
  filename: string
  contentType: string
  size: number
}

export class AttachmentApiClient extends ApiClient {
  readonly profileName: string

  constructor(route: RouteLocation) {
    super()
    this.profileName = getSelectedProfile(route)
  }

  getAttachmentUrl(attachmentId: number, token: string) {
    return (
      import.meta.env.VITE_API_BASE_URL +
      `/profiles/${this.profileName}/attachments/${attachmentId}/download?t=${token}`
    )
  }

  downloadAttachment(attachmentId: number, token: string) {
    window.open(this.getAttachmentUrl(attachmentId, token), '_blank')
  }
}
