import { api } from "@/lib/api-client"
import type { GetVulnerabilitiesParams, Vulnerability } from "@/types/vulnerability.types"
import { USE_MOCK, mockDelay, getMockVulnerabilities } from '@/mock'

export class VulnerabilityService {
  /** Get all vulnerabilities list (used by global vulnerabilities page) */
  static async getAllVulnerabilities(
    params: GetVulnerabilitiesParams,
    filter?: string,
  ): Promise<any> {
    if (USE_MOCK) {
      await mockDelay()
      return getMockVulnerabilities(params)
    }
    const response = await api.get(`/assets/vulnerabilities/`, {
      params: { ...params, filter },
    })
    return response.data
  }

  /** Get single vulnerability by ID */
  static async getVulnerabilityById(id: number): Promise<Vulnerability> {
    const response = await api.get<Vulnerability>(`/assets/vulnerabilities/${id}/`)
    return response.data
  }

  /** Get vulnerability snapshot list by scan task (used by scan history page) */
  static async getVulnerabilitiesByScanId(
    scanId: number,
    params: GetVulnerabilitiesParams,
    filter?: string,
  ): Promise<any> {
    const response = await api.get(`/scans/${scanId}/vulnerabilities/`, {
      params: { ...params, filter },
    })
    return response.data
  }

  /** Get vulnerability asset list by target (used by target details page) */
  static async getVulnerabilitiesByTargetId(
    targetId: number,
    params: GetVulnerabilitiesParams,
    filter?: string,
  ): Promise<any> {
    const response = await api.get(`/targets/${targetId}/vulnerabilities/`, {
      params: { ...params, filter },
    })
    return response.data
  }
}
