#
# Copyright (c) 2025, mstoeck3
# All rights reserved.
#
# This source code is licensed under the BSD-3-Clause license found in the
# LICENSE file in the root directory of this source tree.
#

# from .llm import LLMClient
# from .qdrant import QdrantClient

from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, Field

class Filters(BaseModel):
    """
    filters which can be applied to the search query to limit the search space.
    all filters are optional and can be combined.
    """
    case_ids: list[str] | None = Field(default=None, description="list of case IDs to limit the search to")
    artifact_classes: list[str] | None = Field(default=None, description="list of artifact classes to limit the search to")
    source_platforms: list[str] | None = Field(default=None, description="list of source platforms to limit the search to")
    actor_identifiers: list[str] | None = Field(default=None, description="list of actor identifiers to limit the search to")
    indexed_entities_any: list[str] | None = Field(default=None, description="list of indexed entities, any of which must be present in the artifact")
    time_after: datetime | None = Field(default=None, description="only return artifacts after this timestamp (inclusive)")
    time_before: datetime | None = Field(default=None, description="only return artifacts before this timestamp (inclusive)")
    context_group_ids: list[str] | None = Field(default=None, description="list of context group IDs to limit the search to")
    hosts: list[str] | None = Field(default=None, description="list of hostnames or IPs to limit the search to")
    
class Options(BaseModel):
    """
    options to modify the search behavior.
    """
    limit: int = Field(default=100, description="maximum number of search results to return")
    min_score: float | None = Field(default=None, description="minimum similarity score threshold for returned results")
    use_hybrid: bool = Field(default=False, description="whether to use hybrid search (semantic dense vectors + keyword (sparse vectors)")

class Interpretation(BaseModel):
    """
    this data structure holds information about how the semantic query was interpreted by the generative LLM
    """
    original_query: str = Field(description="the original user query before any modifications or interpretations")
    notes: list[str] = Field(default=[], description="any notes or clarifications made by the LLM regarding the query interpretation")

class SemeionSearchObject(BaseModel):
    """
    standard search object structure for the semeion application.

    example JSON representation:
    {
    "_schema_version": "1.1.0",
    
    "query_id": "string (uuid-v4)",
    "created_at": "string (ISO8601)",
    
    "semantic_query": "string",
    
    "filters": {
        "case_ids": ["string"] | null,
        "artifact_classes": ["string"] | null,
        "source_platforms": ["string"] | null,
        "actor_identifiers": ["string"] | null,
        "indexed_entities_any": ["string"] | null,
        "time_after": "string (ISO8601)" | null,
        "time_before": "string (ISO8601)" | null,
        "context_group_ids": ["string"] | null,
        "hosts": ["string"] | null
    },
    
    "options": {
        "limit": "int",
        "min_score": "float" | null,
        "use_hybrid": "bool"
    },
    
    "interpretation": {
        "original_query": "string",
        "notes": ["string"]
        }
    }
    """
    _schema_version: str = "1.1.0"
    query_id: UUID = Field(description="UUID v4 identifying this search query")
    created_at: datetime = Field(description="timestamp when the search query was created for audit")
    semantic_query: str = Field(description="the search string which gets embedded and used for semantic search")
    filters: Filters = Field(description="filters to limit the search space")
    options: Options = Field(description="options to modify the search behavior")
    interpretation: Interpretation = Field(description="information about how the semantic query was interpreted by the generative LLM")
    

