#!/usr/bin/env python
"""Auto-generated entrypoint script for Ray Jobs API."""
import importlib
import json
import os
import sys
import logging

def main():
    # Configure logging to ensure ConsoleLogger output is visible
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        force=True,
    )

    # Add working directory to path
    cwd = os.getcwd()
    if cwd not in sys.path:
        sys.path.insert(0, cwd)

    # Load params from environment or file
    params_json = os.environ.get("SYNAPSE_ACTION_PARAMS")
    if not params_json:
        params_file = os.environ.get("SYNAPSE_ACTION_PARAMS_FILE")
        if params_file and os.path.exists(params_file):
            with open(params_file) as f:
                params_json = f.read()

    if not params_json:
        raise ValueError("No params provided via SYNAPSE_ACTION_PARAMS or SYNAPSE_ACTION_PARAMS_FILE")

    params = json.loads(params_json)

    # Get entrypoint
    entrypoint = os.environ.get("SYNAPSE_ACTION_ENTRYPOINT")
    if not entrypoint:
        raise ValueError("SYNAPSE_ACTION_ENTRYPOINT not set")

    # Import action class
    module_path, class_name = entrypoint.rsplit(".", 1)
    module = importlib.import_module(module_path)
    action_cls = getattr(module, class_name)

    # Create context
    from synapse_sdk.loggers import ConsoleLogger
    from synapse_sdk.plugins.context import RuntimeContext
    from synapse_sdk.plugins.context.env import PluginEnvironment
    from synapse_sdk.utils.auth import create_backend_client

    client = create_backend_client()
    logger = ConsoleLogger()
    ctx = RuntimeContext(
        logger=logger,
        env=PluginEnvironment.from_environ(),
        job_id=os.environ.get("RAY_JOB_ID"),
        client=client,
    )

    # Dispatch action (handles serve deployments, param validation, etc.)
    result = action_cls.dispatch(params, ctx)

    logger.finish()

    # Output result as JSON for capture
    print("__SYNAPSE_RESULT_START__")
    print(json.dumps(result if isinstance(result, dict) else {"result": result}))
    print("__SYNAPSE_RESULT_END__")

    return result

if __name__ == "__main__":
    main()
