"""
Exporting results to VTK / ParaView
===================================

Once you've got a ``ModalResult`` / ``StaticResult``, sending it
downstream to ParaView / VisIt / any VTK-aware tool is a one-liner.
:mod:`femorph_solver.io` wraps the pyvista writers so you don't have to
hand-roll the point-data scatter.
"""

from __future__ import annotations

import tempfile
from pathlib import Path

import numpy as np
import pyvista as pv

import femorph_solver
from femorph_solver import ELEMENTS

# %%
# Build + solve a tiny plate
# --------------------------
grid = pv.StructuredGrid(
    *np.meshgrid(
        np.linspace(0.0, 1.0, 11),
        np.linspace(0.0, 0.3, 5),
        np.linspace(0.0, 0.01, 3),
        indexing="ij",
    )
).cast_to_unstructured_grid()

m = femorph_solver.Model.from_grid(grid)
m.assign(ELEMENTS.HEX8, material={"EX": 2.0e11, "PRXY": 0.30, "DENS": 7850.0})

pts = np.asarray(grid.points)
node_nums = np.asarray(grid.point_data["ansys_node_num"])
m.fix(nodes=node_nums[pts[:, 0] < 1e-9].tolist(), dof="ALL")

res = m.solve_modal(n_modes=4)

# %%
# Write every mode as a ``.vtu`` snapshot
# ---------------------------------------
out_dir = Path(tempfile.mkdtemp(prefix="femorph_solver_docs_"))
vtu_path = out_dir / "plate_modes.vtu"
femorph_solver.io.write_modal_vtk(vtu_path, m, res)
print(f"wrote modal snapshot → {vtu_path}  ({vtu_path.stat().st_size:,} bytes)")

# %%
# Inspect the file
# ----------------
back = pv.read(str(vtu_path))
vector_arrays = [
    name for name in back.point_data if name.startswith("mode_") and name.endswith("_disp")
]
print(f"file carries {len(vector_arrays)} mode-shape vector fields:")
for name in vector_arrays:
    print(f"  {name}: shape = {back.point_data[name].shape}")

# %%
# For a single static field, the canonical pattern is the same
# ------------------------------------------------------------
# :func:`femorph_solver.io.static_result_to_grid` is the static analogue
# of :func:`modal_result_to_grid`.  Example:
#
# .. code-block:: python
#
#     grid_static = femorph_solver.io.static_result_to_grid(m, static_res)
#     grid_static.save("static.vtu")
