# pyxfluff 2024 - 2025

import AOS

from time import time
from json import dumps
from httpx import post
from datetime import datetime


def posneg_str(value, value2) -> str:
    return f"{(int(value) > int(value2)) and "+" or ""}{int(value) - int(value2)}"


def daily_report(
    database,
) -> None:
    day = round(time() / 86400) - 1

    yesterday_report = database.get(f"day-{day}", database.REPORTED_VERSIONS)
    state = AOS.globals.state
    st = time()

    print(dumps(database.get(day + 1, database.REPORTED_VERSIONS)))

    if not yesterday_report:
        yesterday_report = {
            "places_len": 0,
            "api_requests": 0,
            "app_downloads": 0,
            "app_votes": 0,
            "ip_blocks": 0
        }

    todays_report = {
        "places_len": len(database.get_all(database.PLACES)),
        "api_requests": state["requests"],
        "app_downloads": state["downloads_today"],
        "app_votes": state["votes_today"],
        "ip_blocks": 0
    }

    if database.get(day, database.REPORTED_VERSIONS) is None:
        database.set(
            day,
            {"internal": {}, "qa": {}, "canary": {}, "beta": {}, "live": {}},
            database.REPORTED_VERSIONS,
        )

    database.set(
        f"day-{round(time() / 86400)}", todays_report, database.REPORTED_VERSIONS
    )

    if database.get(day - 1, database.REPORTED_VERSIONS) is None or database.get(
        day - 1, database.REPORTED_VERSIONS
    ) == {"ERROR_CAUGHT": True}:
        database.set(
            day - 1,
            {"ERROR_CAUGHT": True, "live": {"ERROR_CAUGHT": True}},
            database.REPORTED_VERSIONS,
        )

    post(
        url="https://discord.com/api/webhooks/1299120437984497695/IYGS0-Dsh2Rkw24K5w6XnofIzroIEmp_pndhdGGz0EfsdHvBr5xydt2tnMNJYjz_eRNw",
        json={
            "content": f"Here is your report for **day {day}**",
            "embeds": [
                {
                    "title": "Statistics",
                    "description": f"""
                    ```yaml
- New places: ({len(database.get_all(database.PLACES)) - yesterday_report["places_len"]}) ({posneg_str(len(database.get_all(database.PLACES)), yesterday_report["places_len"])}, {len(database.get_all(database.PLACES))} in total)
- Total API Requests: {state["requests"]} ({posneg_str(state["requests"], yesterday_report["api_requests"])})
- Stable branch usage: {dumps(database.get(day, database.REPORTED_VERSIONS)["live"])} (yesterday: {dumps(database.get(day - 1, database.REPORTED_VERSIONS)["live"])})
- App downloads: {state["downloads_today"]} ({posneg_str(state["downloads_today"], yesterday_report["app_downloads"])})
- App votes: {state["votes_today"]} ({posneg_str(state["votes_today"], yesterday_report["app_votes"])})
- Automated IP blocks: 0 (-0)```""".strip(),
                    "color": 5814783,
                    "footer": {
                        "text": f"Report processed and generated in {time() - st}s • Generated by AOS {AOS.globals.version}"
                    },
                    "timestamp": datetime.now().isoformat(timespec="milliseconds")
                    + "Z",
                },
            ],
            "username": "Administer Reporting",
            "attachments": [],
        },
    )

    state["downloads_today"] = 0
    state["requests"] = 0
    state["votes_today"] = 0
