// Run on server: node seed_competitions.js
require('dotenv').config();
const { createClient } = require('@supabase/supabase-js');
const s = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_KEY);

async function seed() {
  // Get Ray's user ID
  const { data: ray, error } = await s.from('app_users')
    .select('id,username').eq('telegram_id', '1831353453').single();

  if (error || !ray) { console.error('User not found:', error); return; }
  console.log('Seeding as user:', ray.id, ray.username);

  const now = new Date();
  const days30 = new Date(now.getTime() + 30 * 86400000).toISOString();
  const days60 = new Date(now.getTime() + 60 * 86400000).toISOString();

  // 1. Open Competition: Write the Best Product Review
  const { data: comp, error: e1 } = await s.from('competitions').insert({
    requester_id: ray.id,
    title: 'Write the Best Product Review',
    description: 'Write a compelling, honest product review (200-400 words) for any product you genuinely use and love. Must include: what problem it solves, who it is for, pros/cons, and a verdict. Best review wins the full prize. All languages welcome.',
    prize_pool: 7500,        // $75
    prize_structure: { '1st': 7500 },
    setup_fee: 1000,         // $10 (under $100)
    platform_fee_pct: 25,
    setup_fee_paid: true,
    min_entries: 5,
    winner_deadline_hours: 72,
    location_required: false,
    allowed_countries: [],
    deadline: days30,
    status: 'active',
    entry_count: 0,
    category: 'writing',
  }).select().single();

  if (e1) console.error('Competition error:', e1);
  else console.log('Competition created:', comp.id, comp.title);

  // 2. Social Contest: Show Your Remote Work Setup
  const { data: chal, error: e2 } = await s.from('social_challenges').insert({
    requester_id: ray.id,
    title: 'Show Your Remote Work Setup',
    description: 'Post a photo or video of your remote work setup on any social platform. Show us where you work from — your home office, a cafe, a rooftop, a beach — anywhere. Be creative! AI-generated and virtual setups welcome too.',
    platform: 'Any',
    hashtag: 'WorkFromAnywhere',
    base_pay_per_post: 300,  // $3 per approved post
    prize_structure: { '1st': 3000, '2nd': 1500, '3rd': 500 },  // $30/$15/$5
    prize_pool: 5000,
    setup_fee: 4900,         // $49
    platform_fee_pct: 25,
    setup_fee_paid: true,
    ai_content_allowed: true,
    requirements: 'Post must be publicly visible. Include #WorkFromAnywhere. Photo/video of your actual or virtual workspace.',
    location_required: false,
    allowed_countries: [],
    deadline: days60,
    status: 'active',
    entry_count: 0,
    approved_count: 0,
  }).select().single();

  if (e2) console.error('Social contest error:', e2);
  else console.log('Social contest created:', chal.id, chal.title);

  console.log('\n✅ Done! Check your bot competitions menu.');
  console.log(`Competition share link: https://t.me/LoopumanBot?start=comp_${comp?.id}`);
  console.log(`Contest share link: https://t.me/LoopumanBot?start=chal_${chal?.id}`);
}

seed().catch(console.error).finally(() => process.exit());
