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

const SUPABASE_URL = 'https://bjgixlugjzsccazdfmph.supabase.co'
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJqZ2l4bHVnanpzY2NhemRmbXBoIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzIwNjQyMTAsImV4cCI6MjA4NzY0MDIxMH0.untLQoPi4yUr3cPnxo23wYSlg6xnNK0daKu9UHmFTp8'
const sb = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

async function test() {
    console.log("Fetching a route and a bus stop to link...");
    const { data: route } = await sb.from('routes').select('id').limit(1);
    const { data: stop } = await sb.from('bus_stops').select('id').limit(1);

    if (route?.[0] && stop?.[0]) {
        console.log("Upserting route_stop link...");
        const res = await sb.from('route_stops').upsert([{
            route_id: route[0].id,
            stop_id: stop[0].id,
            stop_order: 1
        }]).select()
        console.log("Insert response:", JSON.stringify(res, null, 2))

        console.log("Testing join...")
        const { data, error } = await sb.from('route_stops').select('*, bus_stops(*)')
        console.log('Join Data:', JSON.stringify(data?.[0], null, 2))
        console.log('Join Error:', error)
    } else {
        console.log("No existing route or stop to link.")
    }
}
test()
