import { createClient } from '@supabase/supabase-js';

const SUPABASE_URL = 'https://mhvdpopbbtllhvzcpqkf.supabase.co';
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im1odmRwb3BiYnRsbGh2emNwcWtmIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzE0MjUwMTUsImV4cCI6MjA4NzAwMTAxNX0.59OvUt4dj8-OguyD6dItdhC2jvUPSJVXNbsUxdk9w_s';

const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);

const DOMAIN_MAP = {
  'TECHNICAL': [
    { id: 'Computer Science and Engineering', name: 'CSE', image: 'https://images.unsplash.com/photo-1517694712202-14dd9538aa97?auto=format&fit=crop&q=80&w=600' },
    { id: 'Computer Science and Business Systems', name: 'CSBS', image: 'https://images.unsplash.com/photo-1460925895917-afdab827c52f?auto=format&fit=crop&q=80&w=600' },
    { id: 'Artificial Intelligence and Machine Learning (AIML)', name: 'AIML', image: 'https://images.unsplash.com/photo-1677442136019-21780ecad995?auto=format&fit=crop&q=80&w=600' },
    { id: 'AIDS', name: 'AIDS', image: 'https://images.unsplash.com/photo-1509228627152-72ae9ae6848d?auto=format&fit=crop&q=80&w=600' },
    { id: 'Information Technology', name: 'IT', image: 'https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&q=80&w=600' },
    { id: 'ECE', name: 'ECE', image: 'https://images.unsplash.com/photo-1517077304055-6e89abbf09b0?auto=format&fit=crop&q=80&w=600' },
    { id: 'Mechanical', name: 'Mechanical', image: 'https://images.unsplash.com/photo-1537462715879-360eeb61a0ad?auto=format&fit=crop&q=80&w=600' },
    { id: 'Bio-tech', name: 'Bio-tech', image: 'https://images.unsplash.com/photo-1530210124550-912dc1381cb8?auto=format&fit=crop&q=80&w=600' },
    { id: 'CCE', name: 'CCE', image: 'https://images.unsplash.com/photo-1550751827-4bd374c3f58b?auto=format&fit=crop&q=80&w=600' },
    { id: 'VLSI', name: 'VLSI', image: 'https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&q=80&w=600' }
  ],
  'NON-TECHNICAL': [
    { id: 'Management', name: 'Management', image: 'https://images.unsplash.com/photo-1519389950473-47ba0277781c?auto=format&fit=crop&q=80&w=400' },
    { id: 'Arts & Culture', name: 'Arts & Culture', image: 'https://images.unsplash.com/photo-1513364776144-60967b0f800f?auto=format&fit=crop&q=80&w=800' },
    { id: 'Sports & Athletics', name: 'Sports', image: 'https://images.unsplash.com/photo-1552674605-db6ffd4facb5?auto=format&fit=crop&q=80&w=400' },
    { id: 'Social Service', name: 'Social Welfare', image: 'https://images.unsplash.com/photo-1488521787991-ed7bbaae773c?auto=format&fit=crop&q=80&w=400' }
  ],
  'WORKSHOP': [
    { id: 'Software Engineering', name: 'Software Dev', image: 'https://images.unsplash.com/photo-1633356122544-f134324a6cee?auto=format&fit=crop&q=80&w=400' },
    { id: 'UI/UX Design', name: 'Design', image: 'https://images.unsplash.com/photo-1561070791-2526d30994b5?auto=format&fit=crop&q=80&w=800' },
    { id: 'Cloud', name: 'Cloud/DevOps', image: 'https://images.unsplash.com/photo-1451187580459-43490279c0fa?auto=format&fit=crop&q=80&w=400' }
  ]
};

async function migrate() {
  for (const [category, domains] of Object.entries(DOMAIN_MAP)) {
    for (const d of domains) {
      console.log(`Processing ${d.name}...`);
      try {
        const response = await fetch(d.image);
        const arrayBuffer = await response.arrayBuffer();
        const buffer = Buffer.from(arrayBuffer);
        
        const path = `migrated/${Date.now()}_${d.name.replace(/\\s+/g, '_')}.jpg`;
        
        const { data: uploadData, error: uploadError } = await supabase.storage
          .from('domains')
          .upload(path, buffer, {
            contentType: 'image/jpeg',
            upsert: true
          });
          
        if (uploadError) {
           console.log(`Failed to upload ${d.name}`, uploadError);
           continue;
        }
        
        const { data: { publicUrl } } = supabase.storage
          .from('domains')
          .getPublicUrl(path);
          
        console.log(`Uploaded! URL: ${publicUrl}`);
        
        // Insert into database
        const { error: insertError } = await supabase.from('domains').insert({
           name: d.name,
           description: d.id, // using original id name as description
           category: category,
           image: publicUrl,
           status: 'APPROVED'
        });
        
        if (insertError) {
           console.log(`Failed to insert ${d.name} to DB`, insertError);
        } else {
           console.log(`Inserted ${d.name} into DB!`);
        }
        
      } catch (err) {
        console.error(`Error processing ${d.name}`, err);
      }
    }
  }
}

migrate().then(() => console.log('Migration complete'));
