require('dotenv').config();
const database = require('./src/config/database');

const SAMPLE_TEMPLATES = [
  {
    type: 'blog_platform',
    title: 'Blog Platform',
    description: 'Modern blog with content management, comments, and SEO',
    icon: '📝',
    category: 'Content',
    gradient: 'from-purple-50 to-purple-100',
    border: 'border-purple-200',
    text: 'text-purple-900',
    subtext: 'text-purple-700'
  },
  {
    type: 'task_manager',
    title: 'Task Manager',
    description: 'Project and task management with team collaboration',
    icon: '✅',
    category: 'Productivity',
    gradient: 'from-green-50 to-green-100',
    border: 'border-green-200',
    text: 'text-green-900',
    subtext: 'text-green-700'
  },
  {
    type: 'analytics_dashboard',
    title: 'Analytics Dashboard',
    description: 'Data visualization and business intelligence platform',
    icon: '📊',
    category: 'Business',
    gradient: 'from-blue-50 to-blue-100',
    border: 'border-blue-200',
    text: 'text-blue-900',
    subtext: 'text-blue-700'
  },
  {
    type: 'social_network',
    title: 'Social Network',
    description: 'Connect with friends, share content, and build communities',
    icon: '🌐',
    category: 'Social',
    gradient: 'from-pink-50 to-pink-100',
    border: 'border-pink-200',
    text: 'text-pink-900',
    subtext: 'text-pink-700'
  },
  {
    type: 'learning_platform',
    title: 'Learning Platform',
    description: 'Online courses, quizzes, and educational content',
    icon: '🎓',
    category: 'Education',
    gradient: 'from-yellow-50 to-yellow-100',
    border: 'border-yellow-200',
    text: 'text-yellow-900',
    subtext: 'text-yellow-700'
  }
];

async function addSampleTemplates() {
  const client = await database.connect();
  
  try {
    await client.query('BEGIN');
    
    console.log('🚀 Adding sample templates...');
    
    for (const template of SAMPLE_TEMPLATES) {
      const query = `
        INSERT INTO templates (
          id, type, title, description, icon, category, 
          gradient, border, text, subtext, is_active, created_at, updated_at
        ) VALUES (
          gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, true, NOW(), NOW()
        )
      `;
      
      const values = [
        template.type,
        template.title,
        template.description,
        template.icon,
        template.category,
        template.gradient,
        template.border,
        template.text,
        template.subtext
      ];
      
      await client.query(query, values);
      console.log(`✅ Added template: ${template.title}`);
    }
    
    await client.query('COMMIT');
    console.log('🎉 Sample templates added successfully!');
    
  } catch (error) {
    await client.query('ROLLBACK');
    console.error('❌ Error adding sample templates:', error.message);
    throw error;
  } finally {
    client.release();
  }
}

// Run if called directly
if (require.main === module) {
  addSampleTemplates()
    .then(() => {
      console.log('🎉 Process completed!');
      process.exit(0);
    })
    .catch((error) => {
      console.error('💥 Process failed:', error.message);
      process.exit(1);
    });
}

module.exports = { addSampleTemplates };
