File size: 2,408 Bytes
ef78361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Supabase client for dashboard - direct queries for LLM verification data.

Domain queries are in separate modules:
- supabase_sentiment.py: Overview, LLM verification, polarity
- supabase_research.py: Topic clusters, cross-model analysis
- supabase_action_items.py: Action items CRUD, reports, date range
"""

import os
from functools import lru_cache
from pathlib import Path

from supabase import create_client, Client
from dotenv import load_dotenv

# Load environment variables (project root)
_project_root = Path(__file__).parent.parent.parent.parent
for _env_name in (".env.dev", ".env.prod"):
    _env_path = _project_root / _env_name
    if _env_path.exists():
        load_dotenv(_env_path, override=True)
        break


@lru_cache()
def get_supabase_client() -> Client:
    """Create Supabase client for unified database.

    Uses service_role key (bypasses RLS) because the dashboard is an
    internal admin tool that needs cross-campaign analytics.
    DO NOT use this client in user-facing API routes.
    """
    url = os.environ.get("SUPABASE_URL", "")
    key = os.environ.get("SUPABASE_SERVICE_KEY", "")
    if not url or not key:
        raise ValueError("Missing SUPABASE_URL or SUPABASE_SERVICE_KEY")
    return create_client(url, key)


# ============================================================================
# Re-exports for backward compatibility
# All consumers import from core.supabase_client — these re-exports
# ensure existing imports continue to work unchanged.
# ============================================================================

# Sentiment / Overview / LLM Verification
from core.supabase_sentiment import (  # noqa: E402, F401
    get_campaign_overview,
    get_llm_verification_stats,
    get_false_positives,
    get_true_negatives,
    get_llm_verified_for_export,
    get_sentiment_data_for_export,
    get_answers_by_polarity,
    get_polarity_stats,
)

# Research / Topics
from core.supabase_research import (  # noqa: E402, F401
    get_topic_clusters,
    get_topic_map_snapshot,
    get_cross_model_analysis,
    get_gap_scores,
    find_cross_model_pair,
)

# Action Items / Reports
from core.supabase_action_items import (  # noqa: E402, F401
    get_action_items,
    get_action_item_stats,
    update_action_item_status,
    delete_action_item,
    save_action_items_batch,
    get_campaign_date_range,
    get_report_history_count,
)