# -*- coding: utf-8 -*-
# Copyright 2022 Aidentified LLC
#
# 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 requests

import aidentified_matching_api.token_service as token


def get_dataset_id_from_dataset_name(args):
    dataset_params = {"name": args.dataset_name}
    resp_obj = token.token_service.api_call(
        args, requests.get, "/v1/dataset/", params=dataset_params
    )

    if resp_obj["count"] == 0:
        raise Exception(f"No dataset with name '{args.dataset_name}' found")

    return resp_obj["results"][0]["dataset_id"]


def get_dataset_file_id_from_dataset_file_name(args):
    dataset_params = {
        "dataset_name": args.dataset_name,
        "name": args.dataset_file_name,
    }
    resp_obj = token.token_service.api_call(
        args, requests.get, "/v1/dataset-file/", params=dataset_params
    )

    if resp_obj["count"] == 0:
        raise Exception(f"No dataset file with name '{args.dataset_file_name}' found")

    return resp_obj["results"][0]["dataset_file_id"]
