import { apiCall } from "../core";
import { HttpMethod } from "../core/types";
import { getAndWaitForOperationStatusCompleted } from "../operation";
import type { OperationStatus } from "../operation/types";

import type { ListApplicationVulnerabilitiesResponse } from "./types";

/**
 * Retrieves a list of application vulnerabilities.
 * @param {number} appId - The numeric ID of the application.
 * @param {number} serverId - The numeric ID of the server.
 * @returns {Promise<ListApplicationVulnerabilitiesResponse>} A Promise that resolves to the response containing the list of application vulnerabilities.
 * @example
 * {
        "status": true,
        "data": {
            "last_fetched_date": "January 16, 2024",
            "wordpress": {
                "name": "WordPress Core",
                "slug": "wordpress",
                "current_version": "6.4.2",
                "type": "wordpress",
                "is_vulnerable": 1,
                "vulnerabilities": {
                    "id": 9662,
                    "product_id": 8,
                    "title": "WordPress Core All Versions - Unauthenticated Blind Server-Side Request Forgery vulnerability",
                    "disclosure_date": "2022-12-13 06:40:58",
                    "product_slug": "wordpress",
                    "product_name": "WordPress",
                    "product_name_premium": null,
                    "product_type": "WordPress",
                    "vuln_type": "Server Side Request Forgery (SSRF)",
                    "cvss_score": 4,
                    "affected_in": "<= 6.4.2",
                    "fixed_in": "",
                    "patched_in_ranges": [],
                    "direct_url": "https://patchstack.com/database/vulnerability/wordpress/wordpress-6-1-1-unauth-blind-ssrf-vulnerability?_a_id=13"
                },
                "recommendation": "Deactivate and remove the wordpress",
                "active": 1,
                "created_at": "January 16, 2024",
                "updated_at": "2024-01-16T11:10:23.000000Z"
            },
            "themes": [
                {
                    "name": "Twenty Twenty-Four",
                    "slug": "twentytwentyfour",
                    "current_version": "1.0",
                    "type": "theme",
                    "is_vulnerable": 0,
                    "vulnerabilities": [],
                    "recommendation": "",
                    "active": 1,
                    "created_at": "January 16, 2024",
                    "updated_at": "2024-01-16T11:10:23.000000Z"
                },
            ],
            "plugins": [
                {
                    "name": "Breeze",
                    "slug": "breeze",
                    "current_version": "2.1.3",
                    "type": "plugin",
                    "is_vulnerable": 0,
                    "vulnerabilities": [],
                    "recommendation": "",
                    "active": 1,
                    "created_at": "January 16, 2024",
                    "updated_at": "2024-01-16T11:10:23.000000Z"
                },
            ]
        }
}
 */
export function listApplicationVulnerabilities(
  appId: number,
  serverId: number
): Promise<ListApplicationVulnerabilitiesResponse> {
  const data = {
    app_id: appId,
    server_id: serverId,
  };
  return apiCall(`/app/vulnerabilities/${appId}`, HttpMethod.GET, data).then(
    (response) => response as ListApplicationVulnerabilitiesResponse
  );
}

/**
 * Initiates a refresh of application vulnerabilities.
 * @param {number} appId - The numeric ID of the application.
 * @param {number} serverId - The numeric ID of the server.
 * @returns {Promise<OperationStatus>} A Promise that resolves to an object containing the status and operation ID of the refresh operation.
 * @example
 * {
        "status": true,
        "operation_id": 525888
    }
 */
export async function refreshApplicationVulnerabilities(
  appId: number,
  serverId: number
): Promise<OperationStatus> {
  const data = {
    app_id: appId,
    server_id: serverId,
  };
  const req = await apiCall(`/app/vulnerabilities/${appId}/refresh`,HttpMethod.GET,data);
  return await getAndWaitForOperationStatusCompleted(req.operation_id);

}
