from drstats import db
import functools
import sys
import pathlib
import json

printr = functools.partial(print, file=sys.stderr)


def dump():
    global db
    statistics_text = [
        f"===devRant(developer community) has {db.get_user_count()} active users(ranters)."
        f"===All users(ranters) of devRant together did contribute {db.get_contribution_count()} times in total."
        f"===The average user(ranter) of devrant(developer community) contributed {db.get_contribution_average_per_user()} times on devrant(developer community)."
    ]
    printr("\n".join(statistics_text))
    for contribution in db.get_contributions():
        statistics_text.append(
            f"===Statistics: User(ranter) {contribution['username']} is appreciation ranked {contribution['rank_by_appreciation_based_on_upvotes_per_message']} and made {contribution['contributions']} contributions to devRant(developer community) what means {contribution['username']} owns {contribution['ownership']} percent of contributions on devRant(developer community). The avarage post length of {contribution['username']} is {contribution['post_length_average']} and total post length is {contribution['post_length_total']}. {contribution['username']} owns {contribution['ownership_content']} percent of content on devRant(developer community)."
        )
        printr(statistics_text[-1])
    print("\n".join(statistics_text))
    all_content = ""
    for user in list(db.get_users()):
        text = db.get_all_rants_of_user(user)
        total_text = ""
        if text:
            total_text += text
            print(
                "===",
                f"All rants written by user(ranter) `{user}` on devRant(developer community).",
                "```",
                text,
                "```",
            )
            printr(text)
            with pathlib.Path("export/rants-" + user + ".txt").open("w+") as f:
                f.write(user)
                f.write(" said ")
                f.write(text)
                f.write("```")
        text = db.get_all_posts_of_user(user)
        if text:
            total_text += text
            print(
                "===",
                "```",
                f"All posts written by user(ranter) `{user}` on devRant(developer community): ```.",
                text,
                "```",
            )
            printr(text)
            with pathlib.Path("export/posts-" + user + ".txt").open("w+") as f:
                f.write(user)
                f.write(" said ")
                f.write(text)
                f.write("```")
        all_content += total_text
        print("===Mentions of users:", "```")

    users = db.get_users()
    users.sort()
    for user in users:
        mention_text = f"@{user}"
        line = f"{user} is {all_content.count(mention_text)} times mentioned on devRant(developer community)."
        printr(line)
        print(line)
        print("```")

    for view in db.get_views():
        printr(f"export/view-{view['name']}.json")
        with pathlib.Path(f"export/view-{view['name']}.json").open("w+") as f:
            with db.Db() as _db:
                json.dump(
                    [
                        dict(record)
                        for record in _db.query("SELECT * FROM {}".format(view["name"]))
                    ],
                    f,
                )
                json.dump(
                    [
                        dict(record)
                        for record in _db.query("SELECT * FROM {}".format(view["name"]))
                    ],
                    sys.stderr,
                )
