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

async function testImprovedCartItem() {
  try {
    console.log('Testing improved cart item structure...');
    
    // Test with a product that has ticket types
    console.log('\n--- Testing product with ticket types ---');
    const productId = '261'; // Standard Ticket Lake of Stars
    const ticketTypes = await getProductPrices(productId);
    console.log(`Product ${productId} has ${ticketTypes.length} ticket types:`);
    
    if (ticketTypes.length > 0) {
      // Simulate the improved logic
      let productPriceId = null;
      
      // Simulate no selected ticket type (single ticket type scenario)
      if (ticketTypes.length > 0) {
        productPriceId = ticketTypes[0].product_price_id;
      }
      
      // Create cartItem with improved structure
      const cartItem = {
        [productId]: {
          product_id: productId,
          product_count: 2, // Example quantity
          ...(productPriceId && { product_price_id: productPriceId })
        }
      };
      
      console.log('\nImproved cart item structure:');
      console.log(JSON.stringify(cartItem, null, 2));
      
      // Verify that product_price_id is included
      if (cartItem[productId].hasOwnProperty('product_price_id')) {
        console.log(`✓ product_price_id is included: ${cartItem[productId].product_price_id}`);
      } else {
        console.log('✗ product_price_id is missing');
      }
    }
    
    // Test edge case: Empty ticket types array
    console.log('\n--- Testing edge case: Empty ticket types array ---');
    const emptyTicketTypes = [];
    let productPriceId = null;
    
    if (emptyTicketTypes.length > 0) {
      productPriceId = emptyTicketTypes[0].product_price_id;
    }
    
    // Create cartItem with improved structure
    const productId2 = '999'; // Example product ID
    const cartItem2 = {
      [productId2]: {
        product_id: productId2,
        product_count: 1, // Example quantity
        ...(productPriceId && { product_price_id: productPriceId })
      }
    };
    
    console.log('\nCart item structure with null productPriceId:');
    console.log(JSON.stringify(cartItem2, null, 2));
    
    // Verify that product_price_id is not included when it's null
    if (cartItem2[productId2].hasOwnProperty('product_price_id')) {
      console.log('✗ product_price_id should not be included when it is null');
    } else {
      console.log('✓ product_price_id is correctly omitted when it is null');
    }
    
    // Test edge case: Valid productPriceId
    console.log('\n--- Testing edge case: Valid productPriceId ---');
    productPriceId = '123'; // Example valid ID
    
    // Create cartItem with improved structure
    const cartItem3 = {
      [productId2]: {
        product_id: productId2,
        product_count: 1, // Example quantity
        ...(productPriceId && { product_price_id: productPriceId })
      }
    };
    
    console.log('\nCart item structure with valid productPriceId:');
    console.log(JSON.stringify(cartItem3, null, 2));
    
    // Verify that product_price_id is included when it's valid
    if (cartItem3[productId2].hasOwnProperty('product_price_id')) {
      console.log(`✓ product_price_id is correctly included: ${cartItem3[productId2].product_price_id}`);
    } else {
      console.log('✗ product_price_id should be included when it is valid');
    }
    
    console.log('\nAll tests completed successfully!');
  } catch (error) {
    console.error('Test failed:', error);
  }
}

testImprovedCartItem();