import type { FileAttachment } from "@/lib/types";
import { api } from "@/lib/api";
import { parseArtifactUri } from "./file";

export const downloadBlob = (blob: Blob, filename?: string) => {
    try {
        const url = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = url;
        a.download = filename || "download";
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    } catch (error) {
        console.error("Error downloading blob:", error);
    }
};

export const downloadFile = async (file: FileAttachment, sessionId?: string, projectId?: string) => {
    try {
        let blob: Blob;
        let filename = file.name;

        if (file.content) {
            const byteCharacters = atob(file.content);
            const byteNumbers = new Array(byteCharacters.length);
            for (let i = 0; i < byteCharacters.length; i++) {
                byteNumbers[i] = byteCharacters.charCodeAt(i);
            }
            const byteArray = new Uint8Array(byteNumbers);
            blob = new Blob([byteArray], { type: file.mime_type || "application/octet-stream" });
        } else if (file.uri) {
            const parsedUri = parseArtifactUri(file.uri);
            if (!parsedUri) {
                throw new Error(`Invalid or unhandled URI format: ${file.uri}`);
            }

            const { filename: parsedFilename, version: parsedVersion } = parsedUri;
            filename = parsedFilename;
            const version = parsedVersion || "latest";

            let endpoint: string;
            if (sessionId && sessionId.trim() && sessionId !== "null" && sessionId !== "undefined") {
                endpoint = `/api/v1/artifacts/${encodeURIComponent(sessionId)}/${encodeURIComponent(filename)}/versions/${version}`;
            } else if (projectId) {
                endpoint = `/api/v1/artifacts/null/${encodeURIComponent(filename)}/versions/${version}?project_id=${projectId}`;
            } else {
                endpoint = `/api/v1/artifacts/null/${encodeURIComponent(filename)}/versions/${version}`;
            }

            const response = await api.webui.get(endpoint, { fullResponse: true });
            if (!response.ok) {
                throw new Error(`Failed to download file: ${response.statusText}`);
            }
            blob = await response.blob();
        } else {
            throw new Error("File has no content or URI to download.");
        }

        downloadBlob(blob, filename);
    } catch (error) {
        console.error("Error creating download link:", error);
    }
};
