const { getAllProducts, getProductPrices } = require('./api/pamsikaClient');

async function testComprehensivePriceFix() {
  try {
    console.log('Testing comprehensive price fix...');
    
    // Get all products
    const products = await getAllProducts();
    console.log(`Found ${products.length} products`);
    
    // Find products with null price
    const nullPriceProducts = products.filter(product => 
      product.product_price === null || product.product_price === undefined);
    
    console.log(`\nFound ${nullPriceProducts.length} products with null price:`);
    for (const product of nullPriceProducts) {
      console.log(`- ${product.product_name} (ID: ${product.product_id})`);
      
      try {
        // Try to get product prices
        const ticketTypes = await getProductPrices(product.product_id);
        if (ticketTypes && ticketTypes.length > 0) {
          console.log(`  -> Found ${ticketTypes.length} ticket types:`);
          ticketTypes.forEach(type => {
            console.log(`    - ${type.product_price_name}: MWK ${type.product_price || 'NULL'}`);
          });
        } else {
          console.log(`  -> No ticket types found`);
        }
      } catch (error) {
        console.error(` -> Error fetching ticket types:`, error.message);
      }
    }
    
    // Also check some products with valid prices to make sure they still work
    const validPriceProducts = products.filter(product => 
      product.product_price !== null && product.product_price !== undefined);
    
    console.log(`\nFound ${validPriceProducts.length} products with valid price (showing first 3):`);
    for (let i = 0; i < Math.min(3, validPriceProducts.length); i++) {
      const product = validPriceProducts[i];
      console.log(`- ${product.product_name}: MWK ${product.product_price}`);
      
      try {
        // Try to get product prices for these too
        const ticketTypes = await getProductPrices(product.product_id);
        if (ticketTypes && ticketTypes.length > 0) {
          console.log(`  -> Found ${ticketTypes.length} ticket types:`);
          ticketTypes.forEach(type => {
            console.log(`    - ${type.product_price_name}: MWK ${type.product_price || 'NULL'}`);
          });
        } else {
          console.log(`  -> No ticket types found`);
        }
      } catch (error) {
        console.error(`  -> Error fetching ticket types:`, error.message);
      }
    }
    
    console.log('\nTest completed.');
  } catch (error) {
    console.error('Test failed:', error);
  }
}

testComprehensivePriceFix();