import fs from 'fs'
import path from 'path'

const inputPath = process.argv[2]
const outputPath = process.argv[3]

if (!inputPath || !outputPath) {
  console.error('Usage: node strip-fk.js <input.sql> <output.sql>')
  process.exit(1)
}

const input = fs.readFileSync(path.resolve(inputPath), 'utf8')

// Remove FOREIGN KEY clauses from CREATE TABLE statements
const output = input
  .replace(/,?\s*FOREIGN KEY\s*\([^\)]*\)\s*REFERENCES\s*[^;\n]*?/g, '')
  .replace(/\n\s*FOREIGN KEY\s*\([^\)]*\)\s*REFERENCES\s*[^;\n]*\n/gi, '\n')

fs.writeFileSync(path.resolve(outputPath), output, 'utf8')

console.log(`Wrote ${outputPath} without foreign keys`)
