// Run: node seed-tasks.js
const { createClient } = require('@supabase/supabase-js');
require('dotenv').config();

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

const SEED_TASKS = [
  // Writing
  { title: 'Write a 200-word product description', description: 'Write a compelling product description for a wireless Bluetooth speaker. Include features, benefits, and a call to action.', category: 'writing', budget_vae: 150, workers_needed: 2, duration_days: 3 },
  { title: 'Proofread this blog post', description: 'Check this 500-word blog post for grammar, spelling, and punctuation errors. Mark all corrections clearly.', category: 'writing', budget_vae: 100, workers_needed: 1, duration_days: 2 },
  
  // Data Entry
  { title: 'Enter business contact info', description: 'Enter company names, addresses, and phone numbers from 20 business cards into a spreadsheet format.', category: 'data_entry', budget_vae: 200, workers_needed: 1, duration_days: 2 },
  { title: 'Verify email addresses', description: 'Check if these 50 email addresses are valid and correctly formatted. Mark any with typos.', category: 'data_entry', budget_vae: 100, workers_needed: 2, duration_days: 1 },
  
  // Image/Photo
  { title: 'Take photos of local restaurant menus', description: 'Visit 3 restaurants near you and photograph their full menus clearly. Must be readable.', category: 'image', budget_vae: 500, workers_needed: 3, duration_days: 7 },
  { title: 'Screenshot mobile app screens', description: 'Take screenshots of all screens in the Instagram app signup flow. Include every step.', category: 'image', budget_vae: 150, workers_needed: 1, duration_days: 2 },
  
  // Survey
  { title: 'Complete quick shopping survey', description: 'Answer 10 questions about your online shopping habits. Be honest - we want real data!', category: 'survey', budget_vae: 50, workers_needed: 10, duration_days: 3 },
  { title: 'Rate these 5 logo designs', description: 'Look at 5 logo options and rate each from 1-10. Explain what you like/dislike about each.', category: 'survey', budget_vae: 75, workers_needed: 5, duration_days: 2 },
  
  // Research
  { title: 'Find competitor pricing info', description: 'Research pricing for 5 project management tools (Asana, Monday, Trello, Notion, ClickUp). List all plan prices.', category: 'research', budget_vae: 250, workers_needed: 1, duration_days: 3 },
  { title: 'List top 10 coffee shops in Dubai', description: 'Find the top 10 rated coffee shops in Dubai. Include name, location, Google rating, and specialty.', category: 'research', budget_vae: 150, workers_needed: 2, duration_days: 2 },
  
  // Translation
  { title: 'Translate menu to Spanish', description: 'Translate this restaurant menu (50 items) from English to Spanish. Maintain formatting.', category: 'translation', budget_vae: 300, workers_needed: 1, duration_days: 3 },
  { title: 'Translate app strings to French', description: 'Translate 30 short UI strings from English to French. Keep them concise for buttons/labels.', category: 'translation', budget_vae: 200, workers_needed: 1, duration_days: 2 },
  
  // Audio
  { title: 'Transcribe 5-minute interview', description: 'Listen to this audio file and transcribe every word spoken. Include speaker labels.', category: 'audio', budget_vae: 200, workers_needed: 1, duration_days: 2 },
  { title: 'Record voice greeting in Hindi', description: 'Record a 30-second professional greeting message in Hindi for a customer service line.', category: 'audio', budget_vae: 150, workers_needed: 2, duration_days: 3 },
  
  // Moderation
  { title: 'Review social media comments', description: 'Check 100 comments for spam, hate speech, or inappropriate content. Flag any violations.', category: 'moderation', budget_vae: 200, workers_needed: 2, duration_days: 2 },
  { title: 'Verify product listings', description: 'Check 20 product listings for accuracy. Verify titles match images and descriptions are correct.', category: 'moderation', budget_vae: 150, workers_needed: 1, duration_days: 2 },
  
  // Labeling/AI Training
  { title: 'Label objects in images', description: 'Draw bounding boxes around all cars in 50 street photos. Use our labeling tool link provided.', category: 'labeling', budget_vae: 300, workers_needed: 3, duration_days: 5 },
  { title: 'Classify customer support emails', description: 'Read 30 support emails and categorize each as: Billing, Technical, General, or Complaint.', category: 'labeling', budget_vae: 150, workers_needed: 2, duration_days: 2 },
  
  // Other/Micro
  { title: 'Test website signup flow', description: 'Go through our website signup process and report any bugs or confusing steps. Screenshot issues.', category: 'other', budget_vae: 100, workers_needed: 3, duration_days: 2 },
  { title: 'Give feedback on landing page', description: 'Visit our landing page and provide honest feedback. What would make you sign up or leave?', category: 'other', budget_vae: 75, workers_needed: 5, duration_days: 3 },
];

async function seedTasks() {
  // Use YOUR user ID as the requester (Ray's account)
  const { data: rayUser } = await supabase
    .from('app_users')
    .select('id')
    .eq('username', 'RL')
    .single();
  
  let requesterId;
  
  if (rayUser) {
    requesterId = rayUser.id;
    console.log('Using Ray user ID:', requesterId);
  } else {
    // Create a system user if Ray not found
    const { data: newUser, error: userErr } = await supabase
      .from('app_users')
      .insert({
        telegram_id: '0',
        username: 'LoopumanSystem',
        first_name: 'Loopuman',
        trust_score: 200,
      })
      .select()
      .single();
    
    if (userErr) {
      console.error('Could not create system user:', userErr);
      // Try to find any existing user
      const { data: anyUser } = await supabase
        .from('app_users')
        .select('id')
        .limit(1)
        .single();
      
      if (!anyUser) {
        console.error('No users found. Please create a user first.');
        return;
      }
      requesterId = anyUser.id;
    } else {
      requesterId = newUser.id;
      // Give system user balance
      await supabase.from('vae_balances').insert({
        user_id: requesterId,
        deposit_balance: 100000,
        earned_balance: 0,
      });
    }
    console.log('Created/found requester ID:', requesterId);
  }
  
  // Create tasks
  let created = 0;
  for (const task of SEED_TASKS) {
    const expires = new Date();
    expires.setDate(expires.getDate() + task.duration_days);
    
    const { error } = await supabase.from('tasks').insert({
      requester_id: requesterId,
      title: task.title,
      description: task.description,
      category: task.category,
      budget_vae: task.budget_vae,
      workers_needed: task.workers_needed,
      workers_completed: 0,
      status: 'open',
      expires_at: expires.toISOString(),
      is_remote: true,
    });
    
    if (error) {
      console.error('Error creating task:', task.title, error.message);
    } else {
      created++;
      console.log('✅ Created:', task.title);
    }
  }
  
  console.log(`\n🎉 Created ${created}/${SEED_TASKS.length} seed tasks!`);
}

seedTasks().catch(console.error);
