# services/motie_service.py

from supabase import create_client
import os

SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)

def is_article_processed(article_id: str) -> bool:
    """id 컬럼 기준으로 중복 여부 확인"""
    try:
        result = supabase.table("motie_id").select("id").eq("id", article_id).execute()
        return len(result.data) > 0
    except Exception as e:
        print(f"[❌] 중복 확인 실패: {e}")
        return False

def mark_article_as_processed(article_id: str):
    """id 컬럼에 article_id 저장"""
    try:
        supabase.table("motie_id").insert({"id": article_id}).execute()
        print(f"[✅] Supabase에 id 저장 완료: {article_id}")
    except Exception as e:
        print(f"[❌] Supabase 저장 실패: {e}")

def get_motie_subscribers():
    try:
        result = supabase.table("motie_subscribers").select("chat_id").execute()
        return result.data
    except Exception as e:
        print(f"[❌] 구독자 조회 실패: {e}")
        return []
