const{createClient}=require('@supabase/supabase-js');
const s=createClient('https://avxzzqudkqboesygfysr.supabase.co','eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImF2eHp6cXVka3Fib2VzeWdmeXNyIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc2NTg5NTM4NywiZXhwIjoyMDgxNDcxMzg3fQ.6LJGZulo8e8NTGv8tQMXNfFdH_zpzsihaWDIuBsH8Gk');

async function migrate(){
  console.log('Running competition tables migration...\n');

  const migrations=[
    // COMPETITIONS table additions
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS requester_id UUID REFERENCES app_users(id)`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS category TEXT DEFAULT 'other'`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS prize_structure JSONB DEFAULT '{"1st":100}'`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS setup_fee INTEGER DEFAULT 2500`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS platform_fee_pct INTEGER DEFAULT 25`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS entry_count INTEGER DEFAULT 0`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS max_entries INTEGER DEFAULT 500`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS min_entries INTEGER DEFAULT 5`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS winner_deadline_hours INTEGER DEFAULT 72`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS winner_selected_at TIMESTAMPTZ`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS setup_fee_paid BOOLEAN DEFAULT false`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS reference_media JSONB DEFAULT '[]'`,
    `ALTER TABLE competitions ADD COLUMN IF NOT EXISTS auto_winner BOOLEAN DEFAULT false`,

    // SOCIAL_CHALLENGES table additions
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS requester_id UUID REFERENCES app_users(id)`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS base_pay_per_post INTEGER DEFAULT 300`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS prize_structure JSONB DEFAULT '{"1st":5000,"2nd":2500,"3rd":1000}'`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS setup_fee INTEGER DEFAULT 4900`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS platform_fee_pct INTEGER DEFAULT 25`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS entry_count INTEGER DEFAULT 0`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS approved_count INTEGER DEFAULT 0`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS max_entries INTEGER DEFAULT 500`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS judging_deadline TIMESTAMPTZ`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS setup_fee_paid BOOLEAN DEFAULT false`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS total_paid_out INTEGER DEFAULT 0`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS ai_content_allowed BOOLEAN DEFAULT true`,
    `ALTER TABLE social_challenges ADD COLUMN IF NOT EXISTS requirements TEXT`,

    // COMPETITION_ENTRIES additions
    `ALTER TABLE competition_entries ADD COLUMN IF NOT EXISTS worker_wallet TEXT`,
    `ALTER TABLE competition_entries ADD COLUMN IF NOT EXISTS flagged BOOLEAN DEFAULT false`,
    `ALTER TABLE competition_entries ADD COLUMN IF NOT EXISTS flag_reason TEXT`,
    `ALTER TABLE competition_entries ADD COLUMN IF NOT EXISTS is_winner BOOLEAN DEFAULT false`,
    `ALTER TABLE competition_entries ADD COLUMN IF NOT EXISTS paid_at TIMESTAMPTZ`,

    // CHALLENGE_ENTRIES additions
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS views INTEGER DEFAULT 0`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS likes INTEGER DEFAULT 0`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS comments INTEGER DEFAULT 0`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS engagement_score INTEGER DEFAULT 0`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS approved_by_requester BOOLEAN`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS base_pay_paid BOOLEAN DEFAULT false`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS bonus_paid BOOLEAN DEFAULT false`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS worker_wallet TEXT`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS flagged BOOLEAN DEFAULT false`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS platform TEXT`,
    `ALTER TABLE challenge_entries ADD COLUMN IF NOT EXISTS post_url TEXT`,
  ];

  let ok=0,fail=0;
  for(const sql of migrations){
    const{error}=await s.rpc('exec_sql',{sql}).catch(()=>({error:{message:'rpc not available'}}));
    if(error){
      // Try direct approach
      console.log(`  SKIP (will handle via insert): ${sql.substring(0,60)}...`);
      fail++;
    } else {
      console.log(`  ✅ ${sql.substring(0,60)}...`);
      ok++;
    }
  }
  console.log(`\nDone. OK:${ok} Skip:${fail}`);
  console.log('\nNote: If skips occurred, columns may need adding via Supabase dashboard SQL editor.');
  console.log('Copy the ALTER TABLE statements and run in Supabase SQL editor at:');
  console.log('https://supabase.com/dashboard/project/avxzzqudkqboesygfysr/sql');
}
migrate().catch(console.error);
