import json
import logging
from datetime import datetime
from pathlib import Path

from cryptography.fernet import Fernet

from nasa_archive import archive_files
from nasa_export import load_data
from nasa_upload import submit_changes

logging.basicConfig(filename="logs/nasa.log", level=logging.INFO)
logger = logging.getLogger("nasa")


def get_config():
    fernet_key = b"YBckeKYt-8g7LFvpG7XqAAcEbsYESnI-yl8by9rjeQQ="
    fernet = Fernet(fernet_key)

    if Path("config/nasa_config.json").exists():
        with open("config/nasa_config.json", "r") as f:
            config = json.load(f)
        with open("config/nasa_config.crypt", "wb") as f:
            f.write(fernet.encrypt(json.dumps(config).encode()))
    else:
        with open("config/nasa_config.crypt", "rb") as f:
            config = json.loads(fernet.decrypt(f.read()).decode())
        if Path("config/nasa_config.bak").exists():
            Path("config/nasa_config.bak").unlink()
            with open("config/nasa_config.json", "w") as f:
                json.dump(config, f, indent=2)
    return config


def export_all_periods(config) -> None:
    dt = datetime.now()
    prev = str(dt.year - 1)
    periods = [f"{prev}{x:02}" for x in range(1, 13)] + [f"{dt.year}{x:02}" for x in range(1, dt.month)]

    for period in periods:
        print("  Periode: " + period)
        payload = load_data(config, "database", period)
        json.dump(
            payload,
            open(f"{config['export_dir']}/temp/NASA_{config['client_id']}_{period}_{config['timestamp']}.json", "w"),
            indent=2,
        )


def main():
    config = get_config()
    dt = datetime.now()
    config["timestamp"] = dt.strftime("%Y%m%d_%H%M%S")
    print("Start: " + dt.strftime("%d.%m.%Y %H:%M:%S"))
    config["export_dir"] = str(Path.cwd() / "export" / "NASA")
    print("Export")
    export_all_periods(config)
    print("Archivierung")
    archive_files(config["export_dir"])
    print("Upload")
    submit_changes(config)


if __name__ == "__main__":
    main()
