-- 930_add_missing_foreign_keys.sql
-- Add missing foreign key constraints for data integrity
-- BE-DB-002: Add foreign key constraints where missing

-- Add FK constraint for tracks.user_id (legacy field that maps to creator_id)
-- Note: creator_id already has FK, but user_id is still used in some queries
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_schema = 'public' 
        AND table_name = 'tracks' 
        AND column_name = 'user_id'
    ) THEN
        -- Check if constraint doesn't already exist
        IF NOT EXISTS (
            SELECT 1 FROM information_schema.table_constraints 
            WHERE constraint_schema = 'public' 
            AND table_name = 'tracks' 
            AND constraint_name = 'fk_tracks_user_id'
        ) THEN
            ALTER TABLE public.tracks 
            ADD CONSTRAINT fk_tracks_user_id 
            FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE SET NULL;
        END IF;
    END IF;
END $$;

-- Add FK constraint for rooms.owner_id (legacy field that maps to creator_id)
-- Note: creator_id already has FK, but owner_id is still used in some queries
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_schema = 'public' 
        AND table_name = 'rooms' 
        AND column_name = 'owner_id'
    ) THEN
        IF NOT EXISTS (
            SELECT 1 FROM information_schema.table_constraints 
            WHERE constraint_schema = 'public' 
            AND table_name = 'rooms' 
            AND constraint_name = 'fk_rooms_owner_id'
        ) THEN
            ALTER TABLE public.rooms 
            ADD CONSTRAINT fk_rooms_owner_id 
            FOREIGN KEY (owner_id) REFERENCES public.users(id) ON DELETE SET NULL;
        END IF;
    END IF;
END $$;

-- Add FK constraint for messages.user_id (legacy field that maps to sender_id)
-- Note: sender_id already has FK, but user_id is still used in some queries
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_schema = 'public' 
        AND table_name = 'messages' 
        AND column_name = 'user_id'
    ) THEN
        IF NOT EXISTS (
            SELECT 1 FROM information_schema.table_constraints 
            WHERE constraint_schema = 'public' 
            AND table_name = 'messages' 
            AND constraint_name = 'fk_messages_user_id'
        ) THEN
            ALTER TABLE public.messages 
            ADD CONSTRAINT fk_messages_user_id 
            FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE SET NULL;
        END IF;
    END IF;
END $$;

-- Add FK constraint for messages.parent_id (legacy field that maps to reply_to_id)
-- Note: reply_to_id already has FK, but parent_id is still used in some queries
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns 
        WHERE table_schema = 'public' 
        AND table_name = 'messages' 
        AND column_name = 'parent_id'
    ) THEN
        IF NOT EXISTS (
            SELECT 1 FROM information_schema.table_constraints 
            WHERE constraint_schema = 'public' 
            AND table_name = 'messages' 
            AND constraint_name = 'fk_messages_parent_id'
        ) THEN
            ALTER TABLE public.messages 
            ADD CONSTRAINT fk_messages_parent_id 
            FOREIGN KEY (parent_id) REFERENCES public.messages(id) ON DELETE SET NULL;
        END IF;
    END IF;
END $$;

-- Add FK constraint for audit_logs.user_id
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.tables 
        WHERE table_schema = 'public' 
        AND table_name = 'audit_logs'
    ) THEN
        IF EXISTS (
            SELECT 1 FROM information_schema.columns 
            WHERE table_schema = 'public' 
            AND table_name = 'audit_logs' 
            AND column_name = 'user_id'
        ) THEN
            IF NOT EXISTS (
                SELECT 1 FROM information_schema.table_constraints 
                WHERE constraint_schema = 'public' 
                AND table_name = 'audit_logs' 
                AND constraint_name = 'fk_audit_logs_user_id'
            ) THEN
                ALTER TABLE public.audit_logs 
                ADD CONSTRAINT fk_audit_logs_user_id 
                FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE SET NULL;
            END IF;
        END IF;
    END IF;
END $$;

