const { pool } = require('./src/config/database');
const jwt = require('jsonwebtoken');
const config = require('./src/config/config');
const User = require('./src/models/User');

(async () => {
  try {
    console.log('=== Testing Middleware Server ===');
    
    // Get Super Admin user
    const [users] = await pool.execute('SELECT id FROM users WHERE email = ?', ['s_admin@gmail.com']);
    const userId = users[0].id;
    const user = await User.findById(userId);
    
    // Create JWT token
    const token = jwt.sign(
      { 
        id: user.id, 
        email: user.email,
        role: user.role,
        role_id: user.role_id
      },
      config.jwt.secret,
      { expiresIn: config.jwt.expiresIn }
    );
    
    console.log('Testing with token:', token.substring(0, 50) + '...');
    
    // Test the middleware server
    const response = await fetch('http://localhost:3001/test', {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ test: 'data' })
    });
    
    console.log('Response status:', response.status);
    
    if (!response.ok) {
      const errorText = await response.text();
      console.error('Error response:', errorText);
    } else {
      const data = await response.json();
      console.log('Success! Data:', data);
    }
    
    process.exit(0);
  } catch (error) {
    console.error('Error:', error);
    process.exit(1);
  }
})();



