#  Copyright 2021-present, the Recognai S.L. team.
#
#  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 logging
import warnings
from typing import Any, Optional

from ._flair import SequenceTagger, flair_monitor
from ._spacy import Language, ner_monitor
from ._transformers import Pipeline, huggingface_monitor

_LOGGER = logging.getLogger(__name__)


def monitor(
    task_model: Any, dataset: str, sample_rate: float = 0.3, agent: Optional[str] = None
):
    model_monitor = None
    if isinstance(task_model, Language):
        model_monitor = ner_monitor(
            task_model, dataset=dataset, sample_rate=sample_rate
        )
    elif isinstance(task_model, Pipeline):
        model_monitor = huggingface_monitor(
            task_model, dataset=dataset, sample_rate=sample_rate
        )
    elif isinstance(task_model, SequenceTagger):
        model_monitor = flair_monitor(
            task_model, dataset=dataset, sample_rate=sample_rate
        )
    if model_monitor:
        model_monitor.agent = agent
        return model_monitor

    warnings.warn(
        "The provided task model is not supported by monitoring module. "
        "Predictions won't be logged into argilla"
    )
    return task_model
