# MIT License

# Copyright (c) 2024 Taratra D. RAHARISON and The HuggingFace Team

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import json
import logging

import typer
from typer import Argument, Option
from typing_extensions import Annotated

from lighteval.cli_args import custom_tasks, load_tasks_multilingual


app = typer.Typer()


@app.command()
def inspect(
    tasks: Annotated[str, Argument(help="Id of tasks or path to a text file with a list of tasks")],
    load_multilingual: Annotated[bool, Option(help="Whether to load multilingual tasks")] = False,
    custom_tasks: custom_tasks.type = custom_tasks.default,
    num_samples: Annotated[int, Option(help="Number of samples to display")] = 10,
    show_config: Annotated[bool, Option(help="Will display the full task config")] = False,
):
    """Inspect a tasks"""
    from dataclasses import asdict
    from pprint import pformat

    from rich import print

    from lighteval.tasks.registry import Registry

    registry = Registry(tasks=tasks, custom_tasks=custom_tasks, load_multilingual=load_multilingual)

    # Loading task
    task_dict = registry.load_tasks()
    for name, task in task_dict.items():
        print("-" * 10, name, "-" * 10)
        if show_config:
            print("-" * 10, "CONFIG")
            task.config.print()
        for ix, sample in enumerate(task.eval_docs()[: int(num_samples)]):
            if ix == 0:
                print("-" * 10, "SAMPLES")
            print(f"-- sample {ix} --")
            print(pformat(asdict(sample), indent=2))


@app.command()
def list(
    load_tasks_multilingual: load_tasks_multilingual.type = load_tasks_multilingual.default,
    custom_tasks: custom_tasks.type = custom_tasks.default,
):
    """List all tasks"""
    from lighteval.tasks.registry import Registry

    registry = Registry(custom_tasks=custom_tasks, load_multilingual=load_tasks_multilingual)
    registry.print_all_tasks()


@app.command()
def create(template: str, task_name: str, dataset_name: str):
    """Create a new task"""
    logger = logging.getLogger(__name__)

    logger.info(f"Creating task for dataset {dataset_name}")

    with open(template, "r") as f:
        content = f.read()

    content = content.replace("HF_TASK_NAME", task_name)
    content = content.replace("HF_DATASET_NAME", dataset_name)

    with open(f"custom_{task_name}_task.py", "w+") as f:
        f.write(content)

    logger.info(f"Task created in custom_{task_name}_task.py")


@app.command()
def dump(
    load_tasks_multilingual: load_tasks_multilingual.type = load_tasks_multilingual.default,
    custom_tasks: custom_tasks.type = custom_tasks.default,
):
    """Dump all task names, metadata, and docstrings as JSON"""
    from lighteval.tasks.registry import Registry

    registry = Registry(custom_tasks=custom_tasks, load_multilingual=load_tasks_multilingual)
    modules_data = registry.get_tasks_dump()

    print(json.dumps(modules_data, indent=2, default=str))
