#  Copyright 2020 Soda
#  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 json
from dataclasses import dataclass
from typing import Optional, Any

from sodasql.common.json_helper import JsonHelper
from sodasql.scan.test import Test


@dataclass
class TestResult:

    test: Test
    passed: bool
    values: Optional[dict] = None
    error: Optional[str] = None
    group_values: Optional[dict] = None

    def __str__(self):
        return (f'Test {self.test.description} {"passed" if self.passed else "failed"}' +
                (f" with group values {self.group_values}" if self.group_values else '') +
                f' with metric values {json.dumps(self.values)}')

    def to_json(self):
        if not self.test or not self.test.expression:
            return {
                'error': 'Invalid test result'
            }

        test_result_json = {
            'id': self.test.id,
            'description': self.test.description,
            'expression': self.test.expression
        }

        if self.test.column:
            test_result_json['columnName'] = self.test.column

        if self.error:
            test_result_json['error'] = self.error
        else:
            test_result_json['passed'] = self.passed
            values = {}
            if isinstance(self.values, dict):
                for variable_name in self.values:
                    values[variable_name] = JsonHelper.to_jsonnable(self.values[variable_name])
            test_result_json['values'] = values

        if self.group_values:
            test_result_json['groupValues'] = self.group_values

        return test_result_json
