# All files / controllers PlanManagementController.js

0% Statements 0/227
0% Branches 0/160
0% Functions 0/18
0% Lines 0/215

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861

     /**
     * Plan Management Controller
     * Handles tenant plan and add-on resources management
     */

    const BaseController = require('./BaseController');
    const { pool } = require('../config/database');
    const { logger } = require('../config/logger');

    class PlanManagementController extends BaseController {
      /**
       * Get current plan information
       * GET /api/tenant/plan/current
       */
      static async getCurrentPlan(req, res) {
        try {
          const tenantId = req.tenantId;

          const [tenants] = await pool.execute(
            `SELECT t.*, sp.name as plan_name, sp.price, sp.currency, sp.billing_period, sp.description, c.symbol as currency_symbol
             FROM tenants t
             LEFT JOIN subscription_plans sp ON t.plan_id = sp.id
             LEFT JOIN currencies c ON c.code = sp.currency
             WHERE t.id = ?`,
            [tenantId]
          );

          if (tenants.length === 0) {
            return res.status(404).json({
              success: false,
              message: 'Tenant not found'
            });
          }

          const tenant = tenants[0];

          // Get plan features (you can customize this based on your plan structure)
          const features = [];
          if (tenant.max_users > 0) features.push(`Up to ${tenant.max_users} users`);
          if (tenant.max_conversations > 0) features.push(`Up to ${tenant.max_conversations} conversations`);
          if (tenant.max_messages_per_month > 0) features.push(`${tenant.max_messages_per_month} messages/month`);

          return res.json({
            success: true,
            data: {
              plan_name: tenant.plan_name,
              price: tenant.price,
              currency: tenant.currency,
              currency_symbol: tenant.currency_symbol,
              billing_period: tenant.billing_period || 'monthly',
              status: tenant.status,
              trial_ends_at: tenant.trial_ends_at,
              features: features
            }
          });
        } catch (error) {
          logger.error('Error getting current plan', { error: error.message });
          return res.status(500).json({
            success: false,
            message: 'Error loading plan information'
          });
        }
      }

      /**
       * Get resources usage
       * GET /api/tenant/plan/resources-usage
       */
      static async getResourcesUsage(req, res) {
        try {
          const tenantId = req.tenantId;
          console.log('📊 Getting resources usage for tenant:', tenantId);

          // Get tenant limits
          const [tenants] = await pool.execute(
            'SELECT * FROM tenants WHERE id = ?',
            [tenantId]
          );

          if (tenants.length === 0) {
            console.log('❌ Tenant not found:', tenantId);
            return res.status(404).json({
              success: false,
              message: 'Tenant not found'
            });
          }

          const tenant = tenants[0];
          console.log('✅ Tenant found:', tenant.name);

          // Get current usage
          console.log('📊 Querying stores...');
          const [stores] = await pool.execute(
            'SELECT COUNT(*) as count FROM stores WHERE tenant_id = ?',
            [tenantId]
          );

          console.log('📊 Querying departments...');
          const [departments] = await pool.execute(
            'SELECT COUNT(*) as count FROM departments WHERE tenant_id = ?',
            [tenantId]
          );

          console.log('📊 Querying users...');
          const [users] = await pool.execute(
            'SELECT COUNT(*) as count FROM admins WHERE tenant_id = ?',
            [tenantId]
          );

          console.log('📊 Querying conversations...');
          const [conversations] = await pool.execute(
            'SELECT COUNT(*) as count FROM conversations WHERE tenant_id = ?',
            [tenantId]
          );

          console.log('📊 Querying contacts...');
          const [contacts] = await pool.execute(
            'SELECT COUNT(*) as count FROM contacts WHERE tenant_id = ?',
            [tenantId]
          );

          console.log('📊 Querying WhatsApp instances...');
          let whatsappInstancesCount = 0;
          try {
            const [instances] = await pool.execute(
              'SELECT COUNT(*) as count FROM evolution_instances WHERE tenant_id = ? AND is_active = TRUE',
              [tenantId]
            );
            whatsappInstancesCount = instances[0].count;
            console.log(`✅ Found ${whatsappInstancesCount} WhatsApp instances`);
          } catch (err) {
            console.log('⚠️ Could not query instances:', err.message);
          }

          console.log('✅ All queries completed successfully');

          // Calculate usage
          const usage = {
            stores: {
              used: stores[0].count,
              limit: tenant.max_stores || -1,
              percentage: tenant.max_stores > 0 ? (stores[0].count / tenant.max_stores) * 100 : 0
            },
            departments: {
              used: departments[0].count,
              limit: tenant.max_departments || -1,
              percentage: tenant.max_departments > 0 ? (departments[0].count / tenant.max_departments) * 100 : 0
            },
            users: {
              used: users[0].count,
              limit: tenant.max_users || -1,
              percentage: tenant.max_users > 0 ? (users[0].count / tenant.max_users) * 100 : 0
            },
            conversations: {
              used: conversations[0].count,
              limit: tenant.max_conversations || -1,
              percentage: tenant.max_conversations > 0 ? (conversations[0].count / tenant.max_conversations) * 100 : 0
            },
            contacts: {
              used: contacts[0].count,
              limit: tenant.max_contacts || -1,
              percentage: tenant.max_contacts > 0 ? (contacts[0].count / tenant.max_contacts) * 100 : 0
            },
            whatsapp_instances: {
              used: whatsappInstancesCount,
              limit: tenant.max_whatsapp_instances || -1,
              percentage: tenant.max_whatsapp_instances > 0 ? (whatsappInstancesCount / tenant.max_whatsapp_instances) * 100 : 0
            }
          };

          console.log('✅ Usage calculated:', usage);

          return res.json({
            success: true,
            data: usage
          });
        } catch (error) {
          console.error('❌ Error in getResourcesUsage:', error);
          logger.error('Error getting resources usage', { error: error.message, stack: error.stack });
          return res.status(500).json({
            success: false,
            message: 'Error loading resources usage'
          });
        }
      }

      /**
       * Get purchased addons for tenant (grouped by resource type with usage)
       * GET /api/tenant/plan/purchased-addons
       */
      static async getPurchasedAddons(req, res) {
        try {
          const tenantId = req.tenantId;

          // Get tenant addons grouped by resource_key with total quantity
          const [tenantAddons] = await pool.execute(
            `SELECT
              pa.resource_key,
              pa.resource_name,
              pa.currency,
              pa.description,
              SUM(ta.quantity) as total_quantity,
              MIN(ta.started_at) as first_purchase_date
            FROM tenant_addons ta
            JOIN plan_addons pa ON ta.addon_id = pa.id
            WHERE ta.tenant_id = ? AND ta.status = 'active'
            GROUP BY pa.resource_key, pa.resource_name, pa.currency, pa.description
            ORDER BY first_purchase_date DESC`,
            [tenantId]
          );

          // Get current usage for each addon type
          const usageQueries = {
            widget: 'SELECT COUNT(*) as count FROM chat_widgets WHERE tenant_id = ?',
            widgets: 'SELECT COUNT(*) as count FROM chat_widgets WHERE tenant_id = ?',
            invoice: `SELECT COUNT(*) as count FROM invoices WHERE tenant_id = ? AND MONTH(created_at) = MONTH(CURRENT_DATE()) AND YEAR(created_at) = YEAR(CURRENT_DATE())`,
            invoices: `SELECT COUNT(*) as count FROM invoices WHERE tenant_id = ? AND MONTH(created_at) = MONTH(CURRENT_DATE()) AND YEAR(created_at) = YEAR(CURRENT_DATE())`,
            quotes: `SELECT COUNT(*) as count FROM quotes WHERE tenant_id = ? AND MONTH(created_at) = MONTH(CURRENT_DATE()) AND YEAR(created_at) = YEAR(CURRENT_DATE())`,
            payment_links: `SELECT COUNT(*) as count FROM payment_links WHERE tenant_id = ? AND MONTH(created_at) = MONTH(CURRENT_DATE()) AND YEAR(created_at) = YEAR(CURRENT_DATE())`,
            stores: 'SELECT COUNT(*) as count FROM stores WHERE tenant_id = ?',
            departments: 'SELECT COUNT(*) as count FROM departments WHERE tenant_id = ?',
            users: 'SELECT COUNT(*) as count FROM admins WHERE tenant_id = ?',
            contacts: 'SELECT COUNT(*) as count FROM contacts WHERE tenant_id = ?',
            faq: 'SELECT COUNT(*) as count FROM faqs WHERE tenant_id = ?',
            faqs: 'SELECT COUNT(*) as count FROM faqs WHERE tenant_id = ?',
            email_accounts: 'SELECT COUNT(*) as count FROM email_accounts WHERE tenant_id = ?',
            email_sends: `SELECT COUNT(*) as count FROM email_messages WHERE tenant_id = ? AND direction = 'sent' AND MONTH(COALESCE(sent_at, created_at)) = MONTH(CURRENT_DATE()) AND YEAR(COALESCE(sent_at, created_at)) = YEAR(CURRENT_DATE())`,
            omnichannel_accounts: 'SELECT COUNT(*) as count FROM omnichannel_accounts WHERE tenant_id = ?'
          };

          // Get tenant limits for addon resources
          const [tenants] = await pool.execute(
            `SELECT max_widgets, max_invoices_per_month, max_quotes_per_month, max_payment_links_per_month, max_email_accounts, max_emails_per_month, max_omnichannel_accounts
             FROM tenants WHERE id = ?`,
            [tenantId]
          );

          const tenant = tenants[0] || {};

          // Add usage info to each addon
          const addonsWithUsage = await Promise.all(tenantAddons.map(async (addon) => {
            let used = 0;
            let limit = addon.total_quantity;

            // Get usage based on resource type
            const query = usageQueries[addon.resource_key];
            if (query) {
              try {
                const [result] = await pool.execute(query, [tenantId]);
                used = result[0]?.count || 0;
              } catch (e) {
                // Table might not exist, ignore
              }
            }

            // For some resources, the limit comes from tenant table
            if (addon.resource_key === 'widget' || addon.resource_key === 'widgets') {
              limit = tenant.max_widgets || addon.total_quantity;
            } else if (addon.resource_key === 'invoice' || addon.resource_key === 'invoices') {
              limit = tenant.max_invoices_per_month || addon.total_quantity;
            } else if (addon.resource_key === 'quotes') {
              limit = tenant.max_quotes_per_month || addon.total_quantity;
            } else if (addon.resource_key === 'payment_links') {
              limit = tenant.max_payment_links_per_month || addon.total_quantity;
            } else if (addon.resource_key === 'email_accounts') {
              limit = tenant.max_email_accounts || addon.total_quantity;
            } else if (addon.resource_key === 'email_sends') {
              limit = tenant.max_emails_per_month || addon.total_quantity;
            } else if (addon.resource_key === 'omnichannel_accounts') {
              limit = tenant.max_omnichannel_accounts || addon.total_quantity;
            }

            return {
              ...addon,
              used,
              limit,
              percentage: limit > 0 ? Math.min((used / limit) * 100, 100) : 0
            };
          }));

          return res.json({
            success: true,
            data: {
              addons: addonsWithUsage
            }
          });
        } catch (error) {
          logger.error('Error getting purchased addons', { error: error.message });
          return res.status(500).json({
            success: false,
            message: 'Error loading purchased addons'
          });
        }
      }

      /**
       * Get available add-ons
       * GET /api/tenant/plan/available-addons
       */
      static async getAvailableAddons(req, res) {
        try {
          // Get default currency
          const [currencyRows] = await pool.execute(
            `SELECT code, symbol FROM currencies WHERE is_default = TRUE AND active = TRUE ORDER BY id LIMIT 1`
          );
          const defaultCurrency = currencyRows && currencyRows[0] ? currencyRows[0] : { code: 'USD', symbol: '$' };

          const [addons] = await pool.execute(
            `SELECT * FROM plan_addons
             WHERE active = TRUE
             ORDER BY sort_order, resource_name`
          );

          // Override currency with default currency for all addons
          const addonsWithDefaultCurrency = addons.map(addon => ({
            ...addon,
            currency: defaultCurrency.code,
            currency_symbol: defaultCurrency.symbol
          }));

          return res.json({
            success: true,
            data: addonsWithDefaultCurrency
          });
        } catch (error) {
          logger.error('Error getting available add-ons', { error: error.message });
          return res.status(500).json({
            success: false,
            message: 'Error loading add-ons'
          });
        }
      }

      /**
       * Checkout add-ons
       * POST /api/tenant/plan/checkout-addons
       */
      static async checkoutAddons(req, res) {
        try {
          const tenantId = req.tenantId;
          const { items, gateway } = req.body;

          console.log('Checkout addons request:', { tenantId, items, gateway });

          if (!items || items.length === 0) {
            return res.status(400).json({
              success: false,
              message: 'No items in cart'
            });
          }

          if (!gateway) {
            return res.status(400).json({
              success: false,
              message: 'Payment gateway is required'
            });
          }

          // Get tenant info
          const [tenants] = await pool.execute(
            'SELECT * FROM tenants WHERE id = ?',
            [tenantId]
          );

          if (tenants.length === 0) {
            return res.status(404).json({
              success: false,
              message: 'Tenant not found'
            });
          }

          const tenant = tenants[0];

          // Fetch full addon details from database (including price_ids)
          const addonIds = items.map(item => item.addon_id);
          const [addons] = await pool.execute(
            `SELECT * FROM plan_addons WHERE id IN (${addonIds.map(() => '?').join(',')})`,
            addonIds
          );

          // Create a map for quick lookup
          const addonMap = {};
          addons.forEach(addon => {
            addonMap[addon.id] = addon;
          });

          // Process items with full addon data
          let total = 0;
          const processedItems = items.map(item => {
            const addon = addonMap[item.addon_id];
            if (!addon) {
              throw new Error(`Addon ${item.addon_id} not found`);
            }

            const quantity = parseInt(item.quantity) || 0;
            const unitPrice = parseFloat(addon.unit_price) || 0;
            const subtotal = quantity * unitPrice;
            total += subtotal;

            return {
              addon_id: addon.id,
              resource_key: addon.resource_key,
              resource_name: addon.resource_name,
              quantity,
              unit_price: unitPrice,
              currency: addon.currency,
              stripe_price_id: addon.stripe_price_id,
              paypal_plan_id: addon.paypal_plan_id,
              subtotal
            };
          });

          // VERIFY GATEWAY CONFIGURATION
          // STRIPE AND PAYPAL DISABLED FOR ADDONS - ONLY CASH PAYMENT ALLOWED
          if (gateway === 'stripe') {
            return res.status(400).json({
              success: false,
              message: 'Stripe payment for addons is temporarily disabled. Please use Cash/Transfer payment method.'
            });

            /* COMMENTED OUT - FOR FUTURE REACTIVATION
            // Check if all items have stripe_price_id
            const missingPriceIds = processedItems.filter(item => !item.stripe_price_id);
            if (missingPriceIds.length > 0) {
              return res.status(400).json({
                success: false,
                message: 'Some addons are not configured for Stripe. Please configure Stripe Price IDs in addon settings.'
              });
            }

            // Check if Stripe is configured
            const stripeKey = await PlanManagementController.getSettingValue('stripe_secret_key');
            if (!stripeKey) {
              return res.status(400).json({
                success: false,
                message: 'Stripe is not configured. Please configure Stripe API key in system settings.'
              });
            }
            */
          } else if (gateway === 'paypal') {
            return res.status(400).json({
              success: false,
              message: 'PayPal payment for addons is temporarily disabled. Please use Cash/Transfer payment method.'
            });

            /* COMMENTED OUT - FOR FUTURE REACTIVATION
            // Check if all items have paypal_plan_id
            const missingPlanIds = processedItems.filter(item => !item.paypal_plan_id);
            if (missingPlanIds.length > 0) {
              return res.status(400).json({
                success: false,
                message: 'Some addons are not configured for PayPal. Please configure PayPal Plan IDs in addon settings.'
              });
            }

            // Check if PayPal is configured
            const paypalClientId = await PlanManagementController.getSettingValue('paypal_client_id');
            const paypalSecret = await PlanManagementController.getSettingValue('paypal_client_secret');
            if (!paypalClientId || !paypalSecret) {
              return res.status(400).json({
                success: false,
                message: 'PayPal is not configured. Please configure PayPal credentials in system settings.'
              });
            }
            */
          }

          console.log('Creating addon purchase:', { tenantId, total, gateway, processedItems });

          // Check if addon_purchases table exists, create if not
          try {
            await pool.execute(`
              CREATE TABLE IF NOT EXISTS addon_purchases (
                id INT AUTO_INCREMENT PRIMARY KEY,
                tenant_id INT NOT NULL,
                items JSON NOT NULL,
                total_amount DECIMAL(10, 2) NOT NULL,
                currency VARCHAR(3) DEFAULT 'USD',
                status ENUM('pending', 'completed', 'failed', 'cancelled') DEFAULT 'pending',
                payment_method VARCHAR(50),
                payment_id VARCHAR(255),
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                INDEX idx_tenant_id (tenant_id),
                INDEX idx_status (status)
              ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
            `);
          } catch (tableError) {
            console.log('Table check/create:', tableError.message);
          }

          // Create addon purchase record
          const [result] = await pool.execute(
            `INSERT INTO addon_purchases (tenant_id, items, total_amount, status, payment_method, created_at)
             VALUES (?, ?, ?, 'pending', ?, NOW())`,
            [tenantId, JSON.stringify(processedItems), total, gateway]
          );

          const purchaseId = result.insertId;

          console.log('Purchase created:', purchaseId);

          // Handle different payment gateways
          // STRIPE AND PAYPAL DISABLED FOR ADDONS
          if (gateway === 'stripe') {
            // DISABLED - Uncomment to reactivate
            // return await PlanManagementController.createStripeAddonCheckout(req, res, purchaseId, processedItems, total, tenant);
            return res.status(400).json({
              success: false,
              message: 'Stripe payment for addons is temporarily disabled. Please use Cash/Transfer.'
            });
          } else if (gateway === 'paypal') {
            // DISABLED - Uncomment to reactivate
            // return await PlanManagementController.createPayPalAddonOrder(req, res, purchaseId, processedItems, total, tenant);
            return res.status(400).json({
              success: false,
              message: 'PayPal payment for addons is temporarily disabled. Please use Cash/Transfer.'
            });
          } else if (gateway === 'cash') {
            // For cash/transfer, return payment instructions directly
            const instructions = await PlanManagementController.getSettingValue('cash_payment_instructions')
              || 'Please contact support for payment instructions.';

            // Get default currency with symbol
            const [currencyRows] = await pool.execute(
              `SELECT code, symbol FROM currencies WHERE is_default = TRUE AND active = TRUE ORDER BY id LIMIT 1`
            );
            const defaultCurrency = currencyRows && currencyRows[0] ? currencyRows[0] : { code: 'USD', symbol: '$' };

            return res.json({
              success: true,
              data: {
                purchase_id: purchaseId,
                payment_method: 'cash',
                total_amount: total,
                currency: defaultCurrency.code,
                currency_symbol: defaultCurrency.symbol,
                instructions: instructions,
                items: processedItems
              }
            });
          } else {
            return res.status(400).json({
              success: false,
              message: 'Invalid payment gateway'
            });
          }
        } catch (error) {
          console.error('Error processing addon checkout:', error);
          logger.error('Error processing addon checkout', { error: error.message, stack: error.stack });
          return res.status(500).json({
            success: false,
            message: 'Error processing checkout: ' + error.message
          });
        }
      }

      /**
       * Helper to get setting value from payment_gateway_settings or system_settings
       */
      static async getSettingValue(key) {
        try {
          // Map of keys to their gateway and field names
          const gatewayMap = {
            'stripe_secret_key': { gateway: 'stripe', field: 'stripe_secret_key' },
            'stripe_publishable_key': { gateway: 'stripe', field: 'stripe_publishable_key' },
            'paypal_client_id': { gateway: 'paypal', field: 'paypal_client_id' },
            'paypal_client_secret': { gateway: 'paypal', field: 'paypal_client_secret' },
            'paypal_mode': { gateway: 'paypal', field: 'paypal_mode' }
          };

          // Check if it's a payment gateway setting
          if (gatewayMap[key]) {
            const { gateway, field } = gatewayMap[key];
            const [result] = await pool.execute(
              `SELECT ${field} FROM payment_gateway_settings WHERE gateway_name = ?`,
              [gateway]
            );
            return result.length > 0 ? result[0][field] : null;
          }

          // Otherwise, try system_settings
          const [result] = await pool.execute(
            "SELECT setting_value FROM system_settings WHERE setting_key = ?",
            [key]
          );
          return result.length > 0 ? result[0].setting_value : null;
        } catch (e) {
          console.log(`Could not fetch setting ${key}:`, e.message);
          return null;
        }
      }

      /**
       * Create Stripe checkout session for add-ons
       */
      static async createStripeAddonCheckout(req, res, purchaseId, items, total, tenant) {
        try {
          const stripeSecretKey = await PlanManagementController.getSettingValue('stripe_secret_key');
          const stripe = require('stripe')(stripeSecretKey);

          console.log('🔵 Creating Stripe checkout:', { purchaseId, total, itemsCount: items.length });
          console.log('🔵 Items:', JSON.stringify(items, null, 2));

          // Use configured price IDs instead of creating prices dynamically
          const lineItems = items.map(item => ({
            price: item.stripe_price_id, // Use the configured price ID
            quantity: parseInt(item.quantity) || 1
          }));

          console.log('🔵 Line items for Stripe:', JSON.stringify(lineItems, null, 2));

          // Create checkout session
          const session = await stripe.checkout.sessions.create({
            payment_method_types: ['card'],
            line_items: lineItems,
            mode: 'subscription',
            success_url: `${req.protocol}://${req.get('host')}/payment-success?purchase=${purchaseId}&type=addon`,
            cancel_url: `${req.protocol}://${req.get('host')}/admin/plan-management`,
            client_reference_id: `addon_${purchaseId}`,
            customer_email: tenant.email,
            metadata: {
              tenant_id: String(tenant.id),
              purchase_id: String(purchaseId),
              type: 'addon'
            }
          });

          console.log('✅ Stripe session created:', { sessionId: session.id, url: session.url });

          // Update purchase with payment ID
          await pool.execute(
            'UPDATE addon_purchases SET payment_id = ? WHERE id = ?',
            [session.id, purchaseId]
          );

          return res.json({
            success: true,
            data: {
              purchase_id: purchaseId,
              checkout_url: session.url
            }
          });
        } catch (error) {
          console.error('❌ Stripe addon checkout error:', error);
          logger.error('Stripe addon checkout error', { error: error.message, stack: error.stack });
          return res.status(500).json({
            success: false,
            message: 'Error creating Stripe checkout: ' + error.message
          });
        }
      }

      /**
       * Create PayPal subscription for add-ons
       */
      static async createPayPalAddonOrder(req, res, purchaseId, items, total, tenant) {
        try {
          const clientId = await PlanManagementController.getSettingValue('paypal_client_id');
          const clientSecret = await PlanManagementController.getSettingValue('paypal_client_secret');
          const mode = await PlanManagementController.getSettingValue('paypal_mode') || 'sandbox';

          // For PayPal subscriptions with multiple items, we need to create a custom plan
          // or use the billing agreements API
          // For now, we'll use a one-time payment approach

          const paypal = require('@paypal/checkout-server-sdk');
          const environment = mode === 'live'
            ? new paypal.core.LiveEnvironment(clientId, clientSecret)
            : new paypal.core.SandboxEnvironment(clientId, clientSecret);
          const client = new paypal.core.PayPalHttpClient(environment);

          // Ensure total is a valid number
          const totalAmount = parseFloat(total) || 0;
          const currency = (items[0]?.currency || 'USD').toUpperCase();

          // Create order (one-time payment for now)
          // TODO: Implement proper subscription handling with PayPal Plan IDs
          const request = new paypal.orders.OrdersCreateRequest();
          request.prefer('return=representation');
          request.requestBody({
            intent: 'CAPTURE',
            purchase_units: [{
              reference_id: `addon_${purchaseId}`,
              description: `Add-on purchase for ${tenant.name}`,
              amount: {
                currency_code: currency,
                value: totalAmount.toFixed(2),
                breakdown: {
                  item_total: {
                    currency_code: currency,
                    value: totalAmount.toFixed(2)
                  }
                }
              },
              items: items.map(item => ({
                name: item.resource_name,
                description: `${item.quantity}x ${item.resource_name}`,
                unit_amount: {
                  currency_code: currency,
                  value: parseFloat(item.unit_price).toFixed(2)
                },
                quantity: String(parseInt(item.quantity) || 1)
              }))
            }],
            application_context: {
              return_url: `${req.protocol}://${req.get('host')}/payment-success?purchase=${purchaseId}&type=addon`,
              cancel_url: `${req.protocol}://${req.get('host')}/admin/plan-management`,
              brand_name: 'Your SaaS Platform',
              user_action: 'PAY_NOW'
            }
          });

          const order = await client.execute(request);

          // Update purchase with payment ID
          await pool.execute(
            'UPDATE addon_purchases SET payment_id = ? WHERE id = ?',
            [order.result.id, purchaseId]
          );

          // Get approval URL
          const approvalUrl = order.result.links.find(link => link.rel === 'approve').href;

          return res.json({
            success: true,
            data: {
              purchase_id: purchaseId,
              checkout_url: approvalUrl
            }
          });
        } catch (error) {
          logger.error('PayPal addon order error', { error: error.message, stack: error.stack });
          return res.status(500).json({
            success: false,
            message: 'Error creating PayPal order: ' + error.message
          });
        }
      }

      /**
       * Get system add-ons (Bio Link, etc.)
       * GET /api/tenant/plan/system-addons
       */
      static async getSystemAddons(req, res) {
        try {
          const tenantId = req.tenantId;

          // Get tenant's plan to check which system addons are enabled
          const [tenants] = await pool.execute(
            `SELECT t.*, sp.biolink_enabled, sp.max_bio_pages, sp.max_short_links,
                    sp.max_file_transfers, sp.max_vcards, sp.max_event_links,
                    sp.max_html_pages, sp.max_qr_codes
             FROM tenants t
             LEFT JOIN subscription_plans sp ON t.plan_id = sp.id
             WHERE t.id = ?`,
            [tenantId]
          );

          if (tenants.length === 0) {
            return res.status(404).json({
              success: false,
              message: 'Tenant not found'
            });
          }

          const tenant = tenants[0];
          const systemAddons = {};

          // Check if Bio Link addon is enabled
          if (tenant.biolink_enabled) {
            // Get usage counts for Bio Link resources
            let bioPageCount = 0, shortLinkCount = 0, qrCodeCount = 0;
            let fileTransferCount = 0, vcardCount = 0, eventLinkCount = 0, htmlPageCount = 0;

            try {
              // Count bio pages
              const [bioPages] = await pool.execute(
                `SELECT COUNT(*) as count FROM biolink_projects WHERE tenant_id = ? AND type = 'biopage'`,
                [tenantId]
              );
              bioPageCount = bioPages[0]?.count || 0;

              // Count short links
              const [shortLinks] = await pool.execute(
                `SELECT COUNT(*) as count FROM biolink_projects WHERE tenant_id = ? AND type = 'shortlink'`,
                [tenantId]
              );
              shortLinkCount = shortLinks[0]?.count || 0;

              // Count QR codes
              const [qrCodes] = await pool.execute(
                `SELECT COUNT(*) as count FROM biolink_projects WHERE tenant_id = ? AND type = 'qrcode'`,
                [tenantId]
              );
              qrCodeCount = qrCodes[0]?.count || 0;

              // Count file transfers
              const [fileTransfers] = await pool.execute(
                `SELECT COUNT(*) as count FROM biolink_projects WHERE tenant_id = ? AND type = 'file'`,
                [tenantId]
              );
              fileTransferCount = fileTransfers[0]?.count || 0;

              // Count vCards
              const [vcards] = await pool.execute(
                `SELECT COUNT(*) as count FROM biolink_projects WHERE tenant_id = ? AND type = 'vcard'`,
                [tenantId]
              );
              vcardCount = vcards[0]?.count || 0;

              // Count event links
              const [eventLinks] = await pool.execute(
                `SELECT COUNT(*) as count FROM biolink_projects WHERE tenant_id = ? AND type = 'event'`,
                [tenantId]
              );
              eventLinkCount = eventLinks[0]?.count || 0;

              // Count HTML pages
              const [htmlPages] = await pool.execute(
                `SELECT COUNT(*) as count FROM biolink_projects WHERE tenant_id = ? AND type = 'html'`,
                [tenantId]
              );
              htmlPageCount = htmlPages[0]?.count || 0;
            } catch (e) {
              // Tables might not exist yet if addon was just activated
              logger.warn('Bio Link tables not found, using zero counts');
            }

            systemAddons.biolink = {
              enabled: true,
              name: 'Bio Link',
              description: 'Create bio pages, short links, QR codes, and more',
              resources: [
                { key: 'bio_pages', name: 'Bio Pages', used: bioPageCount, limit: tenant.max_bio_pages || 0 },
                { key: 'short_links', name: 'Short Links', used: shortLinkCount, limit: tenant.max_short_links || 0 },
                { key: 'qr_codes', name: 'QR Codes', used: qrCodeCount, limit: tenant.max_qr_codes || 0 },
                { key: 'file_transfers', name: 'File Transfers', used: fileTransferCount, limit: tenant.max_file_transfers || 0 },
                { key: 'vcards', name: 'vCards', used: vcardCount, limit: tenant.max_vcards || 0 },
                { key: 'event_links', name: 'Event Links', used: eventLinkCount, limit: tenant.max_event_links || 0 },
                { key: 'html_pages', name: 'HTML Pages', used: htmlPageCount, limit: tenant.max_html_pages || 0 }
              ]
            };
          }

          return res.json({
            success: true,
            data: systemAddons
          });
        } catch (error) {
          logger.error('Error getting system add-ons', { error: error.message });
          return res.status(500).json({
            success: false,
            message: 'Error loading system add-ons'
          });
        }
      }
    }

    module.exports = PlanManagementController;