let url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_REST_API_BASE_URL}`;

interface SimulationData {
    key: string;
    data?: object | any;
}
interface SimulationUsageRecord {
    activeTime: number;
    isActive: boolean;
    idleTime: number;
    type: "roboticArm" | "vehicle" | "transfer" | "storageUnit" | "crane" | "human" | "machine";
    assetId: string;
}

// Product → holds multiple usage records
interface ProductSimulation {
    productId: string;
    simulateData: SimulationUsageRecord[];
}

// Version → holds multiple products
interface VersionSimulation {
    versionId: string;
    products: ProductSimulation[];
}

// Project → holds multiple versions
interface ProjectSimulation {
    projectId: string | undefined;
    versions: VersionSimulation[];
}

export const saveSimulationData = async (data: any) => {
    try {
        const response = await fetch(`${url_Backend_dwinzo}/api/V1/SimulatedUpsert`, {
            method: "POST",
            headers: {
                Authorization: "Bearer <access_token>",
                "Content-Type": "application/json",
                token: localStorage.getItem("token") || "",
                refresh_token: localStorage.getItem("refreshToken") || "",
            },
            body: JSON.stringify(data),
        });
        const newAccessToken = response.headers.get("x-access-token");
        if (newAccessToken) {
            localStorage.setItem("token", newAccessToken);
        }
        if (!response.ok) {
            console.error("Failed to add project");
        }

        const result = await response.json();

        return result;
    } catch (error) {
        if (error instanceof Error) {
            console.log(error.message);
        } else {
            console.log("An unknown error occurred");
        }
    }
};

export const updateSimulateData = async (data: any) => {
    // console.log("data: update", data);
    try {
        const response = await fetch(`${url_Backend_dwinzo}/api/V1/ValidateSimulated`, {
            method: "POST",
            headers: {
                Authorization: "Bearer <access_token>",
                "Content-Type": "application/json",
                token: localStorage.getItem("token") || "",
                refresh_token: localStorage.getItem("refreshToken") || "",
            },
            body: JSON.stringify(data),
        });
        const newAccessToken = response.headers.get("x-access-token");
        if (newAccessToken) {
            localStorage.setItem("token", newAccessToken);
        }
        if (!response.ok) {
            console.error("Failed to update ");
        }

        const result = await response.json();

        return result;
    } catch (error) {
        if (error instanceof Error) {
            console.log(error.message);
        } else {
            console.log("An unknown error occurred");
        }
    }
};
export const getSimulationData = async (
    projectId: string,
    versionId: string,
    productUuid: string
) => {
    // console.log("called");
    try {
        const response = await fetch(
            `${url_Backend_dwinzo}/api/V1/SimulatedDatas/${projectId}/${versionId}?productUuid=${productUuid}`,
            {
                method: "GET",
                headers: {
                    Authorization: "Bearer <access_token>",
                    "Content-Type": "application/json",
                    token: localStorage.getItem("token") || "",
                    refresh_token: localStorage.getItem("refreshToken") || "",
                },
            }
        );

        console.log("response: ", response);

        const newAccessToken = response.headers.get("x-access-token");
        if (newAccessToken) {
            localStorage.setItem("token", newAccessToken);
        }

        if (!response.ok) {
            throw new Error("Failed to fetch simulateData");
        }

        return await response.json();
    } catch (error: any) {
        console.error("Failed to get simulation data");
        console.log(error.message);
    }
};

// export const clearSimulationData = ({ key, data }: SimulationData) => {};
