# Copyright (c) 2025, NVIDIA CORPORATION.  All rights reserved.
#
# 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.
from typing import Literal

from nemo_curator.datasets.doc_dataset import DocumentDataset
from nemo_curator.modules.base import BaseModule


class ToBackend(BaseModule):
    """
    A module for moving dataframes between backends.
    """

    def __init__(self, backend: Literal["pandas", "cudf"]) -> None:
        """
        Constructs a ToBackend module

        Args:
            backend (str): The backend to transfer the dataset to. Can be "pandas" or "cudf"
        """
        super().__init__(input_backend="any")
        if backend not in BaseModule.SUPPORTED_BACKENDS:
            raise ValueError(
                f"Backend must be either 'pandas' or 'cudf', but got {backend}"
            )
        self.backend = backend

    def call(self, dataset: DocumentDataset) -> DocumentDataset:
        return DocumentDataset(dataset.df.to_backend(self.backend))
