"""
 Copyright 2024 Intel Corporation

 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

      https://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 argparse
from pyrecdp.core.utils import Timer
import os

def language_identify_spark(spark_df, fasttext_model_dir):
    """
    Use fasttext model to identify the language of the docs.
    :param spark_df: the dataframe to be applied language_identify
    :param fasttext_model_dir: the path of fasttext model
    :return dataframe:
    """
    from pyrecdp.primitives.operations import LanguageIdentify
    op = LanguageIdentify(fasttext_model_dir = fasttext_model_dir)
    ret = op.process_spark(spark_df.sparkSession, spark_df)
    return ret

def language_identify(data_dir, data_file_type, fasttext_model_dir, language_identify_output_dir):
    """
    Use fasttext model to identify the language of the docs.
    :param data_dir: the path of dataset folder
    :param data_file_type: support to parquet and jsonl file
    :param fasttext_model_dir: the path of fasttext model
    :param language_identify_output_dir: the output path for language_identify operation
    :return:
    """
    from pyrecdp.LLM import ResumableTextPipeline
    from pyrecdp.primitives.operations import JsonlReader, ParquetReader, LanguageIdentify, PerfileParquetWriter

    if data_file_type == 'jsonl':
        reader = JsonlReader(data_dir)
    elif data_file_type == 'parquet':
        reader = ParquetReader(data_dir)
    else:
        raise NotImplementedError(f"{data_file_type} is not supported in RecDP LLM ResumableTextPipeline yet.")
    
    pipeline = ResumableTextPipeline()
    ops = [
        reader,
        LanguageIdentify(fasttext_model_dir = fasttext_model_dir),
        PerfileParquetWriter(language_identify_output_dir)
    ]
    pipeline.add_operations(ops)
    pipeline.execute()

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--data_dir", dest="data_dir", type=str)
    parser.add_argument("--fasttext_model_dir", dest="fasttext_model_dir", type=str, default="")
    parser.add_argument("--language_identify_output_dir", dest="language_identify_output_dir", type=str, default="")
    args = parser.parse_args()
    data_dir = args.data_dir
    fasttext_model_dir = args.fasttext_model_dir
    language_identify_output_dir = os.path.join(data_dir, "language_identify") \
        if args.language_identify_output_dir == "" else args.language_identify_output_dir

    with Timer(f"Generate language_identify data for {data_dir}"):
        language_identify(data_dir, "jsonl", fasttext_model_dir, language_identify_output_dir)
