import fs from "fs"
import path from "path"
import clipboardy from "clipboardy"
import { fileURLToPath } from "url"
import { dirname } from "path"

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

const credsPath = path.resolve(process.cwd(), "src/utils/creds.json")

if (!fs.existsSync(credsPath)) {
  console.error("❌ File not found:", credsPath)
  process.exit(1)
}

try {
  const content = fs.readFileSync(credsPath, "utf-8")
  const parsed = JSON.parse(content)
  const stringified = JSON.stringify(parsed)

  await clipboardy.write(stringified)

  console.log("✅ JSON credentials copied to clipboard.")
  console.log("👉 Paste it into your .env like this:\n")
  console.log("GCP_CREDENTIALS_JSON='<PASTE_HERE>'\n")

  // Eliminar el archivo creds.json después de copiar
  fs.unlinkSync(credsPath)
  console.log("🗑️ creds.json has been deleted to avoid propagation.")
} catch (error) {
  console.error("❌ Error reading or parsing creds.json:", error.message)
  process.exit(1)
}
