#!/usr/bin/env python3

import argparse
import random
from unidecode import unidecode

import mo.db as db
import mo.util
from mo.util import die

parser = argparse.ArgumentParser(description='Anonymizuje všechny účastníky')
parser.add_argument('--really', default=False, action='store_true', help='potvrdí, že to opravdu chceme udělat')
parser.add_argument('--seed', type=str, default='seedme', help='random seed')
parser.add_argument('-n', '--dry-run', default=False, action='store_true', help='vyzkouší anonymizaci nanečisto (necommitne)')

args = parser.parse_args()
mo.util.init_standalone()

if not args.really:
    die("Nejsem si jistý, jestli to chci udělat. Řekni mi, že opravdu.")


def load(name):
    with open(f'etc/names/{name}') as f:
        return [line.strip() for line in f]


first_names = [load('first-f'), load('first-m')]
last_names = [load('last-f'), load('last-m')]
used_names = set()
rng = random.Random(args.seed)


def norm(name):
    return unidecode(name).lower()


def gen_name(orig_last):
    if orig_last.endswith('á'):
        gender = 0
    else:
        gender = 1

    while True:
        f = rng.choice(first_names[gender])
        l = rng.choice(last_names[gender])
        ff, ll = norm(f), norm(l)
        stripped = f'{ff} {ll}'
        if stripped not in used_names:
            used_names.add(stripped)
            return f, l, ff, ll


sess = db.get_session()

users = sess.query(db.User).all()
cnt_anoned = 0
cnt_orgs = 0
cnt_already = 0

for u in users:
    if u.is_org or u.is_admin:
        cnt_orgs += 1
    elif u.email.endswith('@test'):
        cnt_already += 1
    else:
        first, last, first_stripped, last_stripped = gen_name(u.last_name)
        email = f'{first_stripped}.{last_stripped}@test'
        print(f'{u.first_name} {u.last_name} -> {first} {last} <{email}>')
        u.first_name = first
        u.last_name = last
        u.email = email
        cnt_anoned += 1

if not args.dry_run:
    sess.commit()
else:
    sess.rollback()

print(f'Hotovo: {cnt_anoned} anonymizováno, {cnt_already} už bylo, ponecháno {cnt_orgs} organizátorů')