#!/usr/bin/env python3

import argparse
import datetime
import os
import subprocess
from pathlib import Path


def get_filename(title):
    # now = datetime.now().strftime("%Y-%m-%d")
    # d = datetime.datetime.now(tz=datetime.timezone.utc)
    # now = d.strftime("%Y-%m-%d")
    now = datetime.datetime.now(tz=datetime.timezone.utc).strftime("%Y-%m-%d")

    title = title.strip().replace("/", "_").replace("\\", "_").replace(":", "-")
    if title == "":
        return f"{now}.md"

    return f"{now} {title}.md"


def get_editor():
    return os.getenv("VISUAL") or os.getenv("EDITOR") or "vim"


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("title", nargs="?", default="", help="Note title")
    parser.add_argument("-a", "--archive", action="store_true", help="Put note in archive instead")
    args = parser.parse_args()

    filename = get_filename(args.title)

    path = Path.home() / "notes"
    if args.archive:
        path /= "archive"
    path /= filename

    subprocess.run([get_editor(), path], check=True)


if __name__ == "__main__":
    main()
