from flask import jsonify, make_response

from src.internals.database.database import query_db
from src.lib.announcements import get_artist_announcements
from src.lib.artist import get_artist, get_fancards_by_artist, get_linked_creators
from src.pages.api.v1 import v1api_bp


@v1api_bp.get("/creators")
def all_creators():
    """this view must be cached at nginx/cdn level"""
    query = """
        SELECT
            l.id,
            l.name,
            l.service,
            EXTRACT(epoch from l.indexed)::int AS indexed,
            EXTRACT(epoch from l.updated)::int AS updated,
            COALESCE(aaf.favorited, 0) AS favorited
        FROM lookup l
        LEFT JOIN (
            SELECT
                artist_id,
                service,
                COUNT(*) AS favorited
            FROM account_artist_favorite
            GROUP BY artist_id, service
        ) aaf ON
            l.id = aaf.artist_id
            AND l.service = aaf.service
        WHERE
            l.id NOT IN (SELECT id from dnp)
            AND (l.id, l.service) NOT IN (
               SELECT id, service FROM lookup l WHERE NOT EXISTS (SELECT FROM posts WHERE service = l.service AND "user" = l.id) AND service != 'discord');
    """
    return make_response(jsonify(query_db(query)), 200)


@v1api_bp.get("/<service>/user/<creator_id>/profile")
def get_creator(service, creator_id):
    artist = get_artist(service, creator_id)
    if not artist:
        response = make_response(jsonify({"error": "Creator not found."}), 404)
        response.headers["Cache-Control"] = "s-maxage=60"
        return response

    response = make_response(jsonify(artist), 200)
    response.headers["Cache-Control"] = "s-maxage=60"
    return response


@v1api_bp.get("/<service>/user/<creator_id>/announcements")
def get_announcements(service, creator_id):
    artist = get_artist(service, creator_id)
    if not artist:
        response = make_response(jsonify({"error": "Artist not found."}), 404)
        response.headers["Cache-Control"] = "s-maxage=600"
        return response
    announcements = get_artist_announcements(service, creator_id, reload=True)
    response = make_response(jsonify(announcements), 200)
    response.headers["Cache-Control"] = "s-maxage=600"
    return response


@v1api_bp.get("/<service>/user/<creator_id>/fancards")
def get_fancards(service, creator_id):
    artist = get_artist(service, creator_id)
    if not artist:
        response = make_response(jsonify({"error": "Artist not found."}), 404)
        response.headers["Cache-Control"] = "s-maxage=600"
        return response
    fancards = get_fancards_by_artist(creator_id, reload=True)
    response = make_response(jsonify(fancards), 200)
    response.headers["Cache-Control"] = "s-maxage=600"
    return response


@v1api_bp.get("/<service>/user/<creator_id>/links")
def get_linked_accounts(service, creator_id):
    links = get_linked_creators(service, creator_id)

    response = make_response(jsonify(links), 200)
    response.headers["Cache-Control"] = "s-maxage=600"
    return response
