from typing import List

from src.internals.database.database import cached_count, cached_query, get_cursor


def get_artist_announcements(
    service: str, artist_id: str, query: str | None = None, reload: bool = False
) -> List[dict]:
    key = f"announcements:{service}:{artist_id}:{hash(query) if query else ""}"
    params: tuple[str, ...]

    if query:
        cur = get_cursor()
        ts_query = cur.mogrify("AND to_tsvector('english', content) @@ websearch_to_tsquery(%s)", (query,)).decode()
    else:
        ts_query = ""

    if service == "fanbox":
        query = f"""
            SELECT *, 'fanbox' AS service
            FROM fanbox_newsletters
            WHERE user_id = %s {ts_query}
            ORDER BY published DESC
        """
        params = (artist_id,)
    else:
        query = f"""
            SELECT *
            FROM introductory_messages
            WHERE service = %s AND user_id = %s {ts_query}
            ORDER BY added DESC
        """
        params = (service, artist_id)

    return cached_query(query, key, params, reload=reload)


def get_announcement_count(service: str, artist_id: str, query: str, reload: bool = True):
    key = f"announcement_count:{service}:{artist_id}:{hash(query)}"

    if query:
        cur = get_cursor()
        ts_query = cur.mogrify("AND to_tsvector('english', content) @@ websearch_to_tsquery(%s)", (query,)).decode()
    else:
        ts_query = ""

    if service == "fanbox":
        query = f"SELECT COUNT(1) FROM fanbox_newsletters WHERE user_id = %s {ts_query}"
        params = (artist_id,)
    else:
        query = f"SELECT COUNT(1) FROM introductory_messages WHERE service = %s AND user_id = %s {ts_query}"
        params = (service, artist_id)

    return cached_count(query, key, params, reload)
