-- Add indexes for performance optimization
-- These indexes significantly improve dashboard and API query performance

-- Tenders table indexes (most critical)
CREATE INDEX IF NOT EXISTS idx_tenders_company_id ON public.tenders(company_id);
CREATE INDEX IF NOT EXISTS idx_tenders_status ON public.tenders(status);
CREATE INDEX IF NOT EXISTS idx_tenders_company_status ON public.tenders(company_id, status);
CREATE INDEX IF NOT EXISTS idx_tenders_deadline ON public.tenders(deadline);
CREATE INDEX IF NOT EXISTS idx_tenders_company_deadline ON public.tenders(company_id, deadline);
CREATE INDEX IF NOT EXISTS idx_tenders_created_at ON public.tenders(created_at DESC);

-- Activity events index
CREATE INDEX IF NOT EXISTS idx_activity_events_company_id ON public.activity_events(company_id);
CREATE INDEX IF NOT EXISTS idx_activity_events_created_at ON public.activity_events(created_at DESC);

-- Compliance items index
CREATE INDEX IF NOT EXISTS idx_compliance_items_tender_id ON public.compliance_items(tender_id);

-- Documents index
CREATE INDEX IF NOT EXISTS idx_documents_tender_id ON public.documents(tender_id);

-- Subscription status index
CREATE INDEX IF NOT EXISTS idx_subscriptions_company_status ON public.subscriptions(company_id, status);

-- Notification settings index
CREATE INDEX IF NOT EXISTS idx_notification_settings_user_id ON public.notification_settings(user_id);

-- Chat messages index
CREATE INDEX IF NOT EXISTS idx_chat_messages_tender_id ON public.chat_messages(tender_id);
CREATE INDEX IF NOT EXISTS idx_chat_messages_created_at ON public.chat_messages(created_at DESC);

-- Audit log index
CREATE INDEX IF NOT EXISTS idx_audit_log_company_id ON public.audit_log(company_id);
CREATE INDEX IF NOT EXISTS idx_audit_log_created_at ON public.audit_log(created_at DESC);
