const { echo } = require('shelljs')
const {
  run,
  writeSrc,
  uploadToR2,
  builder: pb,
  renameDist,
  reBuild,
  replaceJSON,
  setChannel
} = require('./build-common')

// Function to add "-legacy" suffix to artifact names in electron-builder.json
function addLegacySuffix () {
  echo('============')
  echo('Updating electron-builder.json to add -legacy suffix')
  replaceJSON((data) => {
    // Update main artifactName
    if (data.artifactName) {
      data.artifactName = data.artifactName.replace('${productName}-${version}-${os}-${arch}.${ext}', '${productName}-${version}-${os}-${arch}-legacy.${ext}') // eslint-disable-line
    }
  })
  echo('Updated artifact names with -legacy suffix')
}

async function main () {
  echo('============================================')
  echo('==== Start: running build for linux part 3 arm64/armv7l legacy ====')
  echo('============================================')

  // Add legacy suffix to names
  addLegacySuffix()

  echo('============================================')
  echo('==== Start: build linux.arm64.tar.gz ====')
  echo('============================================')
  let src = 'linux-arm64-legacy.tar.gz'
  renameDist()
  writeSrc(src)
  replaceJSON(
    (data) => {
      data.linux.target = ['tar.gz']
    }
  )
  setChannel(src)
  // await run(`${reBuild} --arch arm64 -f work/app`)
  await run(`${pb} --linux --arm64`).catch(error => {
    echo('❌ Fatal error in build linux.arm64.tar.gz:')
    console.error(error)
  })
  await uploadToR2(src)

  echo('============================================')
  echo('==== Start: build linux.arm64.deb ====')
  echo('============================================')
  src = 'linux-arm64-legacy.deb'
  renameDist()
  writeSrc(src)
  replaceJSON(
    (data) => {
      data.linux.target = ['deb']
    }
  )
  setChannel(src)
  await run(`${pb} --linux --arm64`).catch(error => {
    echo('❌ Fatal error in build linux.arm64.deb:')
    console.error(error)
  })
  await uploadToR2(src)

  echo('============================================')
  echo('==== Start: build linux.aarch64.rpm ====')
  echo('============================================')
  src = 'linux-aarch64-legacy.rpm'
  renameDist()
  writeSrc(src)
  replaceJSON(
    (data) => {
      data.linux.target = ['rpm']
    }
  )
  setChannel(src)
  await run(`${pb} --linux --arm64`).catch(error => {
    echo('❌ Fatal error in build linux.aarch64.rpm:')
    console.error(error)
  })
  await uploadToR2(src)

  echo('============================================')
  echo('==== Start: build linux.arm64.AppImage ====')
  echo('============================================')
  src = 'linux-arm64-legacy.AppImage'
  renameDist()
  writeSrc(src)
  replaceJSON(
    (data) => {
      data.linux.target = ['AppImage']
    }
  )
  setChannel(src)
  await run(`${pb} --linux --arm64`).catch(error => {
    echo('❌ Fatal error in build linux.arm64.AppImage:')
    console.error(error)
  })
  await uploadToR2(src)

  echo('============================================')
  echo('==== Start: build linux.armv7l.tar.gz ====')
  echo('============================================')
  src = 'linux-armv7l-legacy.tar.gz'
  renameDist()
  writeSrc(src)
  replaceJSON(
    (data) => {
      data.linux.target = ['tar.gz']
    }
  )
  setChannel(src)
  await run(`${reBuild} --arch armv7l -f work/app`)
  await run(`${pb} --linux --armv7l`).catch(error => {
    echo('❌ Fatal error in build linux.armv7l:')
    console.error(error)
  })
  await uploadToR2(src)

  echo('============================================')
  echo('==== Start: build linux.armv7l.deb ====')
  echo('============================================')
  src = 'linux-armv7l-legacy.deb'
  renameDist()
  writeSrc(src)
  replaceJSON(
    (data) => {
      data.linux.target = ['deb']
    }
  )
  setChannel(src)
  await run(`${pb} --linux --armv7l`).catch(error => {
    echo('❌ Fatal error in build linux.armv7l.deb:')
    console.error(error)
  })
  await uploadToR2(src)

  echo('============================================')
  echo('==== Start: build linux.armv7l.rpm ====')
  echo('============================================')
  src = 'linux-armv7l-legacy.rpm'
  renameDist()
  writeSrc(src)
  replaceJSON(
    (data) => {
      data.linux.target = ['rpm']
    }
  )
  setChannel(src)
  await run(`${pb} --linux --armv7l`).catch(error => {
    echo('❌ Fatal error in build linux.armv7l.rpm:')
    console.error(error)
  })
  await uploadToR2(src)

  echo('============================================')
  echo('==== Start: build linux.armv7l.AppImage ====')
  echo('============================================')
  src = 'linux-armv7l-legacy.AppImage'
  renameDist()
  writeSrc(src)
  replaceJSON(
    (data) => {
      data.linux.target = ['AppImage']
    }
  )
  setChannel(src)
  await run(`${pb} --linux --armv7l`).catch(error => {
    echo('❌ Fatal error in build linux.armv7l.AppImage:')
    console.error(error)
  })
  await uploadToR2(src)

  echo('✅ All Linux ARM builds completed successfully')
}

main()
