from database.database import Database
from typing import Optional, List


def get_user_preferred_category(user_id: int) -> Optional[int]:
    """
    Queries user_preferences database to find users favourite streaming category and returns the category
    """
    with Database() as db:
        category = db.fetchone("""
            SELECT category_id 
            FROM user_preferences 
            WHERE user_id = ? 
            ORDER BY favourability DESC 
            LIMIT 1
        """, (user_id,))
    return category["category_id"] if category else None


def followed_categories_recommendations(user_id: int) -> Optional[List[dict]]:
    """
    Returns top 25 streams given a users category following
    """
    with Database() as db:
        streams = db.fetchall("""
            SELECT u.user_id, title, u.username, num_viewers, category_name 
            FROM streams 
            JOIN users u ON streams.user_id = u.user_id
            JOIN categories ON streams.category_id = categories.category_id
            WHERE categories.category_id IN (SELECT category_id FROM followed_categories WHERE user_id = ?)
            ORDER BY num_viewers DESC
            LIMIT 25;
        """, (user_id,))
    return streams


def get_streams_based_on_category(category_id: int) -> Optional[List[dict]]:
    """
    Queries stream database to get top 25 most viewed streams based on given category
    """
    with Database() as db:
        streams = db.fetchall("""
            SELECT u.user_id, title, username, num_viewers, category_name
            FROM streams 
            JOIN users u ON streams.user_id = u.user_id
            JOIN categories ON streams.category_id = categories.category_id
            WHERE categories.category_id = ? 
            ORDER BY num_viewers DESC 
            LIMIT 25
        """, (category_id,))
    return streams


def get_highest_view_streams(no_streams: int) -> Optional[List[dict]]:
    """
    Return a list of live streams by number of viewers
    """
    with Database() as db:
        data = db.fetchall("""
            SELECT u.user_id, username, title, num_viewers, category_name
            FROM streams 
            JOIN users u ON streams.user_id = u.user_id
            JOIN categories ON streams.category_id = categories.category_id
            ORDER BY num_viewers DESC 
            LIMIT ?;
        """, (no_streams,))
        return data

def get_highest_view_categories(no_categories: int) -> Optional[List[dict]]:
    """
    Returns a list of top 5 most popular categories
    """
    with Database() as db:
        categories = db.fetchall("""
            SELECT categories.category_id, categories.category_name, SUM(streams.num_viewers) AS num_viewers
            FROM streams
            JOIN categories ON streams.category_id = categories.category_id
            GROUP BY categories.category_name
            ORDER BY SUM(streams.num_viewers) DESC
            LIMIT ?;
        """, (no_categories,))
    return categories

def get_user_category_recommendations(user_id: int) -> Optional[List[dict]]:
    """
    Queries user_preferences database to find users top 5 favourite streaming category and returns the category
    """
    with Database() as db:
        categories = db.fetchall("""
            SELECT categories.category_id, categories.category_name
            FROM categories 
            JOIN user_preferences ON categories.category_id = user_preferences.category_id
            WHERE user_id = ? 
            ORDER BY favourability DESC 
            LIMIT 5
        """, (user_id,))
    return categories
