"""Build the (LiF)2 cluster with a water molecule above its exposed Li site.

No calculator is used. The script writes two extxyz inputs for adsorption-run.py:

    adsorption-start.extxyz    Li F Li F (host, indices 0-3) and O H H (water, indices 4-6)
    water-gas-start.extxyz     isolated water from the builder, the energy reference

    python adsorption-build.py --output ../models
"""

import argparse
from pathlib import Path

from atomli import Atoms
from atomli.build import molecule
from atomli.io import write

HOST_COLOR = "#a8aeb7"


def build(output):
    output.mkdir(parents=True, exist_ok=True)
    # A finite molecular model: no periodic surface, no coverage.
    start = Atoms("LiFLiFOH2", positions=[
        [0, 0, 0], [1.7, 0, 0], [1.7, 1.7, 0], [0, 1.7, 0],
        [-.3, -.25, 2.15], [-.3, .513, 2.746], [-.3, -1.013, 2.746],
    ])
    write(output / "adsorption-start.extxyz", start)
    write(output / "water-gas-start.extxyz", molecule("H2O"))
    return start


def write_figure(start, spec):
    from atomli.visualize import Viewer

    viewer = Viewer([start], height=420, look={
        "background": "white", "axes": True, "legend": True, "labels": True,
        "atom_colors": [HOST_COLOR] * 4 + [None] * 3,
        "color_legend": {HOST_COLOR: "host (LiF)₂ cluster"}})
    viewer.write_spec(spec, structure_path=spec.with_name("adsorption-start.extxyz"))


def main():
    here = Path(__file__).resolve().parent
    parser = argparse.ArgumentParser()
    parser.add_argument("--output", type=Path, default=here.parent / "models")
    parser.add_argument("--spec", type=Path, help="also write the viewer spec for the documentation page")
    args = parser.parse_args()
    start = build(args.output)
    if args.spec:
        write_figure(start, args.spec)


if __name__ == "__main__":
    main()
