# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import argparse
from typing import Optional

from cosmos_rl.utils.logging import logger
from cosmos_rl.policy.config.wfm import CosmosVisionGenConfig
from cosmos_rl.dispatcher.data.packer.base import worker_entry_parser
from cosmos_rl.policy.policy_entry import policy_entry


def main(args: Optional[argparse.Namespace] = None, **kwargs):
    # This means that args are not parsed in dataset entry script
    # So we need to parse the args manually
    if args is None:
        parser = worker_entry_parser()
        try:
            args = parser.parse_args()
        except SystemExit as e:
            logger.error(
                "Error when parsing args. Did you use custom arguments in your script? If so, please check your custom script and pass `args` to this main function."
            )
            raise e

    # FIXME: (lms) refactor this hard-coded check, wfm and llm should be in one-compatible way.
    is_wfm = os.environ.get("COSMOS_IS_WFM", "False").lower() == "true"
    if is_wfm:
        import toml
        from cosmos_rl.policy.worker.wfm_worker import WFMPolicyWorker

        # For wfm vision gen, we have to load  the config from file now.
        config_path = args.config
        assert config_path is not None, "config file is required for wfm vision gen"
        try:
            with open(config_path, "r") as f:
                config_dict = toml.load(f)
            loaded_config = CosmosVisionGenConfig.from_dict(config_dict)
        except Exception as e:
            raise RuntimeError(f"Failed to load config file {config_path}: {e}")

        training_worker = WFMPolicyWorker(loaded_config, **kwargs)
        training_worker.execute()
    else:
        policy_entry(**kwargs)


if __name__ == "__main__":
    main()
