"""Keyword watches router — CRUD for keyword watch rules and events."""

from fastapi import APIRouter, Depends, HTTPException
from typing import List

from modules.auth.supabase_bearer import SupabaseBearer
from modules.transcription.models import (
    KeywordWatchCreate,
    KeywordWatchResponse,
    KeywordEventCreate,
    KeywordEventResponse,
)

router = APIRouter()


def get_supabase_client():
    """Get Supabase service role client."""
    from modules.database.supabase.utils.client import SupabaseServiceRoleClient
    return SupabaseServiceRoleClient()


def get_user_id(credentials=Depends(SupabaseBearer())) -> str:
    """Extract user_id from Supabase JWT token."""
    return credentials.get("sub", credentials.get("user_id", ""))


@router.get("/keywords", response_model=List[KeywordWatchResponse])
async def list_keyword_watches(
    user_id: str = Depends(get_user_id),
):
    """List user's keyword watches."""
    supabase = get_supabase_client()
    
    result = supabase.supabase.table("keyword_watches").select("*").eq("user_id", user_id).execute()
    
    return result.data


@router.post("/keywords", response_model=KeywordWatchResponse)
async def create_keyword_watch(
    watch: KeywordWatchCreate,
    user_id: str = Depends(get_user_id),
):
    """Create a keyword watch."""
    supabase = get_supabase_client()
    
    data = {
        "user_id": user_id,
        "keyword": watch.keyword.lower(),  # Store lowercase for case-insensitive matching
        "match_type": watch.match_type,
        "action": watch.action,
    }
    
    result = supabase.supabase.table("keyword_watches").insert(data).execute()
    
    if not result.data:
        raise HTTPException(status_code=500, detail="Failed to create keyword watch")
    
    return result.data[0]


@router.delete("/keywords/{watch_id}")
async def delete_keyword_watch(
    watch_id: str,
    user_id: str = Depends(get_user_id),
):
    """Delete a keyword watch."""
    supabase = get_supabase_client()
    
    # Verify ownership
    existing = supabase.supabase.table("keyword_watches").select("*").eq("id", watch_id).eq("user_id", user_id).execute()
    
    if not existing.data:
        raise HTTPException(status_code=404, detail="Keyword watch not found")
    
    supabase.supabase.table("keyword_watches").delete().eq("id", watch_id).execute()
    
    return {"message": "Keyword watch deleted"}


@router.post("/keywords/events")
async def log_keyword_event(
    event: KeywordEventCreate,
    user_id: str = Depends(get_user_id),
):
    """Log a keyword event (triggered when a watch matches)."""
    supabase = get_supabase_client()
    
    # Verify session ownership
    session_check = supabase.supabase.table("transcription_sessions").select("id").eq("id", event.session_id).eq("user_id", user_id).execute()
    
    if not session_check.data:
        raise HTTPException(status_code=404, detail="Session not found")
    
    data = event.model_dump()
    result = supabase.supabase.table("keyword_events").insert(data).execute()
    
    if not result.data:
        raise HTTPException(status_code=500, detail="Failed to log keyword event")
    
    return result.data[0]
