import fs from "node:fs"
import { AnyOctokit } from "./types.js"

const APP_ID: string = process.env.APP_ID || "" // Replace with your GitHub App ID

const PRIVATE_KEY: string = fs.readFileSync("codeflash-app.2023-11-04.private-key.pem", "utf8")
const CLIENT_ID: string = process.env.CLIENT_ID || "" // Replace with your GitHub App client ID
const CLIENT_SECRET: string = process.env.CLIENT_SECRET || "" // Replace with your GitHub App client secret

if (!APP_ID || !PRIVATE_KEY || !CLIENT_ID || !CLIENT_SECRET) {
  throw new Error("Environment variables are not set")
}

export async function createWorkflow(repoOwner: string, repoName: string) {
  const content = Buffer.from(
    `
name: Code Optimization

on:
  push:
    branches:
      - main

jobs:
  optimize:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: Codeflash/optimize-action@main
      with:
        token:
  `,
  ).toString("base64")

  let installationOctokit: AnyOctokit
  await installationOctokit.rest.repos.createOrUpdateFileContents({
    owner: repoOwner,
    repo: repoName,
    path: ".github/workflows/optimize.yml",
    message: "Setup Code Optimization action",
    content,
  })
}

// Example usage
createWorkflow("user-or-org", "repo-name") // Replace with actual owner, repo name, and installation ID
