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

async function testCartItemStructure() {
  try {
    console.log('Testing cart item structure...');
    
    // Test with a product that has multiple ticket types
    console.log('\n--- Testing product with multiple ticket types ---');
    const multiTicketProductId = '213'; // Early Bird Standard - 3 Days Pass
    const multiTicketTypes = await getProductPrices(multiTicketProductId);
    console.log(`Product ${multiTicketProductId} has ${multiTicketTypes.length} ticket types:`);
    multiTicketTypes.forEach((type, index) => {
      console.log(`  ${index + 1}. ${type.product_price_name}: MWK ${type.product_price} (ID: ${type.product_price_id})`);
    });
    
    // Simulate the cartItem structure for multiple ticket types
    if (multiTicketTypes.length > 1) {
      // Simulate selecting the first ticket type
      const selectedTicketType = multiTicketTypes[0];
      
      // Create cartItem as it would be created in the fixed code
      let productPriceId = null;
      if (selectedTicketType) {
        productPriceId = selectedTicketType.product_price_id;
      } else if (multiTicketTypes.length > 0) {
        productPriceId = multiTicketTypes[0].product_price_id;
      }
      
      const cartItem = {
        [multiTicketProductId]: {
          product_id: multiTicketProductId,
          product_count: 2, // Example quantity
          product_price_id: productPriceId
        }
      };
      
      console.log('\nCart item structure for multiple ticket types:');
      console.log(JSON.stringify(cartItem, null, 2));
    }
    
    // Test with a product that has null price in main record but valid price in product prices
    console.log('\n--- Testing product with null main price but valid product prices ---');
    const nullPriceProductId = '261'; // Standard Ticket Lake of Stars
    const nullPriceTicketTypes = await getProductPrices(nullPriceProductId);
    console.log(`Product ${nullPriceProductId} has ${nullPriceTicketTypes.length} ticket types:`);
    nullPriceTicketTypes.forEach((type, index) => {
      console.log(`  ${index + 1}. ${type.product_price_name}: MWK ${type.product_price} (ID: ${type.product_price_id})`);
    });
    
    // Simulate the cartItem structure for a product with null main price
    if (nullPriceTicketTypes.length > 0) {
      // Create cartItem as it would be created in the fixed code
      let productPriceId = null;
      // Simulate no specific ticket type selected (which is the case for single ticket type products)
      if (nullPriceTicketTypes.length > 0) {
        productPriceId = nullPriceTicketTypes[0].product_price_id;
      }
      
      const cartItem = {
        [nullPriceProductId]: {
          product_id: nullPriceProductId,
          product_count: 3, // Example quantity
          product_price_id: productPriceId
        }
      };
      
      console.log('\nCart item structure for product with null main price:');
      console.log(JSON.stringify(cartItem, null, 2));
    }
    
    console.log('\nTest completed successfully!');
  } catch (error) {
    console.error('Test failed:', error);
  }
}

testCartItemStructure();