#!/usr/bin/env node
import { config } from 'dotenv';
import { sql } from '@vercel/postgres';
config({ path: '.env.local' });

async function addFavoritesSystem() {
  try {
    console.log('⭐ Adding favorites system to database...');

    // Create user_favorites table for all types of favorites
    await sql`
      CREATE TABLE IF NOT EXISTS user_favorites (
        id SERIAL PRIMARY KEY,
        user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
        item_type VARCHAR(50) NOT NULL, -- 'card', 'collection', 'deck'
        item_id INTEGER NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        UNIQUE(user_id, item_type, item_id)
      )
    `;
    console.log('✅ Created user_favorites table');

    // Create indexes for performance
    await sql`CREATE INDEX IF NOT EXISTS idx_user_favorites_user_id ON user_favorites(user_id)`;
    await sql`CREATE INDEX IF NOT EXISTS idx_user_favorites_item_type ON user_favorites(item_type)`;
    await sql`CREATE INDEX IF NOT EXISTS idx_user_favorites_item_id ON user_favorites(item_id)`;
    await sql`CREATE INDEX IF NOT EXISTS idx_user_favorites_user_type ON user_favorites(user_id, item_type)`;
    console.log('⚡ Created indexes for user_favorites');

    console.log('\n🎉 Favorites system added successfully!\n');
    console.log('📋 New Features:');
    console.log('   • Users can favorite cards, collections, and decks');
    console.log('   • Unified favorites table with item_type and item_id');
    console.log('   • Optimized with indexes for fast queries');
    console.log('   • Unique constraint prevents duplicate favorites');

  } catch (error) {
    console.error('❌ Failed to add favorites system:', error);
    process.exit(1);
  }
}

addFavoritesSystem(); 