"""Create an ethanol document, SVG and vibe-qc script using the Python SDK.

Run with the environment that has vibe-sketch installed:
    python ethanol.py ethanol-sdk
"""

from __future__ import annotations

import argparse
from pathlib import Path

from vibesketch.document import JobSpec, save, write_output
from vibesketch.document.embed import embed
from vibesketch.document.formats import import_into
from vibesketch.inputs import write_input
from vibesketch.render import render_svg


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("directory", type=Path, help="new directory for the example outputs")
    output = parser.parse_args().directory
    output.mkdir(parents=True, exist_ok=False)

    document = import_into(None, "CCO", "smiles").document
    document, report = embed(document)
    if report.failed:
        raise SystemExit(f"Cannot generate 3D: {report.failed}")
    for warning in report.warnings:
        print(warning)
    species = document.species[0]
    bundle = write_input(
        document,
        species=species.id,
        program="vibeqc",
        spec=JobSpec(task="sp", method="B3LYP", basis="def2-SVP"),
    )
    save(document, output / "ethanol.vsk")
    write_output(output / "ethanol.svg", render_svg(document))
    for file in bundle.files:
        write_output(output / file.name, file.content.encode("utf-8"))
        print(f"Wrote {file.name}")
    print("Saved ethanol.vsk and ethanol.svg. No calculation was submitted.")


if __name__ == "__main__":
    main()
