# Copyright Amazon.com Inc. or its affiliates. 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. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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 pytest
from pydantic.v1 import ValidationError

from braket.task_result.additional_metadata import AdditionalMetadata


def test_additional_metadata_correct_annealing(problem, dwave_metadata):
    metadata = AdditionalMetadata(action=problem, dwaveMetadata=dwave_metadata)
    assert metadata.action == problem
    assert metadata.dwaveMetadata == dwave_metadata
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


def test_additional_metadata_correct_jaqcd_program(jacqd_program):
    metadata = AdditionalMetadata(action=jacqd_program)
    assert metadata.action == jacqd_program
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


def test_additional_metadata_correct_openqasm_program(openqasm_program):
    metadata = AdditionalMetadata(action=openqasm_program)
    assert metadata.action == openqasm_program
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


def test_additional_metadata_jacqd(oqc_metadata, jacqd_program):
    metadata = AdditionalMetadata(action=jacqd_program, oqcMetadata=oqc_metadata)
    assert metadata.oqcMetadata == oqc_metadata
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


def test_additional_metadata_xanadu(xanadu_metadata, blackbird_program):
    metadata = AdditionalMetadata(action=blackbird_program, xanaduMetadata=xanadu_metadata)
    assert metadata.xanaduMetadata == xanadu_metadata


def test_additional_metadata_ionq(ionq_metadata, openqasm_program):
    metadata = AdditionalMetadata(action=openqasm_program, ionqMetadata=ionq_metadata)
    assert metadata.ionqMetadata == ionq_metadata
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


def test_additional_metadata_oqc(oqc_metadata, openqasm_program):
    metadata = AdditionalMetadata(action=openqasm_program, oqcMetadata=oqc_metadata)
    assert metadata.oqcMetadata == oqc_metadata
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


def test_additional_metadata_iqm(iqm_metadata, openqasm_program):
    metadata = AdditionalMetadata(action=openqasm_program, iqmMetadata=iqm_metadata)
    assert metadata.iqmMetadata == iqm_metadata
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


def test_additional_metadata_iqm_no_action(iqm_metadata):
    metadata = AdditionalMetadata(iqmMetadata=iqm_metadata)
    assert metadata.iqmMetadata == iqm_metadata
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


def test_additional_metadata_quera(quera_metadata, ahs_program):
    metadata = AdditionalMetadata(action=ahs_program, queraMetadata=quera_metadata)
    assert metadata.queraMetadata == quera_metadata
    assert AdditionalMetadata.parse_raw(metadata.json()) == metadata


@pytest.mark.xfail(raises=ValidationError)
def test_incorrect_action(dwave_metadata):
    AdditionalMetadata(action=dwave_metadata)


@pytest.mark.xfail(raises=ValidationError)
def test_incorrect_dwave_metadata(jacqd_program):
    AdditionalMetadata(dwaveMetadata=jacqd_program)
