const axios = require('axios');

const API_URL = 'http://localhost:3005/api';

async function testMinimalEmployeeCreation() {
  try {
    console.log('1. Testing login...');
    // First, login to get a token
    const loginResponse = await axios.post(`${API_URL}/auth/login`, {
      username: 'admin',
      password: 'ChangeMe123!@#'
    });

    const token = loginResponse.data.data.token.accessToken;
    console.log('✅ Login successful');

    // Create headers with auth token
    const headers = {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    };

    console.log('\n2. Creating employee with minimal data (like Admin Panel)...');
    // Create a test employee with minimal data
    const minimalEmployeeData = {
      firstName: 'Test',
      lastName: 'Minimal',
      email: 'test.minimal@example.com',
      department: 'IT',
      createUser: true,
      userRole: 'user'
    };

    const createResponse = await axios.post(
      `${API_URL}/employees`,
      minimalEmployeeData,
      { headers }
    );

    console.log('✅ Employee created successfully with minimal data!');
    console.log('Employee ID:', createResponse.data.data.id);
    if (createResponse.data.data.temporaryPassword) {
      console.log('Temporary Password:', createResponse.data.data.temporaryPassword);
    }

    // Fetch the created employee
    console.log('\n3. Fetching created employee...');
    const getResponse = await axios.get(
      `${API_URL}/employees/${createResponse.data.data.id}`,
      { headers }
    );

    console.log('✅ Employee fetched successfully!');
    console.log('Employee Data:');
    console.log('- Name:', getResponse.data.data.firstName, getResponse.data.data.lastName);
    console.log('- Email:', getResponse.data.data.email);
    console.log('- Phone:', getResponse.data.data.phone, '(Default value)');
    console.log('- Position:', getResponse.data.data.position, '(Default value)');
    console.log('- Department:', getResponse.data.data.department);
    console.log('- Employee Number:', getResponse.data.data.employeeNumber, '(Auto-generated)');

    console.log('\n✅ Minimal employee creation working! User can complete profile on first login.');

  } catch (error) {
    console.error('❌ Test failed:', error.response?.data || error.message);
    if (error.response?.data?.error?.details) {
      console.error('Details:', error.response.data.error.details);
    }
  }
}

testMinimalEmployeeCreation();