from typing import TypedDict
from src.internals.database.database import cached_count, cached_query


class TDShare(TypedDict):
    id: int
    name: str
    description: str
    uploader: int
    added: str


def get_share(share_id: int, reload=False) -> TDShare:
    key = f"share:{share_id}"
    params = dict(share_id=share_id)
    query = """
        SELECT
            id,
            name,
            description,
            uploader,
            added
        FROM
            shares
        WHERE
            id = %(share_id)s
    """
    return cached_query(query, key, params, reload=reload, single=True)


def get_shares(offset: int, limit: int = 50, reload=False) -> list[TDShare]:
    key = f"all_shares:{limit}:{offset}:"
    params = dict(offset=offset, limit=limit)
    query = """
        SELECT
            id,
            name,
            description,
            uploader,
            added
        FROM
            shares
        ORDER BY
            id DESC
        OFFSET %(offset)s
        LIMIT %(limit)s
    """
    return cached_query(query, key, params, reload=reload, lock_enabled=True)


def get_all_shares_count(reload: bool = False) -> int:
    return cached_count("SELECT COUNT(*) FROM shares", "all_shares_count", lock_enabled=True)


def get_artist_shares(artist_id, service, reload=False) -> list[TDShare]:
    key = f"artist_shares:{service}:{artist_id}"
    params = dict(artist_id=artist_id, service=service)
    query = """
        SELECT
            shares.id,
            shares.name,
            shares.description,
            shares.uploader,
            shares.added
        FROM
            shares
            INNER JOIN
            lookup_share_relationships AS lsr
            ON
                shares.id = lsr.share_id
        WHERE
            lsr.user_id = %(artist_id)s AND lsr.service = %(service)s
        ORDER BY
            shares.id DESC
    """
    # todo CONSTRAINT lookup_share_relationships_pkey PRIMARY KEY (share_id, service, user_id) should be user_id, service, share_id or we have other index
    return cached_query(query, key, params, reload=reload)


def get_artist_share_count(service: str, artist_id: str, reload=False):
    return 0  # disabled for now while feature is off
    key = f"artist_share_count:{service}:{artist_id}"
    query = """
        SELECT COUNT(*)
        FROM lookup_share_relationships lsr
        WHERE lsr.user_id = %s AND lsr.service = %s
    """
    return cached_count(query, key, (service, artist_id), reload)


class TDShareFile(TypedDict):
    share_id: int
    upload_url: str
    upload_id: str
    file_id: int
    filename: str
    id: int
    hash: str
    mtime: str
    ctime: str
    mime: str
    ext: str
    added: str


def get_files_for_share(share_id: int, reload=False) -> list[TDShareFile]:
    key = f"share_files:{share_id}"
    query = """
        SELECT
            fsr.share_id,
            fsr.upload_url,
            fsr.upload_id,
            fsr.file_id,
            fsr.filename,
            files.id,
            files.hash,
            files.mtime,
            files.ctime,
            files.mime,
            files.ext,
            files.added
        FROM
            file_share_relationships AS fsr
            LEFT JOIN
            files
            ON
                fsr.file_id = files.id
        WHERE
            share_id = %s
        ORDER
            frs.file_id DESC
    """
    return cached_query(query, key, (share_id,), reload=reload)
