-- Create welcome notification trigger for new users
-- Run this in your Supabase SQL Editor

-- 1. Create function to send welcome notification
CREATE OR REPLACE FUNCTION public.send_welcome_notification()
RETURNS TRIGGER AS $$
BEGIN
    -- Insert welcome notification for new user
    INSERT INTO public.notifications (
        user_id,
        type,
        title,
        message,
        is_read,
        created_at
    ) VALUES (
        NEW.id,
        'welcome',
        '🎉 Welkom bij SuperBoys.nl!',
        'Yo SuperBoy! Welkom bij SuperBoys.nl - de nieuwe superdudes, funky hunks website van de jaren 2000! Spreek met vrienden af, maak events aan of show je beste workouts. Veel plezier! 🚀',
        false,
        NOW()
    );
    
    RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- 2. Create trigger for new user registration
DROP TRIGGER IF EXISTS welcome_notification_trigger ON auth.users;
CREATE TRIGGER welcome_notification_trigger
    AFTER INSERT ON auth.users
    FOR EACH ROW
    EXECUTE FUNCTION public.send_welcome_notification();

-- 3. Test the setup
SELECT 'Welcome notification trigger created successfully!' as status;

-- 4. Check if trigger exists
SELECT 
    trigger_name,
    event_manipulation,
    event_object_table,
    action_statement
FROM information_schema.triggers 
WHERE trigger_name = 'welcome_notification_trigger';
