# =====================================================
# Victor Evidence SDK
# Version: 1.0
#
# victor_ai_sdk_v1_0.py
#
# Copyright (c) 2026 Victor Evidence
# All Rights Reserved.
#
# LICENSE NOTICE:
# This software is licensed, not sold.
# Use of this SDK is subject to the Victor Evidence
# Terms of Service and applicable subscription agreement:
# https://victorevidence.com/terms.html
#
# Unauthorized copying, redistribution, reverse engineering,
# decompilation, modification, or derivative works are prohibited.
#
# This SDK communicates with the Victor Evidence API and
# requires a valid API key issued under an active subscription.
#
# Victor Evidence performs deterministic semantic admissibility
# evaluation only. It does NOT generate answers, certify truth,
# or replace professional judgment.
#
# Feature availability depends on subscription tier.
# =====================================================

import os
import re
import requests


class VictorAI:

    def __init__(
        self,
        victor_api_key: str,
        victor_api_url: str = None,
    ):
        """
        Initialize Victor Evidence SDK.

        victor_api_key: Issued by Victor Evidence.
        victor_api_url: Optional override endpoint.
        """

        self.victor_api_key = victor_api_key

        # Allow override for future flexibility
        self.victor_api_url = (
            victor_api_url
            or os.getenv("VICTOR_API_URL")
            or "https://victor-api.fly.dev/evaluate"
        )


    # =================================================
    # BUILD SEMANTIC EVIDENCE UNITS
    # =================================================

    def _build_units(self, answer_text: str):

        raw_units = re.split(r"\n+|•|- ", answer_text)

        units = []
        counter = 1

        for unit in raw_units:

            cleaned = unit.strip()

            # Skip short or empty units
            if len(cleaned) < 35:
                continue

            units.append({
                "id": f"evidence_unit_{counter}",
                "text": cleaned,
                "source": "candidate_information",
                "title": f"Semantic Evidence Unit {counter}",
            })

            counter += 1

        return units


    # =================================================
    # EVALUATE EVIDENCE ADMISSIBILITY
    # =================================================

    def validate(self, question: str, answer: str):

        evidence_units = self._build_units(answer)

        response = requests.post(
            self.victor_api_url,
            headers={
                "Content-Type": "application/json",
                "X-API-Key": self.victor_api_key,
            },
            json={
                "question": question,
                "candidates": evidence_units,
                "include_trace": True,
            },
            timeout=30,
        )

        if response.status_code != 200:
            raise Exception(
                f"Victor API Error: {response.status_code} - {response.text}"
            )

        result = response.json()

        return result