const fetch = require('node-fetch');

async function testEnvConfig() {
  console.log('🧪 Testing .env configuration integration...\n');
  
  // First, check configuration
  try {
    console.log('1. Checking configuration...');
    const configRes = await fetch('http://localhost:5000/config');
    const config = await configRes.json();
    
    console.log('📋 Current Configuration:');
    console.log(`   Business: ${config.config.BUSINESS_NAME}`);
    console.log(`   Phone: ${config.config.BUSINESS_PHONE}`);
    console.log(`   Printer: ${config.config.PRINTER_DEVICE}`);
    console.log(`   Type: ${config.config.PRINTER_TYPE}`);
    
    // Test print with business info
    const testOrder = {
      orderNumber: "ENV-TEST-001",
      orderType: "dine-in",
      items: [
        { name: "Test Item 1", quantity: 2, price: 500, total: 1000 },
        { name: "Test Item 2", quantity: 1, price: 300, total: 300 }
      ],
      subtotal: 1300,
      total: 1300,
      customerPhone: "0300-9876543",
      notes: "Testing .env configuration",
      copyType: "COUNTER",
      cashierId: "ENV001",
      currentTime: "03:45 PM",
      paymentMethods: { cash: true, online: false }
    };
    
    console.log('\n2. Testing print with business configuration...');
    const printRes = await fetch('http://localhost:5000/print', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(testOrder)
    });
    
    const result = await printRes.json();
    
    if (result.success) {
      console.log('✅ Test successful!');
      console.log(`   Receipt printed for: ${config.config.BUSINESS_NAME}`);
      console.log(`   Business phone on receipt: ${config.config.BUSINESS_PHONE}`);
      console.log(`   Order #: ${result.orderNumber}`);
      
      // Verify business info in response
      if (result.businessName === config.config.BUSINESS_NAME) {
        console.log('✅ Business name correctly configured in response');
      }
    } else {
      console.log('❌ Test failed:', result.error);
    }
    
  } catch (error) {
    console.error('❌ Test error:', error.message);
    console.log('\n💡 Make sure the service is running: npm start');
  }
}

// Run test
testEnvConfig();
