-- ============================================
-- Database Performance Optimization: Indexing
-- ============================================
-- Addresses "unindexed_foreign_keys" and "unused_index" linter warnings
-- ============================================

-- ============================================
-- 1. Missing Indexes for Foreign Keys
-- ============================================

-- bottles
CREATE INDEX IF NOT EXISTS idx_bottles_user_id ON public.bottles(user_id);

-- buddies
CREATE INDEX IF NOT EXISTS idx_buddies_user_id ON public.buddies(user_id);
CREATE INDEX IF NOT EXISTS idx_buddies_buddy_profile_id ON public.buddies(buddy_profile_id);

-- credit_transactions
CREATE INDEX IF NOT EXISTS idx_credit_transactions_admin_id ON public.credit_transactions(admin_id);

-- session_participants
CREATE INDEX IF NOT EXISTS idx_session_participants_session_id ON public.session_participants(session_id);
CREATE INDEX IF NOT EXISTS idx_session_participants_buddy_id ON public.session_participants(buddy_id);
CREATE INDEX IF NOT EXISTS idx_session_participants_user_id ON public.session_participants(user_id);

-- tags
CREATE INDEX IF NOT EXISTS idx_tags_created_by ON public.tags(created_by);

-- tasting_buddies
CREATE INDEX IF NOT EXISTS idx_tasting_buddies_tasting_id ON public.tasting_buddies(tasting_id);
CREATE INDEX IF NOT EXISTS idx_tasting_buddies_buddy_id ON public.tasting_buddies(buddy_id);
CREATE INDEX IF NOT EXISTS idx_tasting_buddies_user_id ON public.tasting_buddies(user_id);

-- tasting_sessions
CREATE INDEX IF NOT EXISTS idx_tasting_sessions_user_id ON public.tasting_sessions(user_id);

-- tasting_tags (aroma tags)
CREATE INDEX IF NOT EXISTS idx_tasting_tags_tasting_id ON public.tasting_tags(tasting_id);
CREATE INDEX IF NOT EXISTS idx_tasting_tags_tag_id ON public.tasting_tags(tag_id);
CREATE INDEX IF NOT EXISTS idx_tasting_tags_user_id ON public.tasting_tags(user_id);

-- tastings
CREATE INDEX IF NOT EXISTS idx_tastings_bottle_id ON public.tastings(bottle_id);
CREATE INDEX IF NOT EXISTS idx_tastings_user_id ON public.tastings(user_id);


-- ============================================
-- 2. Cleanup of Unused Indexes
-- ============================================
-- These were flagged by the linter as "never used". 
-- Use with caution, but removal helps with insert/update performance.

-- DROP INDEX IF EXISTS idx_global_products_search_vector;
-- DROP INDEX IF EXISTS idx_buddy_invites_code;
-- DROP INDEX IF EXISTS idx_buddy_invites_expires_at;
-- DROP INDEX IF EXISTS idx_split_participants_status;
-- DROP INDEX IF EXISTS idx_tastings_tasted_at;
-- DROP INDEX IF EXISTS idx_credit_transactions_created_at;
-- DROP INDEX IF EXISTS idx_credit_transactions_type;
-- DROP INDEX IF EXISTS idx_subscription_plans_active;
-- DROP INDEX IF EXISTS idx_subscription_plans_sort_order;
-- DROP INDEX IF EXISTS idx_user_subscriptions_plan_id;
