#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import fse from 'fs-extra';
import inquirer from 'inquirer';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function run() {
  const templatesDir = path.join(__dirname, '../templates');

  const projects = fs.readdirSync(templatesDir)
    .filter(item =>
      fs.statSync(path.join(templatesDir, item)).isDirectory()
    );

  const { project } = await inquirer.prompt([
    {
      type: 'list',
      name: 'project',
      message: 'Select a project:',
      choices: projects
    }
  ]);

  const { projectName } = await inquirer.prompt([
    {
      type: 'input',
      name: 'projectName',
      message: 'Enter project name:'
    }
  ]);

  const source = path.join(templatesDir, project);
  const destination = path.join(process.cwd(), projectName);

  await fse.copy(source, destination);

  console.log(`✅ Project "${projectName}" created successfully!`);
}

run().catch(console.error);