import { execSync } from 'child_process' // nosemgrep: javascript.lang.security.detect-child-process.detect-child-process
import fs from 'fs'
import path from 'path'

// This is a development-only smoke test script, cmd is hardcoded, not user input
function run(cmd) {
  console.log('> ', cmd)
  // nosemgrep: javascript.lang.security.detect-child-process.detect-child-process
  try { const out = execSync(cmd, { stdio: 'pipe' }).toString(); console.log(out); return out } catch (e) { console.error('ERROR:', e.message); return null }
}

async function main() {
  const root = process.cwd()
  run('node scripts/create-fillable-template.js')
  // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
  const uploads = path.join(root, 'public', 'uploads')
  const files = fs.existsSync(uploads) ? fs.readdirSync(uploads).filter(f => f.toLowerCase().endsWith('.pdf')) : []
  console.log('Uploads PDFs:', files)
  // try API if server env present
  const apiUrl = process.env.MEMBERSHIP_API_URL || ''
  if (apiUrl) {
    run(`curl -sS -X POST "${apiUrl}" -H 'Content-Type: application/json' -d '{"nachname":"Test","vorname":"Smoke","strasse":"X","plz":"00000","ort":"Local","geburtsdatum":"1990-01-01","telefon_privat":"000","telefon_mobil":"000","email":"x@example.com","mitgliedschaftsart":"aktiv","kontoinhaber":"Smoke Test","iban":"DE00","bic":"XXXX","bank":"Local"}' -D - | sed -n '1,80p'`)
  }
  run('node scripts/inspect-forms.js')
}

main()
