// Script to check your Project IDs and User IDs
// Run this with: node backend/check-ids.js

require('dotenv').config();
const sequelize = require('./db');
const Projects = require('./models/projects');
const Users = require('./models/users');

async function checkIds() {
  console.log('🔍 Checking available Projects and Users...\n');

  try {
    await sequelize.authenticate();
    console.log('✅ Database connected\n');

    // Get all projects
    console.log('📁 Available Projects:');
    console.log('═══════════════════════════════════════════════════════════');
    const projects = await Projects.findAll({
      attributes: ['id', 'name', 'companyId'],
      order: [['id', 'ASC']],
    });

    if (projects.length === 0) {
      console.log('   ⚠️  No projects found in database\n');
    } else {
      projects.forEach(project => {
        console.log(`   ID: ${project.id} | Name: ${project.name} | Company: ${project.companyId}`);
      });
      console.log('');
    }

    // Get all users
    console.log('👥 Available Users:');
    console.log('═══════════════════════════════════════════════════════════');
    const users = await Users.findAll({
      attributes: ['id', 'name', 'email', 'role'],
      order: [['id', 'ASC']],
    });

    if (users.length === 0) {
      console.log('   ⚠️  No users found in database\n');
    } else {
      users.forEach(user => {
        console.log(`   ID: ${user.id} | Name: ${user.name} | Email: ${user.email} | Role: ${user.role}`);
      });
      console.log('');
    }

    // Recommendations
    console.log('💡 Recommendations:');
    console.log('═══════════════════════════════════════════════════════════');
    if (projects.length > 0 && users.length > 0) {
      console.log(`   Use PROJECT_ID: ${projects[0].id} (${projects[0].name})`);
      console.log(`   Use USER_ID: ${users[0].id} (${users[0].name})`);
      console.log('');
      console.log('📝 Next step:');
      console.log(`   1. Edit backend/insert-test-safety-tools.js`);
      console.log(`   2. Set PROJECT_ID = ${projects[0].id}`);
      console.log(`   3. Set USER_ID = ${users[0].id}`);
      console.log(`   4. Run: node backend/insert-test-safety-tools.js`);
    } else {
      console.log('   ⚠️  Please create a project and user first');
      console.log('   1. Login to the application');
      console.log('   2. Create a company and project');
      console.log('   3. Run this script again');
    }
    console.log('');

  } catch (error) {
    console.error('❌ Error:', error.message);
  } finally {
    await sequelize.close();
  }
}

checkIds();
