require('dotenv').config();
const { createClient } = require('@supabase/supabase-js');
const { v4: uuidv4 } = require('uuid');
const s = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_KEY);

async function reseed() {
  await s.from('competitions').delete().eq('id', '4c46ad6e-c335-41dd-b938-70a26554fab0');
  await s.from('social_challenges').delete().eq('id', 'e8d409fa-9b0d-4b5c-b2ed-0fed9affbd53');
  console.log('Old seeds deleted');

  const { data: ray } = await s.from('app_users').select('id').eq('telegram_id', '1831353453').single();
  const uid = ray.id;
  const compId = uuidv4();
  const chalId = uuidv4();

  const compDesc = [
    'Loopuman is the Human Layer for AI.',
    'We connect businesses with verified human workers in 30+ countries, paying instantly in crypto via Telegram.',
    'We need YOUR words.',
    'Write the single most compelling one-liner that would make a business want to try Loopuman immediately.',
    'Think bold, clear, memorable.',
    'Best headline wins the full prize and may appear on our website and all marketing.',
    'All languages welcome.'
  ].join(' ');

  const { error: e1 } = await s.from('competitions').insert({
    id: compId,
    requester_id: uid,
    title: 'Write Loopumans Best Marketing Headline',
    description: compDesc,
    prize_pool: 7500,
    prize_structure: { '1st': 7500 },
    setup_fee: 1000,
    platform_fee_pct: 25,
    setup_fee_paid: true,
    min_entries: 5,
    winner_deadline_hours: 72,
    location_required: false,
    allowed_countries: [],
    deadline: new Date(Date.now() + 90 * 86400000).toISOString(),
    status: 'active',
    entry_count: 0,
    category: 'writing',
  });
  if (e1) console.error('Comp error:', e1.message);
  else console.log('Competition created:', compId);

  const chalDesc = [
    'Post a short video or image showing you completed a task on Loopuman and received instant crypto payment.',
    'Format is completely yours: screen recording of wallet updating, reaction video, text overlay, AI avatar, voiceover only — anything works.',
    'No face required.',
    'Show the world that getting paid instantly in crypto for simple tasks is real.',
    'Every approved post earns base pay instantly.',
    'Top 3 most creative posts win bonus prizes.'
  ].join(' ');

  const { error: e2 } = await s.from('social_challenges').insert({
    id: chalId,
    requester_id: uid,
    title: 'I Got Paid to Do This Challenge',
    description: chalDesc,
    platform: 'Any',
    hashtag: 'PaidForThis',
    base_pay_per_post: 300,
    prize_structure: { '1st': 5000, '2nd': 2500, '3rd': 1000 },
    prize_pool: 8500,
    setup_fee: 4900,
    platform_fee_pct: 25,
    setup_fee_paid: true,
    ai_content_allowed: true,
    requirements: 'Post must be publicly visible. Include #PaidForThis. Must reference completing a task and receiving crypto payment.',
    location_required: false,
    allowed_countries: [],
    deadline: new Date(Date.now() + 90 * 86400000).toISOString(),
    status: 'active',
    entry_count: 0,
    approved_count: 0,
  });
  if (e2) console.error('Contest error:', e2.message);
  else console.log('Social contest created:', chalId);

  await s.from('competitions').update({ status: 'completed' })
    .lt('deadline', new Date().toISOString()).neq('id', compId);
  await s.from('social_challenges').update({ status: 'completed' })
    .lt('deadline', new Date().toISOString()).neq('id', chalId);
  console.log('Old ended competitions archived');

  console.log('\nDone!');
  console.log('Competition link: https://t.me/LoopumanBot?start=comp_' + compId);
  console.log('Contest link: https://t.me/LoopumanBot?start=chal_' + chalId);
}

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