const fs = require("fs");
const path = require("path");
const logger = require("./server/utils/logger"); // Assuming same logger utility
const s3Client = require("./server/utils/s3"); // Using the S3 client utilities with LocalStack support

// Set bucket name for development with LocalStack
const S3_BUCKET_NAME = "imex-job-totals";

// Set fixtures directory path
const FIXTURES_DIR = path.join(__dirname, "server", "job", "test", "fixtures", "job-totals");

const ensureFixturesDirectory = () => {
  if (!fs.existsSync(FIXTURES_DIR)) {
    fs.mkdirSync(FIXTURES_DIR, { recursive: true });
    logger.log(`Created fixtures directory: ${FIXTURES_DIR}`, "info");
  }
};

const downloadJsonFiles = async (userInfo = { email: "system" }) => {
  logger.log(`Starting download of JSON files from bucket: ${S3_BUCKET_NAME}`, "debug", userInfo.email);

  try {
    ensureFixturesDirectory();
    const contents = await s3Client.listFilesInS3Bucket(S3_BUCKET_NAME);

    if (!contents.length) {
      logger.log("No files found in bucket", "info", userInfo.email);
      return;
    }

    logger.log(`Found ${contents.length} files in bucket`, "info", userInfo.email);

    for (const item of contents) {
      if (!item.Key.endsWith(".json")) {
        logger.log(`Skipping non-JSON file: ${item.Key}`, "debug", userInfo.email);
        continue;
      }

      logger.log(`Downloading: ${item.Key}`, "debug", userInfo.email);
      const fileData = await s3Client.downloadFileFromS3({
        bucketName: S3_BUCKET_NAME,
        key: item.Key
      });

      const fileContent = await fileData.transformToString();
      const fileName = path.basename(item.Key);
      const filePath = path.join(FIXTURES_DIR, fileName);

      fs.writeFileSync(filePath, fileContent);
      logger.log(`Saved: ${filePath}`, "info", userInfo.email);
    }

    logger.log("Download completed successfully", "info", userInfo.email);
  } catch (error) {
    logger.log("Failed to download JSON files", "error", userInfo.email, null, {
      error: error?.message,
      stack: error?.stack
    });
    throw error; // Re-throw to trigger process exit with error code
  }
};

// Run the download if script is executed directly
if (require.main === module) {
  (async () => {
    try {
      await downloadJsonFiles();
      console.log("Script completed successfully");
      process.exit(0); // Explicitly exit with success code
    } catch (error) {
      console.error("Fatal error downloading files:", error);
      process.exit(1); // Explicitly exit with error code
    }
  })();
}

module.exports = downloadJsonFiles;
