// scripts/make-model.ts
import fs from 'fs';
import path from 'path';

const rawName = process.argv[2];

if (!rawName) {
  console.error('❌ Please provide a model name: npm run make:model ModelName');
  process.exit(1);
}

const className = rawName.charAt(0).toUpperCase() + rawName.slice(1);
const fileName = `${className}.ts`;
const filePath = path.resolve(__dirname, `../models/${fileName}`);

if (fs.existsSync(filePath)) {
  console.error(`❌ Model ${className} already exists at src/models/${fileName}`);
  process.exit(1);
}

const template = `import { Model, RelationMappings } from 'objection';

export class ${className} extends Model {
  static tableName = '${rawName.toLowerCase()}s'; // Adjust if your table name is different

  id!: number;

  // Add your columns here

  static relationMappings = (): RelationMappings => ({
    // Define relations here, for example:
    // posts: {
    //   relation: Model.HasManyRelation,
    //   modelClass: Post,
    //   join: {
    //     from: '${rawName.toLowerCase()}s.id',
    //     to: 'posts.user_id',
    //   },
    // },
  });
}
`;

fs.writeFileSync(filePath, template);
console.log(`✅ Model created at src/models/${fileName}`);
