const { Sequelize } = require('sequelize');

async function createDatabase() {
  // Connect to default postgres database first
  const sequelize = new Sequelize('postgres', 'postgres', 'postgres', {
    host: 'localhost',
    port: 5432,
    dialect: 'postgres',
    logging: false
  });

  try {
    console.log('🔌 Connecting to PostgreSQL...');
    await sequelize.authenticate();
    console.log('✅ Connected to PostgreSQL successfully');

    // Create the luckychit database
    console.log('🔄 Creating luckychit database...');
    await sequelize.query('CREATE DATABASE luckychit;');
    console.log('✅ Database "luckychit" created successfully');

    await sequelize.close();
    console.log('🎉 Database setup complete!');
    console.log('💡 You can now start the server with: npm run dev');
  } catch (error) {
    if (error.message.includes('already exists')) {
      console.log('✅ Database "luckychit" already exists');
      console.log('💡 You can now start the server with: npm run dev');
    } else {
      console.error('❌ Error creating database:', error.message);
    }
    await sequelize.close();
  }
}

createDatabase().catch(console.error);
