import { readdir, readFile } from "node:fs/promises";
import * as path from "node:path";
import { getBucketeerUrlFromSuperblocksUrl } from "@superblocksteam/util";
import { getErrorMeta } from "../util/logger.js";
import { getScopedToken } from "./util/scoped-token-utils.js";
/**
 * Uploads all context files from the context folder to Bucketeer
 * Reads all files from .superblocks/context/{userId}/ and uploads them
 * Dynamically detects MIME types for any file type
 */
export async function uploadContextToBucketeer({ applicationId, commitId, branchName, appRootDirPath, userId, superblocksBaseUrl, logger, jwt, }) {
    // Get the context directory path for this userId
    const contextDir = path.join(appRootDirPath, ".superblocks", "context", userId);
    // Read all files in the context directory
    let contextFiles;
    try {
        const entries = await readdir(contextDir, { withFileTypes: true });
        // Filter to only files (exclude directories) and get their names
        contextFiles = entries
            .filter((entry) => entry.isFile())
            .map((entry) => entry.name);
    }
    catch (err) {
        if (err.code === "ENOENT") {
            // Context directory doesn't exist - this is fine, just return
            logger.info(`[uploadContextToBucketeer] No context directory found for userId=${userId}, skipping upload`);
            return;
        }
        throw err;
    }
    if (contextFiles.length === 0) {
        logger.info(`[uploadContextToBucketeer] No context files found for userId=${userId}, skipping upload`);
        return;
    }
    // Get scoped token using shared utility
    let scopedToken;
    try {
        scopedToken = await getScopedToken({
            applicationId,
            superblocksBaseUrl,
            jwt,
        });
    }
    catch (error) {
        logger.error("[uploadContextToBucketeer] Failed to get scoped token", getErrorMeta(error));
        throw error;
    }
    // Create FormData and append all context files
    const formData = new FormData();
    const uploadedFiles = [];
    for (const fileName of contextFiles) {
        const filePath = path.join(contextDir, fileName);
        try {
            // Read file as buffer to preserve binary content
            const fileBuffer = await readFile(filePath);
            // Convert Buffer to Uint8Array for Blob
            // Content-Type is optional - server can infer from filename
            const uint8Array = new Uint8Array(fileBuffer);
            const blob = new Blob([uint8Array]);
            // Append each file with the same field name "context"
            formData.append("context", blob, fileName);
            uploadedFiles.push(fileName);
        }
        catch (err) {
            logger.warn(`[uploadContextToBucketeer] Failed to read context file ${fileName}, skipping`, getErrorMeta(err));
            // Continue with other files even if one fails
        }
    }
    if (uploadedFiles.length === 0) {
        logger.warn(`[uploadContextToBucketeer] No files were successfully read for upload`);
        return;
    }
    // Get Bucketeer URL from Superblocks base URL
    const bucketeerUrl = getBucketeerUrlFromSuperblocksUrl(superblocksBaseUrl);
    // Upload to Bucketeer using the branch-aware context endpoint
    const url = `${bucketeerUrl}/applications/${applicationId}/branches/${encodeURIComponent(branchName)}/commits/${encodeURIComponent(commitId)}/context`;
    try {
        const response = await fetch(url, {
            method: "POST",
            body: formData,
            headers: {
                Authorization: `Bearer ${scopedToken}`,
                // Content-Type will be set automatically by fetch with boundary
            },
        });
        if (!response.ok) {
            const errorText = await response
                .text()
                .catch(() => "Unable to read error response");
            logger.error(`[uploadContextToBucketeer] Upload failed: status=${response.status}, statusText=${response.statusText}, error=${errorText} for applicationId=${applicationId}, commitId=${commitId}`);
            throw new Error(`Context upload failed: ${response.statusText}`);
        }
        logger.info(`[uploadContextToBucketeer] Successfully uploaded ${uploadedFiles.length} context file(s) (${uploadedFiles.join(", ")}) for applicationId=${applicationId}, commitId=${commitId}`);
    }
    catch (error) {
        logger.error("[uploadContextToBucketeer] Failed to upload context", getErrorMeta(error));
        throw error;
    }
}
//# sourceMappingURL=context-upload.js.map