// SPDX-License-Identifier: Apache-2.0
import { PortUtilities } from '../../../../../business/utils/port-utilities.js';
import { PodReference } from '../../../resources/pod/pod-reference.js';
import { SoloError } from '../../../../../core/errors/solo-error.js';
import { sleep } from '../../../../../core/helpers.js';
import { Duration } from '../../../../../core/time/duration.js';
import { StatusCodes } from 'http-status-codes';
import { container } from 'tsyringe-neo';
import { PortForward, V1Pod, V1Container, V1ExecAction, V1ObjectMeta, V1Probe, V1PodSpec, } from '@kubernetes/client-node';
import * as constants from '../../../../../core/constants.js';
import net from 'node:net';
import { InjectTokens } from '../../../../../core/dependency-injection/inject-tokens.js';
import { NamespaceName } from '../../../../../types/namespace/namespace-name.js';
import { ContainerName } from '../../../resources/container/container-name.js';
import { PodName } from '../../../resources/pod/pod-name.js';
import { K8ClientPodCondition } from './k8-client-pod-condition.js';
import { ShellRunner } from '../../../../../core/shell-runner.js';
import chalk from 'chalk';
import http from 'node:http';
export class K8ClientPod {
    podReference;
    pods;
    kubeClient;
    kubeConfig;
    labels;
    startupProbeCommand;
    containerName;
    containerImage;
    containerCommand;
    conditions;
    podIp;
    deletionTimestamp;
    logger;
    constructor(podReference, pods, kubeClient, kubeConfig, labels, startupProbeCommand, containerName, containerImage, containerCommand, conditions, podIp, deletionTimestamp) {
        this.podReference = podReference;
        this.pods = pods;
        this.kubeClient = kubeClient;
        this.kubeConfig = kubeConfig;
        this.labels = labels;
        this.startupProbeCommand = startupProbeCommand;
        this.containerName = containerName;
        this.containerImage = containerImage;
        this.containerCommand = containerCommand;
        this.conditions = conditions;
        this.podIp = podIp;
        this.deletionTimestamp = deletionTimestamp;
        this.logger = container.resolve(InjectTokens.SoloLogger);
    }
    async killPod() {
        try {
            const result = await this.kubeClient.deleteNamespacedPod(this.podReference.name.toString(), this.podReference.namespace.toString(), undefined, undefined, 1);
            if (result.response.statusCode !== StatusCodes.OK) {
                throw new SoloError(`Failed to delete pod ${this.podReference.name} in namespace ${this.podReference.namespace}: statusCode: ${result.response.statusCode}`);
            }
            let podExists = true;
            while (podExists) {
                const pod = await this.pods.read(this.podReference);
                if (pod?.deletionTimestamp) {
                    await sleep(Duration.ofSeconds(1));
                }
                else {
                    podExists = false;
                }
            }
        }
        catch (error) {
            const errorMessage = `Failed to delete pod ${this.podReference.name.name} in namespace ${this.podReference.namespace}: ${error.message}`;
            if (error.body?.code === StatusCodes.NOT_FOUND || error.response?.body?.code === StatusCodes.NOT_FOUND) {
                this.logger.info(`Pod not found: ${errorMessage}`, error);
                return;
            }
            throw new SoloError(errorMessage, error);
        }
    }
    async portForward(localPort, podPort, detach = false, reuse = false) {
        let availablePort = localPort;
        try {
            // first use http.request(url[, options][, callback]) GET method against localhost:localPort to kill any pre-existing
            // port-forward that is no longer active.  It doesn't matter what the response is.
            const url = `http://${constants.LOCAL_HOST}:${localPort}`;
            await new Promise((resolve) => {
                http
                    .request(url, { method: 'GET' }, (response) => {
                    response.on('data', () => {
                        // do nothing
                    });
                    response.on('end', () => {
                        resolve();
                    });
                })
                    .on('close', () => {
                    resolve();
                })
                    .on('timeout', () => {
                    resolve();
                })
                    .on('information', () => {
                    resolve();
                })
                    .on('error', () => {
                    resolve();
                })
                    .setTimeout(Duration.ofMinutes(5).toMillis())
                    .end();
            });
            this.logger.debug(`Returned from http request against http://${constants.LOCAL_HOST}:${localPort}`);
            if (reuse) {
                // use `ps -ef | grep "kubectl port-forward"`|grep ${this.podReference.name}
                // to find previous port-forward port number
                const shellCommand = [
                    'ps',
                    '-ef',
                    '|',
                    'grep',
                    'port-forward',
                    '|',
                    'grep',
                    `${this.podReference.name}`,
                ];
                const shellRunner = new ShellRunner();
                let result;
                try {
                    result = await shellRunner.run(shellCommand.join(' '), [], true, false);
                }
                catch (error) {
                    this.logger.error(`Failed to execute shell command: ${shellCommand.join(' ')}`);
                    this.logger.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
                    throw new SoloError(`Shell command execution failed: ${shellCommand.join(' ')}. Error: ${error instanceof Error ? error.message : String(error)}`);
                }
                this.logger.debug(`ps -ef port-forward command result is ${result}`);
                // if length of result is 1 then could not find previous port forward running, then we can use next available port
                if (!result || result.length === 0) {
                    this.logger.warn(`Shell command returned no output: ${shellCommand.join(' ')}`);
                }
                if (result.length > 1) {
                    // extract local port number from command output
                    const splitArray = result[0].split(/\s+/).filter(Boolean);
                    // The port number should be the last element in the command
                    // It might be in the format localPort:podPort
                    const lastElement = splitArray.at(-1);
                    if (lastElement === undefined) {
                        throw new SoloError(`Failed to extract port: lastElement is undefined in command output: ${result[0]}`);
                    }
                    const extractedString = lastElement.split(':')[0];
                    this.logger.debug(`extractedString = ${extractedString}`);
                    const parsedPort = Number.parseInt(extractedString, 10);
                    if (Number.isNaN(parsedPort) || parsedPort <= 0 || parsedPort > 65_535) {
                        throw new SoloError(`Invalid port extracted: ${extractedString}.`);
                    }
                    else {
                        availablePort = parsedPort;
                        this.logger.info(`Reuse already enabled port ${availablePort}`);
                    }
                    // port forward already enabled
                    return availablePort;
                }
            }
            // Find an available port starting from localPort with a 30-second timeout
            availablePort = await PortUtilities.findAvailablePort(localPort, Duration.ofSeconds(30).toMillis(), this.logger);
            if (availablePort === localPort) {
                this.logger.showUser(chalk.yellow(`Using requested port ${localPort}`));
            }
            else {
                this.logger.showUser(chalk.yellow(`Using available port ${availablePort}`));
            }
            this.logger.debug(`Creating port-forwarder for ${this.podReference.name}:${podPort} -> ${constants.LOCAL_HOST}:${availablePort}`);
            // if detach is true, start a port-forwarder in detached mode
            if (detach) {
                this.logger.warn('Port-forwarding in detached mode has to be manually stopped or will stop when the Kubernetes pod it ', 'is connected to terminates.');
                await new ShellRunner().run(`kubectl port-forward -n ${this.podReference.namespace.name} pods/${this.podReference.name} ${availablePort}:${podPort}`, [], false, true);
                return availablePort;
            }
            const ns = this.podReference.namespace;
            const forwarder = new PortForward(this.kubeConfig, false);
            this.logger.debug(`Creating socket for port-forward for ${this.podReference.name}:${podPort} -> ${constants.LOCAL_HOST}:${localPort}`);
            const server = (await net.createServer((socket) => {
                forwarder.portForward(ns.name, this.podReference.name.toString(), [podPort], socket, undefined, socket, 3);
            }));
            // add info for logging
            server.info = `${this.podReference.name}:${podPort} -> ${constants.LOCAL_HOST}:${availablePort}`;
            server.localPort = availablePort;
            this.logger.debug(`Starting port-forwarder [${server.info}]`);
            return server.listen(availablePort, constants.LOCAL_HOST);
        }
        catch (error) {
            const message = `failed to start port-forwarder [${this.podReference.name}:${podPort} -> ${constants.LOCAL_HOST}:${availablePort}]: ${error.message}`;
            throw new SoloError(message, error);
        }
    }
    async stopPortForward(server, maxAttempts = 20, timeout = 500) {
        if (!server) {
            return;
        }
        this.logger.debug(`Stopping port-forwarder [${server.info}]`);
        // try to close the websocket server
        await new Promise((resolve, reject) => {
            server.close((error) => {
                if (error) {
                    if (error.message?.includes('Server is not running')) {
                        this.logger.debug(`Server not running, port-forwarder [${server.info}]`);
                        resolve();
                    }
                    else {
                        this.logger.debug(`Failed to stop port-forwarder [${server.info}]: ${error.message}`, error);
                        reject(error);
                    }
                }
                else {
                    this.logger.debug(`Stopped port-forwarder [${server.info}]`);
                    resolve();
                }
            });
        });
        // test to see if the port has been closed or if it is still open
        let attempts = 0;
        while (attempts < maxAttempts) {
            let hasError = 0;
            attempts++;
            try {
                const isPortOpen = await new Promise((resolve) => {
                    const testServer = net
                        .createServer()
                        .once('error', (error) => {
                        if (error) {
                            resolve(false);
                        }
                    })
                        .once('listening', () => {
                        testServer
                            .once('close', () => {
                            hasError++;
                            if (hasError > 1) {
                                resolve(false);
                            }
                            else {
                                resolve(true);
                            }
                        })
                            .close();
                    })
                        .listen(server.localPort, '0.0.0.0');
                });
                if (isPortOpen) {
                    return;
                }
            }
            catch {
                return;
            }
            await sleep(Duration.ofMillis(timeout));
        }
        if (attempts >= maxAttempts) {
            throw new SoloError(`failed to stop port-forwarder [${server.info}]`);
        }
    }
    static toV1Pod(pod) {
        const v1Metadata = new V1ObjectMeta();
        v1Metadata.name = pod.podReference.name.toString();
        v1Metadata.namespace = pod.podReference.namespace.toString();
        v1Metadata.labels = pod.labels;
        const v1ExecAction = new V1ExecAction();
        v1ExecAction.command = pod.startupProbeCommand;
        const v1Probe = new V1Probe();
        v1Probe.exec = v1ExecAction;
        const v1Container = new V1Container();
        v1Container.name = pod.containerName.name;
        v1Container.image = pod.containerImage;
        v1Container.command = pod.containerCommand;
        v1Container.startupProbe = v1Probe;
        const v1Spec = new V1PodSpec();
        v1Spec.containers = [v1Container];
        const v1Pod = new V1Pod();
        v1Pod.metadata = v1Metadata;
        v1Pod.spec = v1Spec;
        return v1Pod;
    }
    static fromV1Pod(v1Pod, pods, coreV1Api, kubeConfig) {
        if (!v1Pod) {
            return undefined;
        }
        return new K8ClientPod(PodReference.of(NamespaceName.of(v1Pod.metadata?.namespace), PodName.of(v1Pod.metadata?.name)), pods, coreV1Api, kubeConfig, v1Pod.metadata.labels, v1Pod.spec.containers[0]?.startupProbe?.exec?.command, ContainerName.of(v1Pod.spec.containers[0]?.name), v1Pod.spec.containers[0]?.image, v1Pod.spec.containers[0]?.command, v1Pod.status?.conditions?.map((condition) => new K8ClientPodCondition(condition.type, condition.status)), v1Pod.status?.podIP, v1Pod.metadata.deletionTimestamp ? new Date(v1Pod.metadata.deletionTimestamp) : undefined);
    }
}
//# sourceMappingURL=k8-client-pod.js.map