"""{{ node_description }}

This module implements a standalone MADSci REST node for {{ node_name }}.

Generated by: madsci new node
Template version: {{ template_version }}

This is a minimal node server. For a complete module with interface,
types, and tests, use `madsci new module` instead.
"""

import argparse

from madsci.node_module import ActionHandler, ActionRequest, ActionResult, RestNode


class {{ node_name | pascal_case }}Node(RestNode):
    """{{ node_description }}

    This node provides a REST API for controlling the {{ node_name }} device.
    """

    def startup_handler(self) -> None:
        """Initialize resources on node startup.

        Set up connections, hardware interfaces, or other resources here.
        """

    def shutdown_handler(self) -> None:
        """Clean up resources on node shutdown.

        Close connections, release hardware, or clean up resources here.
        """

    @ActionHandler(
        description="Perform an example action",
    )
    def example_action(self, action: ActionRequest) -> ActionResult:
        """Execute an example action.

        Replace this with your actual device operations.

        Args:
            action: The action request with parameters.

        Returns:
            ActionResult indicating success or failure.
        """
        try:
            message = action.args.get("message", "Hello from {{ node_name }}!")

            return ActionResult.succeeded(
                action=action,
                message=f"Action completed: {message}",
                data={"result": message},
            )
        except Exception as e:
            return ActionResult.failed(
                action=action,
                message=f"Action failed: {e}",
            )


def main() -> None:
    """Entry point for the {{ node_name }} node."""
    parser = argparse.ArgumentParser(description="{{ node_description }}")
    parser.add_argument(
        "--port",
        type=int,
        default={{ port }},
        help="Port to run the node on (default: {{ port }})",
    )
    args = parser.parse_args()

    node = {{ node_name | pascal_case }}Node()
    node.config.node_url = f"http://localhost:{args.port}/"
    node.start_node()


if __name__ == "__main__":
    main()
