# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.

from pathlib import Path

from vqa.check import VQACheck


class VQAException(Exception):
    """Base exception class for VQA-related errors."""

    pass


class MustPassCheckFailedError(VQAException):
    """
    Exception raised when a must-pass VQA check fails validation.

    Attributes:
        vqa_check: The VQACheck object that failed
        actual_answer: The actual answer generated by the model
        video_path: Path to the video that was being tested
        test_config_path: Path to the YAML test configuration file
    """

    def __init__(
        self,
        vqa_check: VQACheck,
        actual_answer: str,
        video_path: str | Path,
        test_config_path: str | Path,
    ) -> None:
        self.vqa_check = vqa_check
        self.actual_answer = actual_answer
        self.video_path = Path(video_path)
        self.test_config_path = Path(test_config_path)

        message = (
            f"Must-pass VQA check failed:\n"
            f"  Video: {self.video_path}\n"
            f"  Config: {self.test_config_path}\n"
            f"  Question: {vqa_check.question}\n"
            f"  Expected: {vqa_check.answer}\n"
            f"  Expected keywords: {vqa_check.keywords}\n"
            f"  Actual: {actual_answer}"
        )
        super().__init__(message)


class ValidationError(VQAException):
    """
    Exception raised when VQA validation fails (non-must-pass checks).

    Attributes:
        vqa_check: The VQACheck object that failed
        actual_answer: The actual answer generated by the model
        found_keywords: List of keywords that were found in the answer
    """

    def __init__(
        self,
        vqa_check: VQACheck,
        actual_answer: str,
        found_keywords: list[str],
    ) -> None:
        self.vqa_check = vqa_check
        self.actual_answer = actual_answer
        self.found_keywords = found_keywords

        message = (
            f"VQA validation failed:\n"
            f"  Question: {vqa_check.question}\n"
            f"  Expected: {vqa_check.answer}\n"
            f"  Expected keywords: {vqa_check.keywords}\n"
            f"  Found keywords: {found_keywords}\n"
            f"  Actual: {actual_answer}"
        )
        super().__init__(message)
