"""{{ experiment_description }}

This is a simple run-once experiment script using ExperimentScript.

Generated by: madsci new experiment --modality script
Template version: {{ template_version }}
"""

from typing import Any

from madsci.common.types.experiment_types import ExperimentDesign
from madsci.experiment_application import ExperimentScript


class {{ experiment_name | pascal_case }}Experiment(ExperimentScript):
    """{{ experiment_description }}

    This experiment uses the ExperimentScript modality, which automatically
    manages the experiment lifecycle (start, end, error handling).

    To run this experiment:
        python {{ experiment_name }}.py

    Or programmatically:
        from {{ experiment_name }} import {{ experiment_name | pascal_case }}Experiment
        result = {{ experiment_name | pascal_case }}Experiment().run()
    """

    # Define the experiment design
    experiment_design = ExperimentDesign(
        experiment_name="{{ experiment_name | replace('_', ' ') | title }}",
        experiment_description="{{ experiment_description }}",
    )

    def run_experiment(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
        """Execute the experiment logic.

        This method is called within the manage_experiment() context, so:
        - The experiment is automatically started before this runs
        - The experiment is automatically ended after this completes
        - Exceptions are logged and the experiment marked as failed

        Available clients (from ExperimentBase):
        - self.workcell_client: Run workflows
        - self.experiment_client: Manage experiment state
        - self.data_client: Store and retrieve data
        - self.resource_client: Track resources
        - self.event_client / self.logger: Log events

        Args:
            *args: Positional arguments from run() or config.run_args.
            **kwargs: Keyword arguments from run() or config.run_kwargs.

        Returns:
            Dictionary containing experiment results.
        """
        # Log experiment start
        self.logger.info(
            "Starting experiment: {{ experiment_name }}",
            experiment_name="{{ experiment_name }}",
        )

        # ==========================================
        # YOUR EXPERIMENT LOGIC HERE
        # ==========================================

        # Example: Run a workflow
        # result = self.workcell_client.run_workflow(
        #     workflow_name="my_workflow",
        #     parameters={"param1": "value1"},
        # )

        # Example: Store data
        # self.data_client.store_data(
        #     name="experiment_output",
        #     data={"key": "value"},
        # )

        # Example: Log progress
        # self.logger.info("Processing step 1...")

        # Placeholder result
        result = {
            "status": "completed",
            "message": "Experiment finished successfully",
            "data": {},
        }

        # ==========================================
        # END EXPERIMENT LOGIC
        # ==========================================

        self.logger.info(
            "Experiment completed",
            result=result,
        )

        return result


def main() -> dict[str, Any]:
    """Main entry point for the experiment script.

    Returns:
        Experiment results dictionary.
    """
    return {{ experiment_name | pascal_case }}Experiment.main()


if __name__ == "__main__":
    result = main()
    print(f"Experiment completed with result: {result}")
