import 'dotenv/config'; // Load environment variables from .env file
import {GoogleAuth} from 'google-auth-library';
import {GoogleGenAI} from '@google/genai';
import * as fs from 'fs'; // Import the file system module

const inputImagePath = 'strawberry.jpg';
const msg1Text1 = {text: `Do you remember a pineapple image? show the pineapple image`};

const outputImagePath = 'strawberry_output.png';
const project = 'gen-lang-client-0531051429';
const location = 'global'; // It's best practice to use a specific region
const model = 'gemini-2.0-flash-preview-image-generation';

// --- Authentication using Service Account Key ---
const auth = new GoogleAuth({
  keyFilename: './gen-lang-client-0531051429-7455e402c041.json', // <-- Path to your downloaded key file
  scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});

// Initialize Vertex with your Cloud project and location
const ai = new GoogleGenAI({
  vertexai: true,
  project: project,
  location: location,
  googleAuth: auth,
});
// Define the log file path
const logFile = 'app.log';

// Set up generation config
const generationConfig = {
  maxOutputTokens: 8192,
  temperature: 1,
  topP: 0.95,
  responseModalities: ["TEXT", "IMAGE"],
  safetySettings: [
    {
      category: 'HARM_CATEGORY_HATE_SPEECH',
      threshold: 'OFF',
    },
    {
      category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
      threshold: 'OFF',
    },
    {
      category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
      threshold: 'OFF',
    },
    {
      category: 'HARM_CATEGORY_HARASSMENT',
      threshold: 'OFF',
    },
    {
      category: 'HARM_CATEGORY_IMAGE_HATE',
      threshold: 'OFF',
    },
    {
      category: 'HARM_CATEGORY_IMAGE_DANGEROUS_CONTENT',
      threshold: 'OFF',
    },
    {
      category: 'HARM_CATEGORY_IMAGE_HARASSMENT',
      threshold: 'OFF',
    },
    {
      category: 'HARM_CATEGORY_IMAGE_SEXUALLY_EXPLICIT',
      threshold: 'OFF',
    }
  ],
};

const imageBuffer = fs.readFileSync(inputImagePath);
const imageBase64 = imageBuffer.toString('base64');

const msg1Image1 = {
  inlineData: {
    mimeType: 'image/jpeg',
    data: imageBase64
  }
};


const chat = ai.chats.create({
  model: model,
  config: generationConfig
});

/**
 * Appends a message to the log file.
 * @param {string} message The message to log.
 */
function writeToLog(message) {
  fs.appendFile(logFile, message + '\n', (err) => {
    if (err) {
      console.error('Error writing to log file:', err);
    }
  });
}

async function sendMessage(message) {
  const response = await chat.sendMessageStream({
    message: message
  });
  console.log(JSON.stringify(response).substring(0, 1000));
  process.stdout.write('stream result: ');
  for await (const chunk of response) {
    const part = chunk.candidates?.[0]?.content?.parts?.[0];

    console.log(JSON.stringify(chunk).substring(0, 200));
    if (chunk.text) {
      process.stdout.write(chunk.text);
    } else if (part && part.inlineData.data !== undefined && JSON.stringify(part).length > 100000) {
      // The image data is in base64 format.
      // Define the file path and name.
      // Write the base64 data to a file. [2, 4, 10]

      return part.inlineData.data
    } else {
      writeToLog(JSON.stringify(chunk));
    }
  }
}


export default async function generateContent(inputImageBase64, textDescription) {
  return await sendMessage([
    {
      inlineData: {
        mimeType: 'image/jpeg',
        data: inputImageBase64.split(',', 2)[1]
      }
    },
    {
      text: textDescription
    }
  ]);
}

/*
export default async function generateContent() {
  await sendMessage([
    msg1Image1,
    msg1Text1
  ]);
}

generateContent()
*/
