/**
 * Setup Stripe Products and Prices
 * Run once: node scripts/setup-stripe-products.js
 */

require('dotenv').config();
const Stripe = require('stripe');

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

async function setupStripeProducts() {
  console.log('Setting up Stripe products and prices...\n');

  try {
    // 1. Create Starter Kit product
    console.log('Creating Starter Kit product...');
    const starterKit = await stripe.products.create({
      name: 'WellNuo Starter Kit',
      description: '2x Motion Sensors + 1x Door Sensor + 1x Hub - Everything you need to start monitoring',
      metadata: {
        type: 'hardware',
        sku: 'KIT-STARTER-001'
      }
    });
    console.log(`✓ Product created: ${starterKit.id}`);

    // Create price for Starter Kit ($249 one-time)
    const starterKitPrice = await stripe.prices.create({
      product: starterKit.id,
      unit_amount: 24900, // $249.00
      currency: 'usd',
      metadata: {
        display_name: 'Starter Kit'
      }
    });
    console.log(`✓ Price created: ${starterKitPrice.id} ($249.00)\n`);

    // 2. Create Premium Subscription product
    console.log('Creating Premium Subscription product...');
    const premium = await stripe.products.create({
      name: 'WellNuo Premium',
      description: 'AI Julia assistant, 90-day activity history, invite up to 5 family members',
      metadata: {
        type: 'subscription',
        tier: 'premium'
      }
    });
    console.log(`✓ Product created: ${premium.id}`);

    // Create price for Premium ($9.99/month)
    const premiumPrice = await stripe.prices.create({
      product: premium.id,
      unit_amount: 999, // $9.99
      currency: 'usd',
      recurring: {
        interval: 'month'
      },
      metadata: {
        display_name: 'Premium Monthly'
      }
    });
    console.log(`✓ Price created: ${premiumPrice.id} ($9.99/month)\n`);

    // Summary
    console.log('='.repeat(50));
    console.log('STRIPE SETUP COMPLETE');
    console.log('='.repeat(50));
    console.log('\nAdd these to your .env file:\n');
    console.log(`STRIPE_PRICE_STARTER_KIT=${starterKitPrice.id}`);
    console.log(`STRIPE_PRICE_PREMIUM=${premiumPrice.id}`);
    console.log(`STRIPE_PRODUCT_STARTER_KIT=${starterKit.id}`);
    console.log(`STRIPE_PRODUCT_PREMIUM=${premium.id}`);
    console.log('\n');

    return {
      starterKit: {
        productId: starterKit.id,
        priceId: starterKitPrice.id
      },
      premium: {
        productId: premium.id,
        priceId: premiumPrice.id
      }
    };

  } catch (error) {
    console.error('Error setting up Stripe:', error.message);
    throw error;
  }
}

setupStripeProducts()
  .then(() => process.exit(0))
  .catch(() => process.exit(1));
