require('dotenv').config();
const { createClient } = require('@supabase/supabase-js');

const supabase = createClient(
  process.env.SUPABASE_URL,
  process.env.SUPABASE_SERVICE_KEY
);

const YOUR_USER_ID = '97c472eb-7db6-4639-90f0-a48047b2e46e';

const testTasks = [
  { title: "Take photo of your breakfast", description: "Take a clear photo of your breakfast meal today. Upload the image as proof.", category: "data_collection", budget: 100, max_workers: 5 },
  { title: "Record audio saying hello", description: "Record yourself saying 'Hello, my name is [your name]' in English. Provide text description.", category: "data_collection", budget: 150, max_workers: 10 },
  { title: "Photo of street sign in your city", description: "Take a photo of any street sign showing the street name clearly. Upload image.", category: "data_collection", budget: 75, max_workers: 8 },
  { title: "List 5 popular local foods", description: "Type 5 foods that are popular in your country/region with brief descriptions.", category: "data_collection", budget: 50, max_workers: 15 },
  { title: "Photo of your workspace", description: "Take a photo of your desk/workspace (no personal items needed). Upload image.", category: "data_collection", budget: 100, max_workers: 6 },
  { title: "Find cheapest iPhone 15 price", description: "Search online and report the cheapest iPhone 15 price you can find with store name and URL", category: "research", budget: 200, max_workers: 3 },
  { title: "List 3 restaurants near Times Square", description: "Find 3 restaurants within walking distance of Times Square NYC with ratings", category: "research", budget: 150, max_workers: 5 },
  { title: "What's trending on Twitter today?", description: "List the top 3 trending topics on Twitter/X right now with brief explanation", category: "research", budget: 100, max_workers: 4 },
  { title: "Find Bitcoin current price", description: "Report current Bitcoin price in USD from a reliable source. Include source URL.", category: "research", budget: 50, max_workers: 10 },
  { title: "Research best VPN for streaming", description: "Find and list 3 VPNs recommended for streaming with brief pros/cons", category: "research", budget: 300, max_workers: 2 },
  { title: "Write 3 product review sentences", description: "Write 3 sentences reviewing any product you own. Be specific and honest.", category: "content_creation", budget: 200, max_workers: 8 },
  { title: "Create a catchy coffee shop slogan", description: "Write a creative slogan for a coffee shop (max 10 words). Make it memorable.", category: "content_creation", budget: 250, max_workers: 5 },
  { title: "Write short poem about the ocean", description: "Write a 4-line poem about the ocean. Be creative and descriptive.", category: "content_creation", budget: 150, max_workers: 10 },
  { title: "Describe your favorite movie", description: "Name your favorite movie and describe it in exactly 2 sentences.", category: "content_creation", budget: 100, max_workers: 12 },
  { title: "Write Instagram caption for sunset", description: "Write an engaging Instagram caption for a sunset photo. Include 3 hashtags.", category: "content_creation", budget: 150, max_workers: 7 },
  { title: "Rate your smartphone satisfaction", description: "Rate how satisfied you are with your current smartphone (1-10) and explain why briefly.", category: "surveys", budget: 75, max_workers: 20 },
  { title: "Online vs in-store shopping", description: "Do you prefer shopping online or in physical stores? Explain in 2-3 sentences.", category: "surveys", budget: 100, max_workers: 15 },
  { title: "Daily social media usage", description: "Estimate your daily social media usage in hours and list top 2 platforms", category: "surveys", budget: 50, max_workers: 25 },
  { title: "Verify email address format", description: "Is support@example.com a valid email format? Answer yes/no and explain.", category: "verification", budget: 50, max_workers: 5 },
  { title: "Check if website is loading", description: "Visit google.com and confirm if it loads. Answer yes/no and describe.", category: "verification", budget: 25, max_workers: 10 },
  { title: "Verify phone number format", description: "Is +1-555-0123 a valid US phone number format? Answer yes/no and explain.", category: "verification", budget: 50, max_workers: 8 },
  { title: "Count to 10 in your language", description: "Write numbers 1-10 in your native language with English pronunciation", category: "other", budget: 100, max_workers: 12 },
  { title: "What time is it where you are?", description: "Report current local time and timezone. Include city/country.", category: "other", budget: 25, max_workers: 30 },
  { title: "Describe your typical breakfast", description: "Describe what you typically eat for breakfast. Include 3-4 items.", category: "other", budget: 75, max_workers: 20 },
  { title: "Name 3 apps you use daily", description: "List 3 apps you use every day and briefly explain what you use each for", category: "other", budget: 50, max_workers: 18 }
];

async function insertTasks() {
  console.log('🚀 Inserting test tasks...\n');
  
  let successCount = 0;
  let failCount = 0;
  
  for (const task of testTasks) {
    const { data, error } = await supabase.from('tasks').insert({
      requester_id: YOUR_USER_ID,
      title: task.title,
      description: task.description,
      category: task.category,
      budget: task.budget,
      max_workers: task.max_workers,
      status: 'active',
      is_local: false,
      current_workers: 0
    }).select();

    if (error) {
      console.error(`❌ ${task.title}`);
      console.error(`   ${error.message}\n`);
      failCount++;
    } else {
      console.log(`✅ ${task.title} (Ⓥ${task.budget} = $${(task.budget/100).toFixed(2)})`);
      successCount++;
    }
  }

  console.log(`\n${'='.repeat(70)}`);
  console.log(`✅ SUCCESS: ${successCount} tasks created`);
  console.log(`❌ FAILED: ${failCount} tasks`);
  console.log(`📊 TOTAL: ${testTasks.length} tasks attempted`);
  console.log(`${'='.repeat(70)}\n`);
}

insertTasks().then(() => {
  console.log('✅ Done! Now restart telegram-bot:');
  console.log('   pm2 restart telegram-bot\n');
  process.exit(0);
}).catch(err => {
  console.error('❌ Script failed:', err);
  process.exit(1);
});
