import fs from 'fs';
import path from 'path';
import { middlewarePath, middlewareTemplate } from './middleware.config';

const rawName = process.argv[2];
if (!rawName) {
  console.error('❌ Please provide a middleware name: npm run make:middleware Name');
  process.exit(1);
}

const name = rawName.charAt(0).toLowerCase() + rawName.slice(1);
const filePath = path.resolve(__dirname, `../${middlewarePath}/${name}.ts`);

if (fs.existsSync(filePath)) {
  console.error(`❌ Middleware already exists at ${filePath}`);
  process.exit(1);
}

fs.writeFileSync(filePath, middlewareTemplate(name));
console.log(`✅ Middleware created at ${middlewarePath}/${name}.ts`);
